-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day16_2.py
executable file
·86 lines (70 loc) · 2.65 KB
/
aoc23_day16_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
from collections import deque
def read_diagram():
res = []
while True:
try:
res.append(input().strip())
except EOFError:
break
return res
def is_valid(diagram, i, j):
return i >= 0 and i < len(diagram) and j >= 0 and j < len(diagram[0])
def solve1(diagram, start_pos, start_step):
start = (start_pos, start_step)
frontier = deque([start])
explored = set()
energised = [[0] * len(diagram[0]) for _ in range(len(diagram))]
while frontier:
current = frontier.pop()
pos, step = current
energised[pos[0]][pos[1]] = 1
action = diagram[pos[0]][pos[1]]
no_change = True
if action == '\\' \
or action == '|' and step[0] == 0 \
or action == '-' and step[1] == 0:
no_change = False
next_step = (step[1], step[0])
next_pos = (pos[0] + next_step[0], pos[1] + next_step[1])
if is_valid(diagram, next_pos[0], next_pos[1]) \
and (next_pos, next_step) not in explored:
frontier.appendleft((next_pos, next_step))
explored.add((next_pos, next_step))
if action == '/' \
or action == '|' and step[0] == 0 \
or action == '-' and step[1] == 0:
no_change = False
next_step = (-step[1], -step[0])
next_pos = (pos[0] + next_step[0], pos[1] + next_step[1])
if is_valid(diagram, next_pos[0], next_pos[1]) \
and (next_pos, next_step) not in explored:
frontier.appendleft((next_pos, next_step))
explored.add((next_pos, next_step))
if no_change:
next_step = (step[0], step[1])
next_pos = (pos[0] + next_step[0], pos[1] + next_step[1])
if is_valid(diagram, next_pos[0], next_pos[1]) \
and (next_pos, next_step) not in explored:
frontier.appendleft((next_pos, next_step))
explored.add((next_pos, next_step))
count = 0
for item in energised:
count += sum(item)
return count
def main():
diagram = read_diagram()
res = 0
for i in range(len(diagram)):
count = solve1(diagram, (i, 0), (0, 1))
res = max(res, count)
count = solve1(diagram, (i, len(diagram[0]) - 1), (0, -1))
res = max(res, count)
for i in range(len(diagram[0])):
count = solve1(diagram, (0, i), (1, 0))
res = max(res, count)
count = solve1(diagram, (len(diagram) - 1, i), (-1, 0))
res = max(res, count)
print(res)
if __name__ == '__main__':
main()