Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created November 19, 2018 15:57
Show Gist options
  • Save jrjames83/a539803da969c66591b8a4e82b59261c to your computer and use it in GitHub Desktop.
Save jrjames83/a539803da969c66591b8a4e82b59261c to your computer and use it in GitHub Desktop.
maze navigation top left to bottom right, gather valid paths
# - + + + < | > > * < < < v - - - - - seems feasable
# exact path is still ambiguous......was the move actually up or down, etc...
maze =[
["-", "+", "+", "+", "<"],
[">", ">", "|", "<", "<"],
[">", ">", ">", ">", "*"],
["v", "<", "<", "<", "<"],
["-", "-", "-", "-", "-"]
]
symbol_moves = {
"*": [(-1,0), (1,0), (0,-1), (0,1), (-1,-1),(1,-1),(-1,1),(1,1)], # all directions
"+": [(-1,0), (1,0), (0,-1), (0,1)], #up/down/left/right only
"x": [(-1,-1),(1,-1),(-1,1),(1,1)], # upleft/downleft/upright/downright only
"|": [(-1,0),(1,0) ], # up and down
"-": [(0,-1), (0,1)], #left and right
"^": [(-1,0)], #up
"v" : [(1,0)], #down
"<": [(0,-1)], #left
">": [(0,1)] #right
}
# How to even think about testing this methodoically? Combos of symbols and positions?
assert(len(set(symbol_moves['*'])) == 8) # didn't miss any valid moves
assert(list(generate_valid_moves('-', 0, 0)) == [(0, 1)])
def generate_valid_moves(symbol, row, col):
for delta_row, delta_col in symbol_moves[symbol]:
next_row, next_col = row + delta_row, col + delta_col
if next_row >=0 and next_col >=0:
if next_row < len(maze) and next_col < len(maze[0]):
yield next_row, next_col
def recurse_path(_maze, _row, _col, current_path, used_positions):
exit_paths = []
if _row == 4 and _col == 4:
exit_paths.append(current_path)
else:
for next_row, next_col in generate_valid_moves(_maze[_row][_col], _row, _col):
if (next_row, next_col) not in used_positions:
used_positions.add((next_row, next_col))
exit_paths.extend(recurse_path(_maze, next_row, next_col,
current_path + _maze[next_row][next_col], used_positions))
used_positions.remove((next_row, next_col))
return exit_paths
found_paths = []
start_symbol = maze[0][0]
found_paths.extend(recurse_path(maze, 0, 0, start_symbol, set( [(0, 0)] )))
print(found_paths)
# ['-+>|>>*<<<<v-----',
# '-+>|>>*<<<v-----',
# '-++|>>*<<<<v-----',
# '-++|>>*<<<v-----',
# '-+++<|>>*<<<<v-----',
# '-+++<|>>*<<<v-----']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment