Weight of the Least Weighted RoD Path
up vote
4
down vote
favorite
Let A
be an m
by n
rectangular matrix of positive integers, where m
and n
are also positive integers.
We are interested in RoD ('Right-or-Down') paths from the upper-left cell of A
to the lower right cell; in an RoD path, each successive cell of the path is either one cell to the Right of or one cell Down from the previous cell.
Given any such RoD path, we can take the sum of the cells in A
in that path.
For example, consider the 4 by 3 matrix:
[ [1, 2, 3, 4],
[5, 1, 6, 7],
[8, 2, 1, 1] ]
Then we can consider the RoD path:
1 > 2 3 4
v
5 1 6 7
v
8 2 > 1 > 1
which has a sum of 1+2+1+2+1+1=8
. It's worth noting that this path has the smallest sum of all possible RoD paths from upper left to lower right in that matrix.
So, the proposed challenge is to provide the shortest function/program in your language of choice that outputs the minimum sum an RoD path from upper left to lower right can have in a given matrix A
.
The usual forbidden loopholes are in effect. Your input can be in any reasonable format; your output must be an integer.
This is code-golf; answers are scored by number of bytes.
Test Cases
[ [ 9 , 1 , 12, 3 ],
[ 12, 11, 6 , 11],
[ 12, 9 , 2 , 11] ] -> 40
[ [ 6 , 8 , 11, 2 ],
[ 3 , 6 , 7 , 6 ],
[ 6 , 2 , 8 , 12] ] -> 37
[ [ 4 , 5 , 8 , 4 ],
[ 6 , 5 , 9 , 4 ],
[ 2 , 5 , 6 , 8 ] ] -> 31
[ [ 4 , 5 , 15, 18, 30],
[ 26, 26, 3 , 4 , 5 ],
[ 7 , 9 , 29, 25, 14],
[ 16, 1 , 27, 13, 27],
[ 23, 11, 25, 24, 12],
[ 17, 23, 7 , 14, 5 ] ] -> 94
[ [ 10, 15, 7 , 2 , 9 ],
[ 24, 5 , 2 , 1 , 25],
[ 2 , 12, 14, 30, 18],
[ 28, 4 , 12, 22, 14],
[ 15, 21, 21, 11, 4 ],
[ 21, 15, 21, 29, 9 ] ] -> 103
code-golf path-finding
add a comment |
up vote
4
down vote
favorite
Let A
be an m
by n
rectangular matrix of positive integers, where m
and n
are also positive integers.
We are interested in RoD ('Right-or-Down') paths from the upper-left cell of A
to the lower right cell; in an RoD path, each successive cell of the path is either one cell to the Right of or one cell Down from the previous cell.
Given any such RoD path, we can take the sum of the cells in A
in that path.
For example, consider the 4 by 3 matrix:
[ [1, 2, 3, 4],
[5, 1, 6, 7],
[8, 2, 1, 1] ]
Then we can consider the RoD path:
1 > 2 3 4
v
5 1 6 7
v
8 2 > 1 > 1
which has a sum of 1+2+1+2+1+1=8
. It's worth noting that this path has the smallest sum of all possible RoD paths from upper left to lower right in that matrix.
So, the proposed challenge is to provide the shortest function/program in your language of choice that outputs the minimum sum an RoD path from upper left to lower right can have in a given matrix A
.
The usual forbidden loopholes are in effect. Your input can be in any reasonable format; your output must be an integer.
This is code-golf; answers are scored by number of bytes.
Test Cases
[ [ 9 , 1 , 12, 3 ],
[ 12, 11, 6 , 11],
[ 12, 9 , 2 , 11] ] -> 40
[ [ 6 , 8 , 11, 2 ],
[ 3 , 6 , 7 , 6 ],
[ 6 , 2 , 8 , 12] ] -> 37
[ [ 4 , 5 , 8 , 4 ],
[ 6 , 5 , 9 , 4 ],
[ 2 , 5 , 6 , 8 ] ] -> 31
[ [ 4 , 5 , 15, 18, 30],
[ 26, 26, 3 , 4 , 5 ],
[ 7 , 9 , 29, 25, 14],
[ 16, 1 , 27, 13, 27],
[ 23, 11, 25, 24, 12],
[ 17, 23, 7 , 14, 5 ] ] -> 94
[ [ 10, 15, 7 , 2 , 9 ],
[ 24, 5 , 2 , 1 , 25],
[ 2 , 12, 14, 30, 18],
[ 28, 4 , 12, 22, 14],
[ 15, 21, 21, 11, 4 ],
[ 21, 15, 21, 29, 9 ] ] -> 103
code-golf path-finding
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Let A
be an m
by n
rectangular matrix of positive integers, where m
and n
are also positive integers.
We are interested in RoD ('Right-or-Down') paths from the upper-left cell of A
to the lower right cell; in an RoD path, each successive cell of the path is either one cell to the Right of or one cell Down from the previous cell.
Given any such RoD path, we can take the sum of the cells in A
in that path.
For example, consider the 4 by 3 matrix:
[ [1, 2, 3, 4],
[5, 1, 6, 7],
[8, 2, 1, 1] ]
Then we can consider the RoD path:
1 > 2 3 4
v
5 1 6 7
v
8 2 > 1 > 1
which has a sum of 1+2+1+2+1+1=8
. It's worth noting that this path has the smallest sum of all possible RoD paths from upper left to lower right in that matrix.
So, the proposed challenge is to provide the shortest function/program in your language of choice that outputs the minimum sum an RoD path from upper left to lower right can have in a given matrix A
.
The usual forbidden loopholes are in effect. Your input can be in any reasonable format; your output must be an integer.
This is code-golf; answers are scored by number of bytes.
Test Cases
[ [ 9 , 1 , 12, 3 ],
[ 12, 11, 6 , 11],
[ 12, 9 , 2 , 11] ] -> 40
[ [ 6 , 8 , 11, 2 ],
[ 3 , 6 , 7 , 6 ],
[ 6 , 2 , 8 , 12] ] -> 37
[ [ 4 , 5 , 8 , 4 ],
[ 6 , 5 , 9 , 4 ],
[ 2 , 5 , 6 , 8 ] ] -> 31
[ [ 4 , 5 , 15, 18, 30],
[ 26, 26, 3 , 4 , 5 ],
[ 7 , 9 , 29, 25, 14],
[ 16, 1 , 27, 13, 27],
[ 23, 11, 25, 24, 12],
[ 17, 23, 7 , 14, 5 ] ] -> 94
[ [ 10, 15, 7 , 2 , 9 ],
[ 24, 5 , 2 , 1 , 25],
[ 2 , 12, 14, 30, 18],
[ 28, 4 , 12, 22, 14],
[ 15, 21, 21, 11, 4 ],
[ 21, 15, 21, 29, 9 ] ] -> 103
code-golf path-finding
Let A
be an m
by n
rectangular matrix of positive integers, where m
and n
are also positive integers.
We are interested in RoD ('Right-or-Down') paths from the upper-left cell of A
to the lower right cell; in an RoD path, each successive cell of the path is either one cell to the Right of or one cell Down from the previous cell.
Given any such RoD path, we can take the sum of the cells in A
in that path.
For example, consider the 4 by 3 matrix:
[ [1, 2, 3, 4],
[5, 1, 6, 7],
[8, 2, 1, 1] ]
Then we can consider the RoD path:
1 > 2 3 4
v
5 1 6 7
v
8 2 > 1 > 1
which has a sum of 1+2+1+2+1+1=8
. It's worth noting that this path has the smallest sum of all possible RoD paths from upper left to lower right in that matrix.
So, the proposed challenge is to provide the shortest function/program in your language of choice that outputs the minimum sum an RoD path from upper left to lower right can have in a given matrix A
.
The usual forbidden loopholes are in effect. Your input can be in any reasonable format; your output must be an integer.
This is code-golf; answers are scored by number of bytes.
Test Cases
[ [ 9 , 1 , 12, 3 ],
[ 12, 11, 6 , 11],
[ 12, 9 , 2 , 11] ] -> 40
[ [ 6 , 8 , 11, 2 ],
[ 3 , 6 , 7 , 6 ],
[ 6 , 2 , 8 , 12] ] -> 37
[ [ 4 , 5 , 8 , 4 ],
[ 6 , 5 , 9 , 4 ],
[ 2 , 5 , 6 , 8 ] ] -> 31
[ [ 4 , 5 , 15, 18, 30],
[ 26, 26, 3 , 4 , 5 ],
[ 7 , 9 , 29, 25, 14],
[ 16, 1 , 27, 13, 27],
[ 23, 11, 25, 24, 12],
[ 17, 23, 7 , 14, 5 ] ] -> 94
[ [ 10, 15, 7 , 2 , 9 ],
[ 24, 5 , 2 , 1 , 25],
[ 2 , 12, 14, 30, 18],
[ 28, 4 , 12, 22, 14],
[ 15, 21, 21, 11, 4 ],
[ 21, 15, 21, 29, 9 ] ] -> 103
code-golf path-finding
code-golf path-finding
asked 2 hours ago
Chas Brown
4,6741519
4,6741519
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
J, 42 bytes
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
Try it online!
How it works
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
v=._1+1#.$ Sum of two dimensions - 1; assign to v
(v is a verb)
(2# ){.!._] Extend the given array in both dimensions
[:</. Extract the antidiagonals as boxed arrays
v @{. Take the first `v` antidiagonals
( )&.>/ Reduce over unboxed items:
}.<.}: Given the right item R, take the minimum of R[1:] and R[:-1]
+ Add to the left item
Illustration
1 2 3 4 Input array, dimensions = 3,4
5 1 6 7
8 2 1 1
1 2 3 4 _ _ Extended to 6,6 with filler _ (infinity)
5 1 6 7 _ _
8 2 1 1 _ _
_ _ _ _ _ _
_ _ _ _ _ _
_ _ _ _ _ _
1 Diagonalize and take first 6 rows
5 2
8 1 3
_ 2 6 4
_ _ 1 7 _
_ _ _ 1 _ _
Reduction: left+min(right[1:], right[:-1])
1 1 => 8
5 2 5 2 => 10 7
8 1 3 8 1 3 => 12 5 11
_ 2 6 4 _ 2 6 4 => _ 4 8 12
_ _ 1 7 _ => _ _ 2 8 _
_ _ _ 1 _ _
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
J, 42 bytes
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
Try it online!
How it works
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
v=._1+1#.$ Sum of two dimensions - 1; assign to v
(v is a verb)
(2# ){.!._] Extend the given array in both dimensions
[:</. Extract the antidiagonals as boxed arrays
v @{. Take the first `v` antidiagonals
( )&.>/ Reduce over unboxed items:
}.<.}: Given the right item R, take the minimum of R[1:] and R[:-1]
+ Add to the left item
Illustration
1 2 3 4 Input array, dimensions = 3,4
5 1 6 7
8 2 1 1
1 2 3 4 _ _ Extended to 6,6 with filler _ (infinity)
5 1 6 7 _ _
8 2 1 1 _ _
_ _ _ _ _ _
_ _ _ _ _ _
_ _ _ _ _ _
1 Diagonalize and take first 6 rows
5 2
8 1 3
_ 2 6 4
_ _ 1 7 _
_ _ _ 1 _ _
Reduction: left+min(right[1:], right[:-1])
1 1 => 8
5 2 5 2 => 10 7
8 1 3 8 1 3 => 12 5 11
_ 2 6 4 _ 2 6 4 => _ 4 8 12
_ _ 1 7 _ => _ _ 2 8 _
_ _ _ 1 _ _
add a comment |
up vote
2
down vote
J, 42 bytes
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
Try it online!
How it works
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
v=._1+1#.$ Sum of two dimensions - 1; assign to v
(v is a verb)
(2# ){.!._] Extend the given array in both dimensions
[:</. Extract the antidiagonals as boxed arrays
v @{. Take the first `v` antidiagonals
( )&.>/ Reduce over unboxed items:
}.<.}: Given the right item R, take the minimum of R[1:] and R[:-1]
+ Add to the left item
Illustration
1 2 3 4 Input array, dimensions = 3,4
5 1 6 7
8 2 1 1
1 2 3 4 _ _ Extended to 6,6 with filler _ (infinity)
5 1 6 7 _ _
8 2 1 1 _ _
_ _ _ _ _ _
_ _ _ _ _ _
_ _ _ _ _ _
1 Diagonalize and take first 6 rows
5 2
8 1 3
_ 2 6 4
_ _ 1 7 _
_ _ _ 1 _ _
Reduction: left+min(right[1:], right[:-1])
1 1 => 8
5 2 5 2 => 10 7
8 1 3 8 1 3 => 12 5 11
_ 2 6 4 _ 2 6 4 => _ 4 8 12
_ _ 1 7 _ => _ _ 2 8 _
_ _ _ 1 _ _
add a comment |
up vote
2
down vote
up vote
2
down vote
J, 42 bytes
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
Try it online!
How it works
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
v=._1+1#.$ Sum of two dimensions - 1; assign to v
(v is a verb)
(2# ){.!._] Extend the given array in both dimensions
[:</. Extract the antidiagonals as boxed arrays
v @{. Take the first `v` antidiagonals
( )&.>/ Reduce over unboxed items:
}.<.}: Given the right item R, take the minimum of R[1:] and R[:-1]
+ Add to the left item
Illustration
1 2 3 4 Input array, dimensions = 3,4
5 1 6 7
8 2 1 1
1 2 3 4 _ _ Extended to 6,6 with filler _ (infinity)
5 1 6 7 _ _
8 2 1 1 _ _
_ _ _ _ _ _
_ _ _ _ _ _
_ _ _ _ _ _
1 Diagonalize and take first 6 rows
5 2
8 1 3
_ 2 6 4
_ _ 1 7 _
_ _ _ 1 _ _
Reduction: left+min(right[1:], right[:-1])
1 1 => 8
5 2 5 2 => 10 7
8 1 3 8 1 3 => 12 5 11
_ 2 6 4 _ 2 6 4 => _ 4 8 12
_ _ 1 7 _ => _ _ 2 8 _
_ _ _ 1 _ _
J, 42 bytes
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
Try it online!
How it works
v(+}.<.}:)&.>/@{.[:</.(2#v=._1+1#.$){.!._]
v=._1+1#.$ Sum of two dimensions - 1; assign to v
(v is a verb)
(2# ){.!._] Extend the given array in both dimensions
[:</. Extract the antidiagonals as boxed arrays
v @{. Take the first `v` antidiagonals
( )&.>/ Reduce over unboxed items:
}.<.}: Given the right item R, take the minimum of R[1:] and R[:-1]
+ Add to the left item
Illustration
1 2 3 4 Input array, dimensions = 3,4
5 1 6 7
8 2 1 1
1 2 3 4 _ _ Extended to 6,6 with filler _ (infinity)
5 1 6 7 _ _
8 2 1 1 _ _
_ _ _ _ _ _
_ _ _ _ _ _
_ _ _ _ _ _
1 Diagonalize and take first 6 rows
5 2
8 1 3
_ 2 6 4
_ _ 1 7 _
_ _ _ 1 _ _
Reduction: left+min(right[1:], right[:-1])
1 1 => 8
5 2 5 2 => 10 7
8 1 3 8 1 3 => 12 5 11
_ 2 6 4 _ 2 6 4 => _ 4 8 12
_ _ 1 7 _ => _ _ 2 8 _
_ _ _ 1 _ _
answered 1 hour ago
Bubbler
5,799757
5,799757
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176563%2fweight-of-the-least-weighted-rod-path%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown