-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.py
213 lines (190 loc) · 7.22 KB
/
day20.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from dataclasses import dataclass
from typing import Dict, Tuple
from collections import deque
import string
from utils import neighbours
Pos = Tuple[int, int]
@dataclass
class Maze:
maze: Dict[Pos, str]
pos_to_pos: Dict[Pos, Pos]
start: Pos
goal: Pos
@staticmethod
def from_string(maze_str: str) -> "Maze":
lines = maze_str.splitlines()
pos_to_portal = {}
maze = {}
portal_letters = set(string.ascii_uppercase)
height = len(lines)
for y in range(height):
width = len(lines[y])
for x in range(width):
if lines[y][x] == " ":
continue
if lines[y][x] in ".#":
maze[y, x] = lines[y][x]
continue
if lines[y][x] in portal_letters:
if x + 1 < width and lines[y][x + 1] in portal_letters:
if x - 1 >= 0 and lines[y][x - 1] == ".":
pos_to_portal[y, x - 1] = lines[y][x] + lines[y][x + 1]
elif x + 2 < width and lines[y][x + 2] == ".":
pos_to_portal[y, x + 2] = lines[y][x] + lines[y][x + 1]
elif (
y + 1 < height
and x < len(lines[y + 1])
and lines[y + 1][x] in portal_letters
):
if (
y - 1 >= 0
and x < len(lines[y - 1])
and lines[y - 1][x] == "."
):
pos_to_portal[y - 1, x] = lines[y][x] + lines[y + 1][x]
elif (
y + 2 < height
and x < len(lines[y + 2])
and lines[y + 2][x] == "."
):
pos_to_portal[y + 2, x] = lines[y][x] + lines[y + 1][x]
start = goal = None
pos_to_pos = {}
for pos, portal in pos_to_portal.items():
if portal == "AA":
start = pos
elif portal == "ZZ":
goal = pos
else:
for other_pos, other_portal in pos_to_portal.items():
if pos != other_pos and portal == other_portal:
pos_to_pos[pos] = other_pos
assert start and goal
return Maze(maze, pos_to_pos, start, goal)
def a(maze_str):
maze = Maze.from_string(maze_str)
visited = {maze.start}
queue = deque([(0, maze.start)])
while queue:
steps, pos = queue.popleft()
if pos == maze.goal:
return steps
for neighbour in neighbours(pos):
if neighbour in visited or maze.maze.get(neighbour, "#") == "#":
continue
visited.add(neighbour)
queue.append((steps + 1, neighbour))
if pos in maze.pos_to_pos:
neighbour = maze.pos_to_pos[pos]
if neighbour in visited:
continue
visited.add(neighbour)
queue.append((steps + 1, neighbour))
@dataclass
class RecursiveMaze:
maze: Dict[Pos, str]
pos_to_pos: Dict[Pos, Tuple[Pos, int]]
start: Pos
goal: Pos
@staticmethod
def from_string(maze_str: str) -> "Maze":
lines = maze_str.splitlines()
def is_inner_hor(x):
width = max(map(len, lines))
if 3 < x < width - 3:
return 1
return -1
def is_inner_ver(y):
height = len(lines)
if 3 < y < height - 3:
return 1
return -1
pos_to_portal = {}
maze = {}
portal_letters = set(string.ascii_uppercase)
height = len(lines)
for y in range(height):
width = len(lines[y])
for x in range(width):
if lines[y][x] == " ":
continue
if lines[y][x] in ".#":
maze[y, x] = lines[y][x]
continue
if lines[y][x] in portal_letters:
if x + 1 < width and lines[y][x + 1] in portal_letters:
if x - 1 >= 0 and lines[y][x - 1] == ".":
pos_to_portal[y, x - 1] = (
lines[y][x] + lines[y][x + 1],
is_inner_hor(x - 1),
)
elif x + 2 < width and lines[y][x + 2] == ".":
pos_to_portal[y, x + 2] = (
lines[y][x] + lines[y][x + 1],
is_inner_hor(x + 2),
)
elif (
y + 1 < height
and x < len(lines[y + 1])
and lines[y + 1][x] in portal_letters
):
if (
y - 1 >= 0
and x < len(lines[y - 1])
and lines[y - 1][x] == "."
):
pos_to_portal[y - 1, x] = (
lines[y][x] + lines[y + 1][x],
is_inner_ver(y - 1),
)
elif (
y + 2 < height
and x < len(lines[y + 2])
and lines[y + 2][x] == "."
):
pos_to_portal[y + 2, x] = (
lines[y][x] + lines[y + 1][x],
is_inner_ver(y + 2),
)
start = goal = None
pos_to_pos = {}
for pos, (portal, level) in pos_to_portal.items():
if portal == "AA":
start = pos
elif portal == "ZZ":
goal = pos
else:
for other_pos, (other_portal, _) in pos_to_portal.items():
if pos != other_pos and portal == other_portal:
pos_to_pos[pos] = other_pos, level
assert start and goal
return RecursiveMaze(maze, pos_to_pos, start, goal)
def b(maze_str):
maze = RecursiveMaze.from_string(maze_str)
start = (maze.start, 0)
visited = {start}
queue = deque([(0, start)])
while queue:
steps, (pos, level) = queue.popleft()
if pos == maze.goal and level == 0:
return steps
for neighbour in neighbours(pos):
new_state = (neighbour, level)
if new_state in visited or maze.maze.get(neighbour, "#") == "#":
continue
visited.add(new_state)
queue.append((steps + 1, new_state))
if pos in maze.pos_to_pos:
neighbour, level_incr = maze.pos_to_pos[pos]
if level == 0 and level_incr == -1:
continue
new_state = (neighbour, level + level_incr)
if new_state in visited:
continue
visited.add(new_state)
queue.append((steps + 1, new_state))
def main():
maze_str = open("input20.txt").read()
print(b(maze_str))
if __name__ == "__main__":
main()