-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_maze.py
executable file
·38 lines (31 loc) · 965 Bytes
/
19_maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def handle_plus(maze, direction, x, y):
n, e, s, w = maze[y-1][x], maze[y][x+1], maze[y+1][x], maze[y][x-1]
if direction in ("N", "S"):
if e == " ": return x-1, y, "W"
else: return x+1, y, "E"
elif direction in ("E", "W"):
if n == " ": return x, y+1, "S"
else: return x, y-1, "N"
def walk(maze, direction, x, y):
if direction == "N": y -= 1
elif direction == "E": x += 1
elif direction == "S": y += 1
elif direction == "W": x -= 1
return x, y
with open("19_input.txt") as f:
maze = f.readlines()
y = 0
x = maze[y].index("|")
direction = "S"
steps = 0
while maze[y][x] != " ":
steps += 1
x, y = walk(maze, direction, x, y)
if maze[y][x] in ("|", "-"):
continue
elif maze[y][x] == "+":
x, y, direction = handle_plus(maze, direction, x, y)
steps += 1
else:
print(maze[y][x])
print(steps)