-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.py
77 lines (64 loc) · 1.72 KB
/
part2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
BOUNCES = {
'/': {
'>': '^',
'<': 'v',
'^': '>',
'v': '<'
},
'\\': {
'>': 'v',
'<': '^',
'^': '<',
'v': '>'
},
'|': {
'>': 'v^',
'<': 'v^',
'^': '^',
'v': 'v'
},
'-': {
'>': '>',
'<': '<',
'^': '<>',
'v': '<>'
}
}
with open('input.txt') as file:
layout = [list(line) for line in filter(None, map(str.strip, file))]
width, height = len(layout[0]), len(layout)
starting_points = sum((
[(x, -1, 'v') for x in range(width)],
[(x, 0, '^') for x in range(width)],
[(-1, y, '>') for y in range(height)],
[(0, y, '<') for y in range(height)] # 0 insted of height/width also works because of negative indexing
), [])
max_energized = 0
for start_x, start_y, direction in starting_points:
visited = [[set() for _ in range(width)] for _ in range(height)]
stack = [(start_x, start_y, direction)]
while stack:
x, y, direction = stack.pop()
match direction:
case '<':
x -= 1
case '>':
x += 1
case '^':
y -= 1
case 'v':
y += 1
if not 0 <= x < width or not 0 <= y < height or direction in visited[y][x]:
continue
visited[y][x].add(direction)
if layout[y][x] == '.':
stack.append((x, y, direction))
else:
for new_direction in BOUNCES[layout[y][x]][direction]:
stack.append((x, y, new_direction))
max_energized = max(max_energized, sum(
bool(visited[y][x])
for y in range(height)
for x in range(width)
))
print(max_energized)