-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.py
365 lines (336 loc) · 12.8 KB
/
day16.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Day 16: I don't know where I'm a gonna go when the volcano blow"""
from dataclasses import dataclass
from pathlib import Path
from copy import deepcopy
from collections import deque
from typing import Self
import networkx
TURNS = 30
TEST_INPUT = """Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II""".splitlines()
START = "AA"
START_FLOW = 0
Valve = tuple[bool, int, list[str]]
def parse_input(puzzle: list[str]) -> tuple[dict[str, Valve], networkx.Graph]:
graph = networkx.Graph()
valves: dict[str, Valve] = {}
for line in puzzle:
words = line.split()
name = words[1]
flow_rate = int(words[4].split("=")[1][:-1])
valve_neighbors = [word.split(",")[0] for word in words[9:]]
for neighbor in valve_neighbors:
graph.add_edge(name, neighbor)
# HACK: if there's no flow, go ahead and mark it as open
valves[name] = (flow_rate == 0, flow_rate, valve_neighbors)
return valves, graph
def max_theoretical_flow(valves: dict[str, Valve], turns_remaining: int) -> int:
"""What's the best possible remaining flow we could get by turning things on
regardless of distance"""
value = 0
for index, (state, rate, _) in enumerate(
sorted([v for v in valves.values() if not v[0]], key=lambda k: -k[1]), start=1
):
assert not state
value += max((turns_remaining - index) * rate, 0)
return value
def part_one(puzzle: list[str]) -> int:
valves, graph = parse_input(puzzle)
distances: dict[str, dict[str, int]] = {}
valves_to_open = [
name
for name, (valve_open, rate, _) in valves.items()
if rate and not valve_open
]
for valve in valves:
distances[valve] = {}
for target in valves_to_open:
if target == valve:
continue
distances[valve][target] = networkx.shortest_path_length(
graph, valve, target
)
queue: deque[tuple[int, int, str, dict[str, Valve]]] = deque(
[(0, TURNS, "AA", deepcopy(valves))]
)
# queue: total flow, turns remaining, current position, valves
best_flow = 0
while True:
try:
(
total_flow,
turns_remaining,
current_position,
current_valves,
) = queue.pop()
except IndexError:
return best_flow
open_valves = []
total_flow_this_turn = 0
for name, (flow_open, flow_rate, _) in current_valves.items():
if flow_open and flow_rate:
open_valves.append(name)
total_flow_this_turn += flow_rate
if turns_remaining <= 0:
best_flow = max(best_flow, total_flow)
# end state
continue
if all(valve[0] for valve in current_valves.values()):
# do nothing
if total_flow > best_flow:
best_flow = max(best_flow, total_flow)
continue
if (
total_flow
+ (
limit := max_theoretical_flow(
current_valves, turns_remaining=turns_remaining
)
)
< best_flow
):
# bail out if there's no way we could make it work
continue
# try turning on the valve
current_valve = current_valves[current_position]
valve_open, flow_rate, neighbors = current_valve
if not valve_open:
new_valves: dict[str, Valve] = {**current_valves}
new_valves[current_position] = (True, flow_rate, neighbors)
current_flow = flow_rate * (turns_remaining - 1)
queue.append(
(
total_flow + current_flow,
turns_remaining - 1,
current_position,
new_valves,
)
)
continue
# valve is already open (or opening wouldn't do any good)
# Now let's hop to each closed valve and start another round
for target, distance in distances[current_position].items():
if not current_valves[target][0]:
# move to that valve
queue.append(
(total_flow, turns_remaining - distance, target, current_valves)
)
def open_or_move(
valves: dict[str, Valve],
turns_remaining: int,
current_position: str,
distances: dict[str, dict[str, int]],
) -> list[tuple[dict[str, Valve], str, int, int]]:
"""Take an action, either turning on a valve or moving
Returns a list of:
- updated valve dict
- new position of the actor
- flow generated by the action
- new turns_remaining
"""
flow_open, flow_rate, neighbors = valves[current_position]
if not flow_open:
new_valves: dict[str, Valve] = {**valves}
new_valves[current_position] = (True, flow_rate, neighbors)
current_flow = flow_rate * (turns_remaining - 1)
return [(new_valves, current_position, current_flow, turns_remaining - 1)]
result = []
for target, distance in distances[current_position].items():
if not valves[target][0]:
# move to that valve
result.append((valves, target, 0, turns_remaining - distance))
return result
@dataclass
class State:
valves: dict[str, Valve]
distances: dict[str, dict[str, int]]
human_position: str
elephant_position: str
turns_remaining: int
human_next_turn: int
elephant_next_turn: int
total_flow: int
def take_action(self) -> list[Self]:
new_states = []
if self.turns_remaining == self.human_next_turn:
# take the human action
for (
new_valves,
new_human_position,
new_human_flow,
new_human_turns_remaining,
) in open_or_move(
self.valves,
turns_remaining=self.turns_remaining,
current_position=self.human_position,
distances=self.distances,
):
if self.turns_remaining == self.elephant_next_turn:
for (
newer_valves,
new_elephant_position,
new_elephant_flow,
new_elephant_turns_remaining,
) in open_or_move(
valves=new_valves,
turns_remaining=self.elephant_next_turn,
current_position=self.elephant_position,
distances=self.distances,
):
new_states.append(
State(
valves=newer_valves,
distances=self.distances,
human_next_turn=new_human_turns_remaining,
elephant_next_turn=new_elephant_turns_remaining,
human_position=new_human_position,
turns_remaining=self.turns_remaining - 1,
elephant_position=new_elephant_position,
total_flow=self.total_flow
+ new_human_flow
+ new_elephant_flow,
)
)
else:
# elephant can't move
new_states.append(
State(
valves=new_valves,
distances=self.distances,
human_next_turn=new_human_turns_remaining,
elephant_next_turn=self.elephant_next_turn,
human_position=new_human_position,
elephant_position=self.elephant_position,
total_flow=self.total_flow + new_human_flow,
turns_remaining=self.turns_remaining - 1,
)
)
elif self.elephant_next_turn == self.turns_remaining:
# human can't move, but elephant can
for (
newer_valves,
new_elephant_position,
new_elephant_flow,
new_elephant_turns_remaining,
) in open_or_move(
valves=self.valves,
turns_remaining=self.elephant_next_turn,
current_position=self.elephant_position,
distances=self.distances,
):
new_states.append(
State(
valves=newer_valves,
distances=self.distances,
human_next_turn=self.human_next_turn,
elephant_next_turn=new_elephant_turns_remaining,
human_position=self.human_position,
turns_remaining=self.turns_remaining - 1,
elephant_position=new_elephant_position,
total_flow=self.total_flow + new_elephant_flow,
)
)
else:
# neither can move
assert self.human_next_turn < self.turns_remaining
assert self.elephant_next_turn < self.turns_remaining
# count down until the next time either can move
new_states.append(
State(
valves=self.valves,
distances=self.distances,
human_next_turn=self.human_next_turn,
elephant_next_turn=self.elephant_next_turn,
human_position=self.human_position,
elephant_position=self.elephant_position,
turns_remaining=max(self.human_next_turn, self.elephant_next_turn),
total_flow=self.total_flow,
)
)
return new_states
def part_two(puzzle: list[str]) -> int:
valves, graph = parse_input(puzzle)
distances: dict[str, dict[str, int]] = {}
valves_to_open = [
name
for name, (valve_open, rate, _) in valves.items()
if rate and not valve_open
]
for valve in valves:
distances[valve] = {}
for target in valves_to_open:
if target == valve:
continue
distances[valve][target] = networkx.shortest_path_length(
graph, valve, target
)
queue: deque[State] = deque(
[
State(
human_next_turn=26,
turns_remaining=26,
valves=valves,
distances=distances,
human_position="AA",
elephant_position="AA",
total_flow=0,
elephant_next_turn=26,
)
]
)
best_flow = 0
turns = 0
while True:
try:
# eliminate the late-stage games first in the hopes
# of getting cutoffs faster
state = queue.pop()
except IndexError:
if puzzle != TEST_INPUT:
print("\n")
return best_flow
if state.turns_remaining <= 0:
best_flow = max(best_flow, state.total_flow)
# end state
continue
if all(valve[0] for valve in state.valves.values()):
# do nothing
if state.total_flow > best_flow:
best_flow = max(best_flow, state.total_flow)
continue
if (
state.total_flow
+ (
limit := max_theoretical_flow(
state.valves, turns_remaining=state.turns_remaining
)
)
< best_flow
):
# bail out if there's no way we could make it work
continue
queue.extend(state.take_action())
turns += 1
if puzzle != TEST_INPUT and not turns % 10000:
print(
f"{turns=}\t{len(queue)=}\t{best_flow=}",
end="\r",
)
def main():
part_one_result = part_one(TEST_INPUT)
assert part_one_result == 1651, part_one_result
puzzle = Path("day16.txt").read_text().splitlines()
print(part_one(puzzle))
part_two_result = part_two(TEST_INPUT)
assert part_two_result == 1707, part_two_result
print(part_two(puzzle))
if __name__ == "__main__":
main()