8
\$\begingroup\$

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.

They might be all the same number.

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. (not 19, since the path sequence must be strictly descending)

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

10 12 13 14 15 15
17 14 15 15 15 16
18 20 21 15 15 15
21 14 10 11 11 15
15 15 15 15 15 15

The longest path here is of length 6. (21, 20, 18, 17, 14, 12, 10)

...And even bigger numbers are fine too.

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, is nonempty, 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
\$\endgroup\$
  • \$\begingroup\$ How should the last example be interpreted? \$\endgroup\$ – Peter Taylor Jan 3 at 18:11
  • \$\begingroup\$ @PeterTaylor I'm not sure what you mean. \$\endgroup\$ – Beefster Jan 3 at 19:00
  • \$\begingroup\$ I think the last example is just a matrix of multi digit numbers \$\endgroup\$ – Embodiment of Ignorance Jan 3 at 19:08
  • \$\begingroup\$ @EmbodimentofIgnorance, ah, yes, I see. It would be a lot easier to spot the path with two-digit numbers rather than 4 to 6. \$\endgroup\$ – Peter Taylor Jan 3 at 21:08
  • 1
    \$\begingroup\$ @Οurous: just rectangular. Not jagged. \$\endgroup\$ – Beefster Jan 3 at 21:21
8
\$\begingroup\$

JavaScript (ES7),  106 103 102  98 bytes

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

Try it online!

Commented

f = (                        // f = recursive function taking:
  m,                         //   m[]  = input matrix
  n = b = -1,                //   n    = length of the current path; b = best length so far
  x, y,                      //   x, y = coordinates of the previous cell
  p                          //   p    = value of the previous cell
) =>                         //
  m.map((r, Y) =>            // for each row r[] at position Y in m[]:
    r.map((v, X) =>          //   for each value v at position X in r[]:
      (x - X) ** 2 +         //     compute the squared Euclidean distance
      (y - Y) ** 2           //     between (x, y) and (X, Y)
      - 1                    //     if A) the above result is not equal to 1
      | v / p ?              //     or B) v is greater than or equal to p:
        b = n < b ? b : n    //       end of path: update b to n if n >= b
      :                      //     else:
        f(m, n + 1, X, Y, v) //       do a recursive call
    )                        //   end of inner map()
  ) | b                      // end of outer map(); return b

How?

During the first iteration, \$x\$, \$y\$ and \$p\$ are all undefined and both tests (A and B in the comments) evaluate to NaN, which triggers the recursive call. Therefore, all cells are considered as a possible starting point of the path.

\$\endgroup\$
6
\$\begingroup\$

Jelly,  23 21  20 bytes

-2 thanks to Erik the Outgolfer

ŒỤŒPạƝ§ỊẠƲƇœị⁸QƑƇṪL’

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

How?

ŒỤŒPạƝ§ỊẠƲƇœị⁸QƑƇṪL’ - Link: list of lists of integers, M
ŒỤ                   - multi-dimensional indices sorted by values
  ŒP                 - power-set
          Ƈ          - filter, keep those for which:
         Ʋ           -   last four links as a monad:
     Ɲ               -     for each pair of neighbours:
    ạ                -       absolute difference
      §              -     sum each
       Ị             -     insignificant?
        Ạ            -     all?
           œị        - multi-dimensional index into:
             ⁸       -   chain's left argument, M
                Ƈ    - filter, keep only those:
               Ƒ     -   unaffected by?:
              Q      -     de-duplicate
                 Ṫ   - tail
                  L  - length
                   ’ - decrement
\$\endgroup\$
3
\$\begingroup\$

Python 2, 150 147 140 136 134 132 125 123 120 bytes

l=lambda g,i,j:max(0<g.get(t)<g[i,j]and-~l(g,*t)for d in(-1,1)for t in((i+d,j),(i,j+d)))
lambda g:max(l(g,*t)for t in g)

Try it online!

Takes input in the form of a dictionary (x, y): value.

-7 bytes thanks to wizzwizz4, -2 bytes thanks to Jonathan Allen, -2 bytes thanks to BMO

Alternative, 123 121 bytes

l=lambda i,j:max(0<g.get(t)<g[i,j]and-~l(*t)for d in(-1,1)for t in((i+d,j),(i,j+d)))
g=input();print max(l(*t)for t in g)

Try it online!

Essentially the same solution, just with the final lambda replaced by an actual program. I personally like the first one better, but this one comes close in byte count by allowing g to be used as a global variable.

\$\endgroup\$
2
\$\begingroup\$

Clean, 211 207 bytes

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

Indented:

$ l
    = maximum
        [ length k-1
        \\p <- permutations
            [ (v, [x, y])
            \\y <- [0..] & u <- l
            , x <- [0..] & v <- u
            ]
        , (k, [m: n]) <- map unzip
            (subsequences p)
        | and
            [ all
                ((>) 2 o sum o map abs)
                (zipWith (zipWith (-)) n [m:n])
                :
                zipWith (>) k (tl k)
            ]
        ]
\$\endgroup\$
2
\$\begingroup\$

Python 3, 219 bytes

e,m=len,enumerate
b=lambda g,x,y:[b(g,i,j)for o in[-1,1]for i,j in[(x+o,y),(x,y+o)]if e(g)>i>=0<=j<e(g[x])and g[x][y]<g[i][j]]
l=lambda t:e(t)and 1+max(map(l,t))
d=lambda g:max(l(b(g,x,y))for x,r in m(g)for y,_ in m(r))

Try it online!

Grid is represented as list of lists:

[
    [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],
]

Original ungolfed code:

def potential_neighbours(x, y):
    return [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]

def neighbours(grid, x, y):
    result = []
    for i, j in potential_neighbours(x, y):
        if 0 <= i < len(grid) and 0 <= j < len(grid[x]) and grid[x][y] < grid[i][j]:
            result += [(i, j)]
    return result

def build_tree(grid, x, y):
    return [build_tree(grid, i, j) for i, j in neighbours(grid, x, y)]

def longest_path_in_tree(tree):
    if len(tree) == 0:
        return 0
    return 1 + max(map(longest_path_in_tree, tree))

def longest_descent(grid):
    trees = [build_tree(grid, x, y) for x, row in enumerate(grid) for y, _ in enumerate(row)]
    return max(map(longest_path_in_tree, trees))
\$\endgroup\$
2
\$\begingroup\$

Haskell, 188 186 bytes

Needs \$\texttt{-XNoMonomorphismRestriction}\$:

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

Try it online!

Alternatively we could use notElem over (not.).(#) without the flag for \$+4\$ 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
  | c <- [0..length(m!!0)-1]             -- all possible indices of xs' columns
  , r <- [0..length m-1]                 -- all possible indices of xs' rows
  = safeMaximum                          -- maximize ..
      [ g [(x,y)]                        -- .. initially we haven't visited any others
      | x <- c, y<-r                     -- .. all possible entries
-- For the purpose of golfing we define g in the list-comprehension, it takes all visited entries (p) where (x,y) is the most recent
      , let g((x,y):p) = safeMaximum     -- maximize ..
          [ 1 + g(k: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]
          , k@(u,v) <-[(x+i,y),(x,y+i)]  -- next entry-candidate
          , u#c, 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
          ]
      ]
\$\endgroup\$
  • \$\begingroup\$ How does this take grids? List of lists? \$\endgroup\$ – Beefster Jan 4 at 17:42
  • \$\begingroup\$ @Beefster: Yes, it says [[Integer]] is list of lists. Though in the linked TIO you have a wrapper parse :: String -> [[Integer]], st. you can use strings split on spaces and new-lines. \$\endgroup\$ – ბიმო Jan 4 at 17:46
1
\$\begingroup\$

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())}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.