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









share|improve this question


























    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









    share|improve this question
























      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









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Chas Brown

      4,6741519




      4,6741519






















          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 _ _





          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "200"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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 _ _





            share|improve this answer

























              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 _ _





              share|improve this answer























                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 _ _





                share|improve this answer













                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 _ _






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                Bubbler

                5,799757




                5,799757






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)