-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday15.py
executable file
·206 lines (158 loc) · 3.87 KB
/
day15.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
#!/usr/bin/env python3
from utils.all import *
import sys
advent.setup(2019, 15)
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
NORTH, SOUTH, WEST, EAST = 1, 2, 3, 4
HITWALL, MOVED, FOUNDOXYGEN = 0, 1, 2
from lib.intcode import IntcodeVM
prog = read_ints(fin)
vm = IntcodeVM(prog)
def path_to_moves(path):
for a, b in zip(path, path[1:]):
if b[0] > a[0]:
yield EAST
elif b[0] < a[0]:
yield WEST
elif b[1] > a[1]:
yield NORTH
elif b[1] < a[1]:
yield SOUTH
def walk(path, stop_if_oxygen=False):
vm.reset()
dst = None
for dst, mv in zip(path[1:], path_to_moves(path)):
out = vm.run([mv], resume=1, n_out=1)
if not out:
break
res = out[0]
# log('{} -> {} -> {}\n', mv, dst, 'WMO'[res])
if res == HITWALL:
return res, dst
elif stop_if_oxygen and res == FOUNDOXYGEN:
return res, dst
assert dst is not None
return MOVED, dst
def build_grid(walls, oxygen_position, my_position):
minx = min(x for x, _ in walls)
maxx = max(x for x, _ in walls)
miny = min(y for _, y in walls)
maxy = max(y for _, y in walls)
height = maxy - miny + 1
width = maxx - minx + 1
grid = [[' '] * width for _ in range(height)]
for y in range(height):
for x in range(width):
cell = (minx + x, miny + y)
if cell == oxygen_position:
grid[y][x] = 'x'
elif cell == my_position:
grid[y][x] = '@'
else:
grid[y][x] = '█' if cell in walls else ' '
return grid
# import pickle
# from copy import deepcopy
# eprint('restoring walls')
# bkp_walls = set()
# try:
# with open('walls.pkl', 'rb') as f:
# bkp_walls = pickle.load(f)
# except:
# eprint('failed restore starting new')
# walls = deepcopy(bkp_walls) if bkp_walls else set()
walls = set()
minx, maxx = -50, 50
miny, maxy = -50, 50
tosee = set()
G = nx.Graph()
# eprint('building graph')
for x in range(minx, maxx+1):
for y in range(miny, maxy+1):
xy = (x, y)
if xy not in walls:
G.add_node(xy)
tosee.add(xy)
else:
continue
neigh = (
(x+1, y),
(x-1, y),
(x, y+1),
(x, y-1)
)
for n in neigh:
if n not in walls:
G.add_edge(xy, n)
# log(' {} \r', x)
# eprint('graph done')
src = (0, 0)
tot = len(tosee)
oxygen_found = False
try:
for i, dst in enumerate(tosee, 1):
log('{:04d}/{} check {} \r', i, tot, dst)
if src == dst:
continue
while True:
try:
path = nx.shortest_path(G, src, dst)
except nx.exception.NetworkXNoPath:
break
except nx.exception.NodeNotFound:
break
log('{:04d}/{} going from {} to {}... ', i, tot, src, dst)
res, lastpos = walk(path, stop_if_oxygen=(not oxygen_found))
log('{} {} \r', 'WMO'[res], lastpos)
if res == HITWALL:
G.remove_node(lastpos)
walls.add(lastpos)
elif res == FOUNDOXYGEN:
eprint()
eprint('found oxygen')
oxygen_found = True
oxygen_position = lastpos
oxygen_path = nx.shortest_path(G, src, oxygen_position)
oxygen_distance = len(oxygen_path) - 1
advent.submit_answer(1, oxygen_distance)
break
elif res == MOVED:
break
except KeyboardInterrupt:
eprint('stopping')
pass
eprint()
# if len(walls) > len(bkp_walls):
# log('saving {} walls\n', len(walls))
# with open('walls.pkl', 'wb') as f:
# pickle.dump(walls, f)
# advent.submit_answer(1, ans)
# PART 2
my_position = (0, 0)
grid = build_grid(walls, oxygen_position, my_position)
dump_char_matrix(grid)
G = nx.Graph()
startpos = None
for y in range(len(grid)):
for x, cell in enumerate(grid[y]):
if cell == '█':
continue
xy = (x, y)
if cell == 'x': # oxygen
startpos = xy
neigh = (
(x+1, y),
(x-1, y),
(x, y+1),
(x, y-1)
)
for n in neigh:
x2, y2 = n
if 0 <= x2 < len(grid[0]) and 0 <= y2 < len(grid):
if grid[y2][x2] != '█':
G.add_edge(xy, n)
ans = max(nx.shortest_path_length(G, startpos, node) for node in G)
advent.submit_answer(2, ans)