Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created March 28, 2020 21:08
Show Gist options
  • Save ahmed4end/2547fa258c60a77249c687bdfa40cbe9 to your computer and use it in GitHub Desktop.
Save ahmed4end/2547fa258c60a77249c687bdfa40cbe9 to your computer and use it in GitHub Desktop.
origin ="""
S0110
01000
01010
00010
0001E
"""
grid = [[j for j in i] for i in origin.split("\n")][1:-1]
look = lambda p, grid=grid: list(filter(lambda x:grid[x[0]][x[1]]!="1", filter(lambda x: len(grid)>x[0]>=0 and len(grid[0])>x[1]>=0 , map(lambda x: (p[0]+x[0], p[1]+x[1]), [(0,1),(1,0),(0,-1),(-1,0)]))))
def walkpath(grid=grid, s=(0,0), e=(4,4)):
def walk(grid, s, e, trac=[(0,0)], res=[]):
if s==e:
trac =trac[:]
res.append(trac)
look_ahead = look(s)
for i in set(look_ahead)-set(trac):
trac.append(i)
walk(grid, i, e)
trac.remove(i)
return res
return min(walk(grid, s, e), key=len)
for i in walkpath((0,0)):
grid[i[0]][i[1]] = "#"
print(*["".join(i)for i in grid], sep="\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment