Length of the Longest Descent












5














Your task is to determine the length of the longest descent down a "mountain" represented as a grid of integer heights. A "descent" is any path from a starting cell to orthogonally adjacent cells with strictly decreasing heights (i.e. not diagonal and not to the same height). For instance, you can move from 5-4-3-1 but not 5-5-4-3-3-2-1. The length of this path is how many cell movements there are from the starting cell to the ending cell, thus 5-4-3-1 is length 3.



You will receive a rectangular grid as input and you should output an integer indicating the longest descent.



Examples



1 2 3 2 2
3 4 5 5 5
3 4 6 7 4
3 3 5 6 2
1 1 2 3 1


The length of the longest descent down this mountain is 5. The longest path starts at the 7, moves left, up, left, up, and then left (7-6-5-4-2-1). Since there are 5 movements in this path, the path length is 5.



1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


Since this height map is flat, the longest descent is 0.



Height maps can be made up of larger numbers than single-digit numbers.



949858 789874  57848  43758 387348
5848 454115 4548 448545 216464
188452 484126 484216 786654 145451
189465 474566 156665 132645 456651
985464 94849 151654 151648 484364


The longest descent here is of length 7. (786654, 484216, 484126, 474566, 156665, 151654, 151648, 132645)



Rules and Notes




  • Grids may be taken in any convenient format. Specify your format in your answer.

  • You may assume the height map is perfectly rectangular and contains only positive integers in the signed 32-bit integer range.

  • The longest descent path can begin and end anywhere on the grid.

  • You do not need to describe the longest descent path in any way. Only its length is required.

  • Shortest code wins










share|improve this question






















  • How should the last example be interpreted?
    – Peter Taylor
    2 hours ago










  • @PeterTaylor I'm not sure what you mean.
    – Beefster
    1 hour ago










  • I think the last example is just a matrix of multi digit numbers
    – Embodiment of Ignorance
    1 hour ago
















5














Your task is to determine the length of the longest descent down a "mountain" represented as a grid of integer heights. A "descent" is any path from a starting cell to orthogonally adjacent cells with strictly decreasing heights (i.e. not diagonal and not to the same height). For instance, you can move from 5-4-3-1 but not 5-5-4-3-3-2-1. The length of this path is how many cell movements there are from the starting cell to the ending cell, thus 5-4-3-1 is length 3.



You will receive a rectangular grid as input and you should output an integer indicating the longest descent.



Examples



1 2 3 2 2
3 4 5 5 5
3 4 6 7 4
3 3 5 6 2
1 1 2 3 1


The length of the longest descent down this mountain is 5. The longest path starts at the 7, moves left, up, left, up, and then left (7-6-5-4-2-1). Since there are 5 movements in this path, the path length is 5.



1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


Since this height map is flat, the longest descent is 0.



Height maps can be made up of larger numbers than single-digit numbers.



949858 789874  57848  43758 387348
5848 454115 4548 448545 216464
188452 484126 484216 786654 145451
189465 474566 156665 132645 456651
985464 94849 151654 151648 484364


The longest descent here is of length 7. (786654, 484216, 484126, 474566, 156665, 151654, 151648, 132645)



Rules and Notes




  • Grids may be taken in any convenient format. Specify your format in your answer.

  • You may assume the height map is perfectly rectangular and contains only positive integers in the signed 32-bit integer range.

  • The longest descent path can begin and end anywhere on the grid.

  • You do not need to describe the longest descent path in any way. Only its length is required.

  • Shortest code wins










share|improve this question






















  • How should the last example be interpreted?
    – Peter Taylor
    2 hours ago










  • @PeterTaylor I'm not sure what you mean.
    – Beefster
    1 hour ago










  • I think the last example is just a matrix of multi digit numbers
    – Embodiment of Ignorance
    1 hour ago














5












5








5







Your task is to determine the length of the longest descent down a "mountain" represented as a grid of integer heights. A "descent" is any path from a starting cell to orthogonally adjacent cells with strictly decreasing heights (i.e. not diagonal and not to the same height). For instance, you can move from 5-4-3-1 but not 5-5-4-3-3-2-1. The length of this path is how many cell movements there are from the starting cell to the ending cell, thus 5-4-3-1 is length 3.



You will receive a rectangular grid as input and you should output an integer indicating the longest descent.



Examples



1 2 3 2 2
3 4 5 5 5
3 4 6 7 4
3 3 5 6 2
1 1 2 3 1


The length of the longest descent down this mountain is 5. The longest path starts at the 7, moves left, up, left, up, and then left (7-6-5-4-2-1). Since there are 5 movements in this path, the path length is 5.



1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


Since this height map is flat, the longest descent is 0.



Height maps can be made up of larger numbers than single-digit numbers.



949858 789874  57848  43758 387348
5848 454115 4548 448545 216464
188452 484126 484216 786654 145451
189465 474566 156665 132645 456651
985464 94849 151654 151648 484364


The longest descent here is of length 7. (786654, 484216, 484126, 474566, 156665, 151654, 151648, 132645)



Rules and Notes




  • Grids may be taken in any convenient format. Specify your format in your answer.

  • You may assume the height map is perfectly rectangular and contains only positive integers in the signed 32-bit integer range.

  • The longest descent path can begin and end anywhere on the grid.

  • You do not need to describe the longest descent path in any way. Only its length is required.

  • Shortest code wins










share|improve this question













Your task is to determine the length of the longest descent down a "mountain" represented as a grid of integer heights. A "descent" is any path from a starting cell to orthogonally adjacent cells with strictly decreasing heights (i.e. not diagonal and not to the same height). For instance, you can move from 5-4-3-1 but not 5-5-4-3-3-2-1. The length of this path is how many cell movements there are from the starting cell to the ending cell, thus 5-4-3-1 is length 3.



You will receive a rectangular grid as input and you should output an integer indicating the longest descent.



Examples



1 2 3 2 2
3 4 5 5 5
3 4 6 7 4
3 3 5 6 2
1 1 2 3 1


The length of the longest descent down this mountain is 5. The longest path starts at the 7, moves left, up, left, up, and then left (7-6-5-4-2-1). Since there are 5 movements in this path, the path length is 5.



1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


Since this height map is flat, the longest descent is 0.



Height maps can be made up of larger numbers than single-digit numbers.



949858 789874  57848  43758 387348
5848 454115 4548 448545 216464
188452 484126 484216 786654 145451
189465 474566 156665 132645 456651
985464 94849 151654 151648 484364


The longest descent here is of length 7. (786654, 484216, 484126, 474566, 156665, 151654, 151648, 132645)



Rules and Notes




  • Grids may be taken in any convenient format. Specify your format in your answer.

  • You may assume the height map is perfectly rectangular and contains only positive integers in the signed 32-bit integer range.

  • The longest descent path can begin and end anywhere on the grid.

  • You do not need to describe the longest descent path in any way. Only its length is required.

  • Shortest code wins







code-golf grid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









Beefster

1,053722




1,053722












  • How should the last example be interpreted?
    – Peter Taylor
    2 hours ago










  • @PeterTaylor I'm not sure what you mean.
    – Beefster
    1 hour ago










  • I think the last example is just a matrix of multi digit numbers
    – Embodiment of Ignorance
    1 hour ago


















  • How should the last example be interpreted?
    – Peter Taylor
    2 hours ago










  • @PeterTaylor I'm not sure what you mean.
    – Beefster
    1 hour ago










  • I think the last example is just a matrix of multi digit numbers
    – Embodiment of Ignorance
    1 hour ago
















How should the last example be interpreted?
– Peter Taylor
2 hours ago




How should the last example be interpreted?
– Peter Taylor
2 hours ago












@PeterTaylor I'm not sure what you mean.
– Beefster
1 hour ago




@PeterTaylor I'm not sure what you mean.
– Beefster
1 hour ago












I think the last example is just a matrix of multi digit numbers
– Embodiment of Ignorance
1 hour ago




I think the last example is just a matrix of multi digit numbers
– Embodiment of Ignorance
1 hour ago










6 Answers
6






active

oldest

votes


















1














Python, 150 147 140 bytes



-7 bytes thanks to wizzwizz4!



l=lambda g,i,j:1+max(0<i+m<6>j+n>0<g[i+m,j+n]<g[i,j]and l(g,i+m,j+n)for m,n in((-1,0),(1,0),(0,-1),(0,1)))
lambda g:max(l(g,*t)for t in g)-1


Try it online!



Like the other Python answer, this takes input in the form of a dictionary (x, y): value; however, I use 1-indexed dictionaries.



I definitely think this could be golfed some more, especially the in((-1,0),(1,0),(0,-1),(0,1)) part and the use of two lambdas.






share|improve this answer



















  • 1




    "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
    – Jonathan Allan
    37 mins ago










  • Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
    – ArBo
    35 mins ago










  • Positive means greater than zero (non-negative would include it)
    – Jonathan Allan
    34 mins ago










  • I see, in Dutch "positive" generally includes zero. Interesting!
    – ArBo
    33 mins ago










  • Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
    – wizzwizz4
    13 mins ago



















0















Clean, 209 bytes



import StdEnv,Data.List
z=zipWith
$l=maximum[length k\p<-permutations[(v,[x,y])\y<-[0..]&u<-l,x<-[0..]&v<-u],(k,[m:n])<-map unzip(subsequences p)|prod(map(sum o map abs)(z(z(-))n[m:n]))==1&&and(z(>)k(tl k))]


Try it online!



A brute-force solution taking a list-of-lists-of-integers ([[Int]]).

The TIO driver takes the same format as the examples through STDIN.



It's too slow to run any of the examples on TIO and probably locally too, but works in theory.



This one does the same thing faster, can do 3x3 or 2x4 on TIO and 4x4 and 3x5 locally.






share|improve this answer





















  • This seems to return one too much.
    – BMO
    58 mins ago










  • @BMO can you provide an input instance for that so I can fix it?
    – Οurous
    22 mins ago










  • Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
    – BMO
    19 mins ago



















0















Haskell, 165 bytes





Needs $texttt{-XNoMonomorphismRestriction}$:



f m|r<-[0..length m-1]=h[x!y$|x<-r,y<-r,let(x!y)p=h[1+(u!v$(x,y):p)|i<-[-1,1],(u,v)<-[(x+i,y),(x,y+i)],u#r,v#r,m!!u!!v<m!!x!!y,not$(u,v)#p]]
(#)=elem
h=foldl max 0


Try it online!



Alternatively we could use notElem over (not.).(#) without the flag for $+2$ bytes:



Try it online!



Explanation & Ungolfed



Strategy: Recursively try all feasible paths, keeping track of visited entries and maximize their length.



Let's first define some helpers.. Since we need elem and notElem, let's use (#) for elem. Also, to maximize we'll need a total function (maximize is not), returning $0$ when the list is empty:



safeMaximum = foldl max 0


Now we're ready to define our recursive function fun :: [[Integer]] -> Integer:



fun xs
| r <- [0..length m-1] -- all possible indices of xs (luckily it's square)
= safeMaximum -- maximize ..
[ x!y $ -- .. initially we haven't visited any others
| x <- r, y<-r -- .. all possible entries
-- For the purpose of golfing we define (!) in the list-comprehension, it takes the current entry (x,y) and all visited ones (p)
, let (x!y) p = safeMaximum -- maximize ..
[ 1 + (u!v$(x,y):p) -- .. recurse, adding (x,y) to the visited nodes & increment (the next path will be 1 longer)
| i <- [-1,1] -- offsets [left/up,right/down]
, (u,v) <-[(x+i,y),(x,y+i)] -- next entry-candidate
, u#r, v#r -- make sure indices are in bound ..
, m!!u!!v < m!!x!!y -- .. , the the path is decreasing
, not$(u,v)#p -- .. and we haven't already visited that element
]
]
(#)=elem





share|improve this answer































    0














    JavaScript (ES7),  106 103 102  98 bytes





    f=(a,x,y,n=m=-1,p)=>a.map((r,Y)=>r.map((v,X)=>(x-X)**2+(y-Y)**2-1|v/p?m=n<m?m:n:f(a,X,Y,n+1,v)))|m


    Try it online!






    share|improve this answer































      0















      Jelly, 25 bytes



      ZIASỊẠ
      ŒỤṚŒPḊÇƇœị⁸>Ɲ€ẠƇṪL


      Try it online! (way to inefficient for the examples - path here is 95 94 93 83 77 40 10 so 6 is yielded)






      share|improve this answer































        0














        Python 3, 263 227 bytes



        def f(m):
        p={(x,y):[c for i in[-1,1]for c in[(x,y+i),(x+i,y)]]for x,y in m};d={c:0 for c in p if not p[c]}
        while len(p)-len(d):
        for c in p:
        for b in p[c]:
        if b in d:d[c]=max(d[b]+1,d.get(c,0))
        return max(d.values())


        Try it online!



        -2 bytes thanks to BMO



        Takes grids in the format {(0, 0): 1, (1, 0): 2, ...}. This format can be generated from the example format using the following utility function:



        lambda s,e=enumerate:{(x,y):int(n)for y,l in e(s.split('n'))for x,n in e(l.split())}





        share|improve this answer



















        • 1




          This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
          – BMO
          52 mins ago











        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',
        autoActivateHeartbeat: false,
        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%2f178326%2flength-of-the-longest-descent%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Python, 150 147 140 bytes



        -7 bytes thanks to wizzwizz4!



        l=lambda g,i,j:1+max(0<i+m<6>j+n>0<g[i+m,j+n]<g[i,j]and l(g,i+m,j+n)for m,n in((-1,0),(1,0),(0,-1),(0,1)))
        lambda g:max(l(g,*t)for t in g)-1


        Try it online!



        Like the other Python answer, this takes input in the form of a dictionary (x, y): value; however, I use 1-indexed dictionaries.



        I definitely think this could be golfed some more, especially the in((-1,0),(1,0),(0,-1),(0,1)) part and the use of two lambdas.






        share|improve this answer



















        • 1




          "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
          – Jonathan Allan
          37 mins ago










        • Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
          – ArBo
          35 mins ago










        • Positive means greater than zero (non-negative would include it)
          – Jonathan Allan
          34 mins ago










        • I see, in Dutch "positive" generally includes zero. Interesting!
          – ArBo
          33 mins ago










        • Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
          – wizzwizz4
          13 mins ago
















        1














        Python, 150 147 140 bytes



        -7 bytes thanks to wizzwizz4!



        l=lambda g,i,j:1+max(0<i+m<6>j+n>0<g[i+m,j+n]<g[i,j]and l(g,i+m,j+n)for m,n in((-1,0),(1,0),(0,-1),(0,1)))
        lambda g:max(l(g,*t)for t in g)-1


        Try it online!



        Like the other Python answer, this takes input in the form of a dictionary (x, y): value; however, I use 1-indexed dictionaries.



        I definitely think this could be golfed some more, especially the in((-1,0),(1,0),(0,-1),(0,1)) part and the use of two lambdas.






        share|improve this answer



















        • 1




          "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
          – Jonathan Allan
          37 mins ago










        • Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
          – ArBo
          35 mins ago










        • Positive means greater than zero (non-negative would include it)
          – Jonathan Allan
          34 mins ago










        • I see, in Dutch "positive" generally includes zero. Interesting!
          – ArBo
          33 mins ago










        • Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
          – wizzwizz4
          13 mins ago














        1












        1








        1






        Python, 150 147 140 bytes



        -7 bytes thanks to wizzwizz4!



        l=lambda g,i,j:1+max(0<i+m<6>j+n>0<g[i+m,j+n]<g[i,j]and l(g,i+m,j+n)for m,n in((-1,0),(1,0),(0,-1),(0,1)))
        lambda g:max(l(g,*t)for t in g)-1


        Try it online!



        Like the other Python answer, this takes input in the form of a dictionary (x, y): value; however, I use 1-indexed dictionaries.



        I definitely think this could be golfed some more, especially the in((-1,0),(1,0),(0,-1),(0,1)) part and the use of two lambdas.






        share|improve this answer














        Python, 150 147 140 bytes



        -7 bytes thanks to wizzwizz4!



        l=lambda g,i,j:1+max(0<i+m<6>j+n>0<g[i+m,j+n]<g[i,j]and l(g,i+m,j+n)for m,n in((-1,0),(1,0),(0,-1),(0,1)))
        lambda g:max(l(g,*t)for t in g)-1


        Try it online!



        Like the other Python answer, this takes input in the form of a dictionary (x, y): value; however, I use 1-indexed dictionaries.



        I definitely think this could be golfed some more, especially the in((-1,0),(1,0),(0,-1),(0,1)) part and the use of two lambdas.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 mins ago

























        answered 41 mins ago









        ArBo

        314




        314








        • 1




          "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
          – Jonathan Allan
          37 mins ago










        • Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
          – ArBo
          35 mins ago










        • Positive means greater than zero (non-negative would include it)
          – Jonathan Allan
          34 mins ago










        • I see, in Dutch "positive" generally includes zero. Interesting!
          – ArBo
          33 mins ago










        • Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
          – wizzwizz4
          13 mins ago














        • 1




          "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
          – Jonathan Allan
          37 mins ago










        • Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
          – ArBo
          35 mins ago










        • Positive means greater than zero (non-negative would include it)
          – Jonathan Allan
          34 mins ago










        • I see, in Dutch "positive" generally includes zero. Interesting!
          – ArBo
          33 mins ago










        • Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
          – wizzwizz4
          13 mins ago








        1




        1




        "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
        – Jonathan Allan
        37 mins ago




        "You may assume ... and contains only positive integers in the signed 32-bit integer range" - so assume away :)
        – Jonathan Allan
        37 mins ago












        Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
        – ArBo
        35 mins ago




        Ah, only saw the "signed" part, and not the "positive" :) Can they be zero?
        – ArBo
        35 mins ago












        Positive means greater than zero (non-negative would include it)
        – Jonathan Allan
        34 mins ago




        Positive means greater than zero (non-negative would include it)
        – Jonathan Allan
        34 mins ago












        I see, in Dutch "positive" generally includes zero. Interesting!
        – ArBo
        33 mins ago




        I see, in Dutch "positive" generally includes zero. Interesting!
        – ArBo
        33 mins ago












        Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
        – wizzwizz4
        13 mins ago




        Why do you need g.keys()? Wouldn't g suffice for -7 bytes?
        – wizzwizz4
        13 mins ago











        0















        Clean, 209 bytes



        import StdEnv,Data.List
        z=zipWith
        $l=maximum[length k\p<-permutations[(v,[x,y])\y<-[0..]&u<-l,x<-[0..]&v<-u],(k,[m:n])<-map unzip(subsequences p)|prod(map(sum o map abs)(z(z(-))n[m:n]))==1&&and(z(>)k(tl k))]


        Try it online!



        A brute-force solution taking a list-of-lists-of-integers ([[Int]]).

        The TIO driver takes the same format as the examples through STDIN.



        It's too slow to run any of the examples on TIO and probably locally too, but works in theory.



        This one does the same thing faster, can do 3x3 or 2x4 on TIO and 4x4 and 3x5 locally.






        share|improve this answer





















        • This seems to return one too much.
          – BMO
          58 mins ago










        • @BMO can you provide an input instance for that so I can fix it?
          – Οurous
          22 mins ago










        • Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
          – BMO
          19 mins ago
















        0















        Clean, 209 bytes



        import StdEnv,Data.List
        z=zipWith
        $l=maximum[length k\p<-permutations[(v,[x,y])\y<-[0..]&u<-l,x<-[0..]&v<-u],(k,[m:n])<-map unzip(subsequences p)|prod(map(sum o map abs)(z(z(-))n[m:n]))==1&&and(z(>)k(tl k))]


        Try it online!



        A brute-force solution taking a list-of-lists-of-integers ([[Int]]).

        The TIO driver takes the same format as the examples through STDIN.



        It's too slow to run any of the examples on TIO and probably locally too, but works in theory.



        This one does the same thing faster, can do 3x3 or 2x4 on TIO and 4x4 and 3x5 locally.






        share|improve this answer





















        • This seems to return one too much.
          – BMO
          58 mins ago










        • @BMO can you provide an input instance for that so I can fix it?
          – Οurous
          22 mins ago










        • Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
          – BMO
          19 mins ago














        0












        0








        0







        Clean, 209 bytes



        import StdEnv,Data.List
        z=zipWith
        $l=maximum[length k\p<-permutations[(v,[x,y])\y<-[0..]&u<-l,x<-[0..]&v<-u],(k,[m:n])<-map unzip(subsequences p)|prod(map(sum o map abs)(z(z(-))n[m:n]))==1&&and(z(>)k(tl k))]


        Try it online!



        A brute-force solution taking a list-of-lists-of-integers ([[Int]]).

        The TIO driver takes the same format as the examples through STDIN.



        It's too slow to run any of the examples on TIO and probably locally too, but works in theory.



        This one does the same thing faster, can do 3x3 or 2x4 on TIO and 4x4 and 3x5 locally.






        share|improve this answer













        Clean, 209 bytes



        import StdEnv,Data.List
        z=zipWith
        $l=maximum[length k\p<-permutations[(v,[x,y])\y<-[0..]&u<-l,x<-[0..]&v<-u],(k,[m:n])<-map unzip(subsequences p)|prod(map(sum o map abs)(z(z(-))n[m:n]))==1&&and(z(>)k(tl k))]


        Try it online!



        A brute-force solution taking a list-of-lists-of-integers ([[Int]]).

        The TIO driver takes the same format as the examples through STDIN.



        It's too slow to run any of the examples on TIO and probably locally too, but works in theory.



        This one does the same thing faster, can do 3x3 or 2x4 on TIO and 4x4 and 3x5 locally.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        Οurous

        6,46211033




        6,46211033












        • This seems to return one too much.
          – BMO
          58 mins ago










        • @BMO can you provide an input instance for that so I can fix it?
          – Οurous
          22 mins ago










        • Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
          – BMO
          19 mins ago


















        • This seems to return one too much.
          – BMO
          58 mins ago










        • @BMO can you provide an input instance for that so I can fix it?
          – Οurous
          22 mins ago










        • Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
          – BMO
          19 mins ago
















        This seems to return one too much.
        – BMO
        58 mins ago




        This seems to return one too much.
        – BMO
        58 mins ago












        @BMO can you provide an input instance for that so I can fix it?
        – Οurous
        22 mins ago




        @BMO can you provide an input instance for that so I can fix it?
        – Οurous
        22 mins ago












        Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
        – BMO
        19 mins ago




        Any? For example [[1]] or [[4,1,2],[3,1,1]], your solution returns the number of entries, but you should return the length of the path (ie. -1).
        – BMO
        19 mins ago











        0















        Haskell, 165 bytes





        Needs $texttt{-XNoMonomorphismRestriction}$:



        f m|r<-[0..length m-1]=h[x!y$|x<-r,y<-r,let(x!y)p=h[1+(u!v$(x,y):p)|i<-[-1,1],(u,v)<-[(x+i,y),(x,y+i)],u#r,v#r,m!!u!!v<m!!x!!y,not$(u,v)#p]]
        (#)=elem
        h=foldl max 0


        Try it online!



        Alternatively we could use notElem over (not.).(#) without the flag for $+2$ bytes:



        Try it online!



        Explanation & Ungolfed



        Strategy: Recursively try all feasible paths, keeping track of visited entries and maximize their length.



        Let's first define some helpers.. Since we need elem and notElem, let's use (#) for elem. Also, to maximize we'll need a total function (maximize is not), returning $0$ when the list is empty:



        safeMaximum = foldl max 0


        Now we're ready to define our recursive function fun :: [[Integer]] -> Integer:



        fun xs
        | r <- [0..length m-1] -- all possible indices of xs (luckily it's square)
        = safeMaximum -- maximize ..
        [ x!y $ -- .. initially we haven't visited any others
        | x <- r, y<-r -- .. all possible entries
        -- For the purpose of golfing we define (!) in the list-comprehension, it takes the current entry (x,y) and all visited ones (p)
        , let (x!y) p = safeMaximum -- maximize ..
        [ 1 + (u!v$(x,y):p) -- .. recurse, adding (x,y) to the visited nodes & increment (the next path will be 1 longer)
        | i <- [-1,1] -- offsets [left/up,right/down]
        , (u,v) <-[(x+i,y),(x,y+i)] -- next entry-candidate
        , u#r, v#r -- make sure indices are in bound ..
        , m!!u!!v < m!!x!!y -- .. , the the path is decreasing
        , not$(u,v)#p -- .. and we haven't already visited that element
        ]
        ]
        (#)=elem





        share|improve this answer




























          0















          Haskell, 165 bytes





          Needs $texttt{-XNoMonomorphismRestriction}$:



          f m|r<-[0..length m-1]=h[x!y$|x<-r,y<-r,let(x!y)p=h[1+(u!v$(x,y):p)|i<-[-1,1],(u,v)<-[(x+i,y),(x,y+i)],u#r,v#r,m!!u!!v<m!!x!!y,not$(u,v)#p]]
          (#)=elem
          h=foldl max 0


          Try it online!



          Alternatively we could use notElem over (not.).(#) without the flag for $+2$ bytes:



          Try it online!



          Explanation & Ungolfed



          Strategy: Recursively try all feasible paths, keeping track of visited entries and maximize their length.



          Let's first define some helpers.. Since we need elem and notElem, let's use (#) for elem. Also, to maximize we'll need a total function (maximize is not), returning $0$ when the list is empty:



          safeMaximum = foldl max 0


          Now we're ready to define our recursive function fun :: [[Integer]] -> Integer:



          fun xs
          | r <- [0..length m-1] -- all possible indices of xs (luckily it's square)
          = safeMaximum -- maximize ..
          [ x!y $ -- .. initially we haven't visited any others
          | x <- r, y<-r -- .. all possible entries
          -- For the purpose of golfing we define (!) in the list-comprehension, it takes the current entry (x,y) and all visited ones (p)
          , let (x!y) p = safeMaximum -- maximize ..
          [ 1 + (u!v$(x,y):p) -- .. recurse, adding (x,y) to the visited nodes & increment (the next path will be 1 longer)
          | i <- [-1,1] -- offsets [left/up,right/down]
          , (u,v) <-[(x+i,y),(x,y+i)] -- next entry-candidate
          , u#r, v#r -- make sure indices are in bound ..
          , m!!u!!v < m!!x!!y -- .. , the the path is decreasing
          , not$(u,v)#p -- .. and we haven't already visited that element
          ]
          ]
          (#)=elem





          share|improve this answer


























            0












            0








            0







            Haskell, 165 bytes





            Needs $texttt{-XNoMonomorphismRestriction}$:



            f m|r<-[0..length m-1]=h[x!y$|x<-r,y<-r,let(x!y)p=h[1+(u!v$(x,y):p)|i<-[-1,1],(u,v)<-[(x+i,y),(x,y+i)],u#r,v#r,m!!u!!v<m!!x!!y,not$(u,v)#p]]
            (#)=elem
            h=foldl max 0


            Try it online!



            Alternatively we could use notElem over (not.).(#) without the flag for $+2$ bytes:



            Try it online!



            Explanation & Ungolfed



            Strategy: Recursively try all feasible paths, keeping track of visited entries and maximize their length.



            Let's first define some helpers.. Since we need elem and notElem, let's use (#) for elem. Also, to maximize we'll need a total function (maximize is not), returning $0$ when the list is empty:



            safeMaximum = foldl max 0


            Now we're ready to define our recursive function fun :: [[Integer]] -> Integer:



            fun xs
            | r <- [0..length m-1] -- all possible indices of xs (luckily it's square)
            = safeMaximum -- maximize ..
            [ x!y $ -- .. initially we haven't visited any others
            | x <- r, y<-r -- .. all possible entries
            -- For the purpose of golfing we define (!) in the list-comprehension, it takes the current entry (x,y) and all visited ones (p)
            , let (x!y) p = safeMaximum -- maximize ..
            [ 1 + (u!v$(x,y):p) -- .. recurse, adding (x,y) to the visited nodes & increment (the next path will be 1 longer)
            | i <- [-1,1] -- offsets [left/up,right/down]
            , (u,v) <-[(x+i,y),(x,y+i)] -- next entry-candidate
            , u#r, v#r -- make sure indices are in bound ..
            , m!!u!!v < m!!x!!y -- .. , the the path is decreasing
            , not$(u,v)#p -- .. and we haven't already visited that element
            ]
            ]
            (#)=elem





            share|improve this answer















            Haskell, 165 bytes





            Needs $texttt{-XNoMonomorphismRestriction}$:



            f m|r<-[0..length m-1]=h[x!y$|x<-r,y<-r,let(x!y)p=h[1+(u!v$(x,y):p)|i<-[-1,1],(u,v)<-[(x+i,y),(x,y+i)],u#r,v#r,m!!u!!v<m!!x!!y,not$(u,v)#p]]
            (#)=elem
            h=foldl max 0


            Try it online!



            Alternatively we could use notElem over (not.).(#) without the flag for $+2$ bytes:



            Try it online!



            Explanation & Ungolfed



            Strategy: Recursively try all feasible paths, keeping track of visited entries and maximize their length.



            Let's first define some helpers.. Since we need elem and notElem, let's use (#) for elem. Also, to maximize we'll need a total function (maximize is not), returning $0$ when the list is empty:



            safeMaximum = foldl max 0


            Now we're ready to define our recursive function fun :: [[Integer]] -> Integer:



            fun xs
            | r <- [0..length m-1] -- all possible indices of xs (luckily it's square)
            = safeMaximum -- maximize ..
            [ x!y $ -- .. initially we haven't visited any others
            | x <- r, y<-r -- .. all possible entries
            -- For the purpose of golfing we define (!) in the list-comprehension, it takes the current entry (x,y) and all visited ones (p)
            , let (x!y) p = safeMaximum -- maximize ..
            [ 1 + (u!v$(x,y):p) -- .. recurse, adding (x,y) to the visited nodes & increment (the next path will be 1 longer)
            | i <- [-1,1] -- offsets [left/up,right/down]
            , (u,v) <-[(x+i,y),(x,y+i)] -- next entry-candidate
            , u#r, v#r -- make sure indices are in bound ..
            , m!!u!!v < m!!x!!y -- .. , the the path is decreasing
            , not$(u,v)#p -- .. and we haven't already visited that element
            ]
            ]
            (#)=elem






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            BMO

            11.5k22187




            11.5k22187























                0














                JavaScript (ES7),  106 103 102  98 bytes





                f=(a,x,y,n=m=-1,p)=>a.map((r,Y)=>r.map((v,X)=>(x-X)**2+(y-Y)**2-1|v/p?m=n<m?m:n:f(a,X,Y,n+1,v)))|m


                Try it online!






                share|improve this answer




























                  0














                  JavaScript (ES7),  106 103 102  98 bytes





                  f=(a,x,y,n=m=-1,p)=>a.map((r,Y)=>r.map((v,X)=>(x-X)**2+(y-Y)**2-1|v/p?m=n<m?m:n:f(a,X,Y,n+1,v)))|m


                  Try it online!






                  share|improve this answer


























                    0












                    0








                    0






                    JavaScript (ES7),  106 103 102  98 bytes





                    f=(a,x,y,n=m=-1,p)=>a.map((r,Y)=>r.map((v,X)=>(x-X)**2+(y-Y)**2-1|v/p?m=n<m?m:n:f(a,X,Y,n+1,v)))|m


                    Try it online!






                    share|improve this answer














                    JavaScript (ES7),  106 103 102  98 bytes





                    f=(a,x,y,n=m=-1,p)=>a.map((r,Y)=>r.map((v,X)=>(x-X)**2+(y-Y)**2-1|v/p?m=n<m?m:n:f(a,X,Y,n+1,v)))|m


                    Try it online!







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 49 mins ago

























                    answered 1 hour ago









                    Arnauld

                    72.4k689305




                    72.4k689305























                        0















                        Jelly, 25 bytes



                        ZIASỊẠ
                        ŒỤṚŒPḊÇƇœị⁸>Ɲ€ẠƇṪL


                        Try it online! (way to inefficient for the examples - path here is 95 94 93 83 77 40 10 so 6 is yielded)






                        share|improve this answer




























                          0















                          Jelly, 25 bytes



                          ZIASỊẠ
                          ŒỤṚŒPḊÇƇœị⁸>Ɲ€ẠƇṪL


                          Try it online! (way to inefficient for the examples - path here is 95 94 93 83 77 40 10 so 6 is yielded)






                          share|improve this answer


























                            0












                            0








                            0







                            Jelly, 25 bytes



                            ZIASỊẠ
                            ŒỤṚŒPḊÇƇœị⁸>Ɲ€ẠƇṪL


                            Try it online! (way to inefficient for the examples - path here is 95 94 93 83 77 40 10 so 6 is yielded)






                            share|improve this answer















                            Jelly, 25 bytes



                            ZIASỊẠ
                            ŒỤṚŒPḊÇƇœị⁸>Ɲ€ẠƇṪL


                            Try it online! (way to inefficient for the examples - path here is 95 94 93 83 77 40 10 so 6 is yielded)







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 23 mins ago

























                            answered 31 mins ago









                            Jonathan Allan

                            50.7k534165




                            50.7k534165























                                0














                                Python 3, 263 227 bytes



                                def f(m):
                                p={(x,y):[c for i in[-1,1]for c in[(x,y+i),(x+i,y)]]for x,y in m};d={c:0 for c in p if not p[c]}
                                while len(p)-len(d):
                                for c in p:
                                for b in p[c]:
                                if b in d:d[c]=max(d[b]+1,d.get(c,0))
                                return max(d.values())


                                Try it online!



                                -2 bytes thanks to BMO



                                Takes grids in the format {(0, 0): 1, (1, 0): 2, ...}. This format can be generated from the example format using the following utility function:



                                lambda s,e=enumerate:{(x,y):int(n)for y,l in e(s.split('n'))for x,n in e(l.split())}





                                share|improve this answer



















                                • 1




                                  This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                  – BMO
                                  52 mins ago
















                                0














                                Python 3, 263 227 bytes



                                def f(m):
                                p={(x,y):[c for i in[-1,1]for c in[(x,y+i),(x+i,y)]]for x,y in m};d={c:0 for c in p if not p[c]}
                                while len(p)-len(d):
                                for c in p:
                                for b in p[c]:
                                if b in d:d[c]=max(d[b]+1,d.get(c,0))
                                return max(d.values())


                                Try it online!



                                -2 bytes thanks to BMO



                                Takes grids in the format {(0, 0): 1, (1, 0): 2, ...}. This format can be generated from the example format using the following utility function:



                                lambda s,e=enumerate:{(x,y):int(n)for y,l in e(s.split('n'))for x,n in e(l.split())}





                                share|improve this answer



















                                • 1




                                  This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                  – BMO
                                  52 mins ago














                                0












                                0








                                0






                                Python 3, 263 227 bytes



                                def f(m):
                                p={(x,y):[c for i in[-1,1]for c in[(x,y+i),(x+i,y)]]for x,y in m};d={c:0 for c in p if not p[c]}
                                while len(p)-len(d):
                                for c in p:
                                for b in p[c]:
                                if b in d:d[c]=max(d[b]+1,d.get(c,0))
                                return max(d.values())


                                Try it online!



                                -2 bytes thanks to BMO



                                Takes grids in the format {(0, 0): 1, (1, 0): 2, ...}. This format can be generated from the example format using the following utility function:



                                lambda s,e=enumerate:{(x,y):int(n)for y,l in e(s.split('n'))for x,n in e(l.split())}





                                share|improve this answer














                                Python 3, 263 227 bytes



                                def f(m):
                                p={(x,y):[c for i in[-1,1]for c in[(x,y+i),(x+i,y)]]for x,y in m};d={c:0 for c in p if not p[c]}
                                while len(p)-len(d):
                                for c in p:
                                for b in p[c]:
                                if b in d:d[c]=max(d[b]+1,d.get(c,0))
                                return max(d.values())


                                Try it online!



                                -2 bytes thanks to BMO



                                Takes grids in the format {(0, 0): 1, (1, 0): 2, ...}. This format can be generated from the example format using the following utility function:



                                lambda s,e=enumerate:{(x,y):int(n)for y,l in e(s.split('n'))for x,n in e(l.split())}






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 4 mins ago

























                                answered 2 hours ago









                                wizzwizz4

                                1,2071035




                                1,2071035








                                • 1




                                  This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                  – BMO
                                  52 mins ago














                                • 1




                                  This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                  – BMO
                                  52 mins ago








                                1




                                1




                                This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                – BMO
                                52 mins ago




                                This returns $6$ for the last test case.. Also, you can save two bytes by using for i in[-1,1]for a,b in[[x,y+i],[x+i,y]].
                                – BMO
                                52 mins ago


















                                draft saved

                                draft discarded




















































                                If this is an answer to a challenge…




                                • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                  Explanations of your answer make it more interesting to read and are very much encouraged.


                                • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                More generally…




                                • …Please make sure to answer the question and provide sufficient detail.


                                • …Avoid asking for help, clarification or responding to other answers (use comments instead).






                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178326%2flength-of-the-longest-descent%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

                                Understanding the information contained in the Deep Space Network XML data?

                                Ross-on-Wye

                                Eastern Orthodox Church