-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-1.py
42 lines (34 loc) · 1.06 KB
/
16-1.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
39
40
41
42
from collections import deque
UP = (-1, 0)
DOWN = (1, 0)
LEFT = (0, -1)
RIGHT = (0, 1)
DIRECTIONS = {
"|": {LEFT: (DOWN, UP), RIGHT: (DOWN, UP)},
"-": {UP: (LEFT, RIGHT), DOWN: (LEFT, RIGHT)},
"/": {UP: (RIGHT,), DOWN: (LEFT,), LEFT: (DOWN,), RIGHT: (UP,)},
"\\": {UP: (LEFT,), DOWN: (RIGHT,), LEFT: (UP,), RIGHT: (DOWN,)},
".": {},
}
with open("input.txt") as f:
lines = f.read().splitlines()
queue = deque([(0, -1, *RIGHT)])
seen = set()
while queue:
row, column, dr, dc = queue.popleft()
new_row = row + dr
new_column = column + dc
if (
new_row < 0
or new_row >= len(lines)
or new_column < 0
or new_column >= len(lines[0])
):
continue
for new_directions in DIRECTIONS[lines[new_row][new_column]].get(
(dr, dc), ((dr, dc),)
):
if (new_row, new_column, *new_directions) not in seen:
queue.append((new_row, new_column, *new_directions))
seen.add((new_row, new_column, *new_directions))
print(len({(row, column) for row, column, *_ in seen}))