Skip to content

Instantly share code, notes, and snippets.

@fakedrake
Created October 29, 2019 17:37
Show Gist options
  • Save fakedrake/44bd0c6045534c29ffdda897f90c4df8 to your computer and use it in GitHub Desktop.
Save fakedrake/44bd0c6045534c29ffdda897f90c4df8 to your computer and use it in GitHub Desktop.
a = [[1,2,1,1],
[1,2,1,1],
[1,2,1,1]]
STEPS = [
lambda (x,y): (x+1,y), # STEPS[0]
lambda (x,y): (x,y+1), # STEPS[1]
lambda (x,y): (x-1,y), # STEPS[2]
lambda (x,y): (x,y-1), # STEPS[3]
]
def next_step(cur,grid):
cur_x, cur_y = cur
which_change_step = grid[cur_y][cur_x] -1
change_step = STEPS[which_change_step]
return change_step(cur)
def in_grid(cur,grid):
x,y = cur
height = len(grid)
if y >= height or y < 0:
return False
row = grid[y]
width = len(row)
if x >= len(row) or x < 0:
return False
return True
def labyrinth(start, end, grid):
start_i, start_j = start
end_i, end_j = end
cur = start
follow = start
should_advance_follow = False
while True:
if cur == end:
return True
if cur is follow:
return False
if not in_grid(cur, grid):
return False
cur = next_step(cur, grid)
if should_advance_follow:
follow = next_step(follow, grid)
shoould_advance_follow = not should_advance_follow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment