-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
65 lines (55 loc) · 1.22 KB
/
part1.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
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)
visited = [[set() for _ in range(width)] for _ in range(height)]
stack = [(-1, 0, '>')]
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))
print(sum(
bool(visited[y][x])
for y in range(height)
for x in range(width)
))