-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-2.py
64 lines (53 loc) · 1.61 KB
/
16-2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from collections import deque
UP = (-1, 0)
DOWN = (1, 0)
LEFT = (0, -1)
RIGHT = (0, 1)
DIRECTIONS = {
"|": {LEFT: (UP, DOWN), RIGHT: (UP, DOWN)},
"-": {UP: (LEFT, RIGHT), DOWN: (LEFT, RIGHT)},
"/": {UP: (RIGHT,), DOWN: (LEFT,), LEFT: (DOWN,), RIGHT: (UP,)},
"\\": {UP: (LEFT,), DOWN: (RIGHT,), LEFT: (UP,), RIGHT: (DOWN,)},
".": {},
}
def get_energized_tiles(row, column, dr, dc):
queue = deque([(row, column, dr, dc)])
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_direction in DIRECTIONS[lines[new_row][new_column]].get(
(dr, dc), ((dr, dc),)
):
if (new_row, new_column, *new_direction) not in seen:
queue.append((new_row, new_column, *new_direction))
seen.add((new_row, new_column, *new_direction))
return len({(row, column) for row, column, *_ in seen})
with open("input.txt") as f:
lines = f.read().splitlines()
print(
max(
[
max(
get_energized_tiles(i, -1, *RIGHT),
get_energized_tiles(i, len(lines[0]), *LEFT),
)
for i in range(len(lines))
]
+ [
max(
get_energized_tiles(-1, i, *DOWN),
get_energized_tiles(len(lines), i, *UP),
)
for i in range(len(lines[0]))
]
)
)