-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday20.py
executable file
·295 lines (213 loc) · 5.75 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
from utils.all import *
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
@lru_cache(2**20)
def key_from_pos(grid, r, c):
if grid[r][c] != '.':
return None
valid = False
for nr, nc in neighbors4(grid, r, c):
v = grid[nr][nc]
if 'A' <= v <= 'Z':
valid = True
break
if not valid:
return None
valid = False
for nnr, nnc in neighbors4(grid, nr, nc):
vv = grid[nnr][nnc]
if 'A' <= vv <= 'Z':
valid = True
break
assert valid
if nnr > nr or nnc > nc:
key = v + vv
else:
key = vv + v
return key + '(' + str(r) + ',' + str(c) +')'
def neighbors4(grid, r, c):
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
rr, cc = (r + dr, c + dc)
if 0 <= rr < len(grid) and 0 <= cc < len(grid[rr]):
if grid[rr][cc] not in '# ':
yield (rr, cc)
def dijkstra(G, src, dst):
distance = defaultdict(lambda: float('inf'))
queue = []
visited = set()
queue.append((0, src, [src]))
distance[src] = 0
while queue:
dist, node, path = heapq.heappop(queue)
if node not in visited:
if node == dst:
return dist, path
visited.add(node)
for neighbor in filter(lambda p: p not in visited, G[node]):
new_dist = dist + G[node][neighbor]
if new_dist < distance[neighbor]:
distance[neighbor] = new_dist
heapq.heappush(queue, (new_dist, neighbor, path + [neighbor]))
return float('inf'), None
def find_adjacent(grid, src, src_key):
visited = {src}
queue = deque()
found = {}
for n in neighbors4(grid, *src):
queue.append((1, n))
while queue:
dist, node = queue.popleft()
if node not in visited:
visited.add(node)
key = key_from_pos(grid, *node)
if key is not None and key != src_key and (key not in found or found[key] > dist):
found[key] = dist
continue
for neighbor in filter(lambda n: n not in visited, neighbors4(grid, *node)):
queue.append((dist + 1, neighbor))
return found
def build_graph(grid):
graph = {}
for r, row in enumerate(grid):
for c in range(len(row)):
key = key_from_pos(grid, r, c)
if key is not None:
graph[key] = find_adjacent(grid, (r, c), key)
return graph
grid = tuple(l.strip('\n') for l in fin)
# print(find_adjacent(grid, (8, 0)))
G = build_graph(grid)
# dump_dict(G)
for k in G:
if k.startswith('AA'):
start = k
if k.startswith('ZZ'):
end = k
# eprint(start, end)
for k1 in G:
for k2 in G:
if k1 != k2:
if k1[:2] == k2[:2]:
G[k1][k2] = 1
G[k2][k1] = 1
ans, path = dijkstra(G, start, end)
# eprint(ans)
# dump_iterable(path)
advent.submit_answer(1, ans)
################################################################################
# part 2... don't you love duplicated code?
################################################################################
@lru_cache(2**20)
def recursive_neighbors(src):
depth = int(src[3:])
ksrc = src[:3] + '0'
res = []
depth0_neighbors = G[ksrc]
if src[2] == 'i':
res.append((src[:2] + 'o' + str(depth + 1), 1))
if depth == 0:
for k, d in depth0_neighbors:
if k[2] == 'i' or k == ENTRANCE or k == EXIT:
res.append((k, d))
else:
sdepth = str(depth)
if src[2] == 'o':
res.append((src[:2] + 'i' + str(depth - 1), 1))
for k, d in depth0_neighbors:
if k != ENTRANCE and k != EXIT:
res.append((k[:3] + sdepth, d))
# if src == 'CKo1':
# print(ksrc, depth0_neighbors)
# print(src, res)
return tuple(res)
@lru_cache(2**20)
def key_from_pos2(grid, r, c):
if grid[r][c] != '.':
return None
valid = False
for nr, nc in neighbors4(grid, r, c):
v = grid[nr][nc]
if 'A' <= v <= 'Z':
valid = True
break
if not valid:
return None
valid = False
for nnr, nnc in neighbors4(grid, nr, nc):
vv = grid[nnr][nnc]
if 'A' <= vv <= 'Z':
valid = True
break
assert valid
if nnr > nr or nnc > nc:
key = v + vv
else:
key = vv + v
if nnr == 0 or nnc == 0 or nnr == MAXROW or nnc == MAXCOLUMN or nr == 0 or nc == 0 or nr == MAXROW or nc == MAXCOLUMN:
return key + 'o0'
return key + 'i0'
def dijkstra2(G, src, dst):
distance = defaultdict(lambda: float('inf'))
queue = [(0, src, [src])]
# queue = [(0, src)]
visited = set()
distance[src] = 0
while queue:
dist, node, path = heapq.heappop(queue)
# dist, node = heapq.heappop(queue)
if node not in visited:
if node == dst:
return dist, path
# return dist
visited.add(node)
for neighbor, weight in filter(lambda p: p[0] not in visited, recursive_neighbors(node)):
new_dist = dist + weight
if new_dist < distance[neighbor]:
distance[neighbor] = new_dist
heapq.heappush(queue, (new_dist, neighbor, path + [neighbor]))
# heapq.heappush(queue, (new_dist, neighbor))
return float('inf'), None
# return float('inf')
def find_adjacent2(grid, src, src_key):
visited = {src}
queue = deque()
found = []
for n in neighbors4(grid, *src):
queue.append((1, n))
while queue:
dist, node = queue.popleft()
if node not in visited:
visited.add(node)
key = key_from_pos2(grid, *node)
if key is not None and key != src_key and key not in found:
found.append((key, dist))
continue
for neighbor in filter(lambda n: n not in visited, neighbors4(grid, *node)):
queue.append((dist + 1, neighbor))
return found
def build_graph2(grid):
graph = {}
for r, row in enumerate(grid):
for c in range(len(row)):
key = key_from_pos2(grid, r, c)
if key is not None:
graph[key] = find_adjacent2(grid, (r, c), key)
return graph
ROWS = len(grid)
COLUMNS = len(grid[0])
MAXROW, MAXCOLUMN = ROWS-1, COLUMNS-1
G = build_graph2(grid)
# dump_dict(G)
for k in G:
if k.startswith('AA'):
ENTRANCE = k
if k.startswith('ZZ'):
EXIT = k
ans, path = dijkstra2(G, ENTRANCE, EXIT)
# eprint(ans)
# dump_iterable(path)
# 1007 wrong
advent.submit_answer(2, ans)