Skip to main content
added 59 characters in body
Source Link
wizzwizz4
  • 2.4k
  • 16
  • 42

Python 3, 263263 227 bytes

def f(m):
 p={(x,y):[(a,b)[c for a,bi in[[x,y+1],[x,y-1],[x+1,y],[xin[-1,y]]if1]for c in[(ax,by+i)in m and m[a,b]<m[x(x+i,y]]fory)]]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())}

Python 3, 263 bytes

def f(m):
 p={(x,y):[(a,b)for a,b in[[x,y+1],[x,y-1],[x+1,y],[x-1,y]]if(a,b)in m and m[a,b]<m[x,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!

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())}

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())}
Source Link
wizzwizz4
  • 2.4k
  • 16
  • 42

Python 3, 263 bytes

def f(m):
 p={(x,y):[(a,b)for a,b in[[x,y+1],[x,y-1],[x+1,y],[x-1,y]]if(a,b)in m and m[a,b]<m[x,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!

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())}