-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.py
399 lines (341 loc) · 13.7 KB
/
day19.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import unittest, sys
from tqdm import tqdm
from enum import Enum
if len(sys.argv) != 2:
print("Missing input file.")
sys.exit(1)
filename = sys.argv[1]
sys.argv = sys.argv[:1] # strip args, they scare the unittest module
is_sample = filename != "input.txt"
verbose = False
# This speeds up things a lot, but it is risky. When this is enabled,
# and a geode bot can be produced, it will be produced and all other
# options are ignored. There can be blueprint for which this does not work.
# With our sample and input data, it does, though.
optimize_for_geode_bots = True
def parse(filename=filename):
with open(filename) as f:
blueprints = []
for line in f.readlines():
words = line.strip().split()
num = int(words[1].replace(":",""))
ore_bot_cost = int(words[6])
clay_bot_cost = int(words[12])
obsidian_bot_ore_cost = int(words[18])
obsidian_bot_clay_cost = int(words[21])
geode_bot_ore_cost = int(words[27])
geode_bot_obsidian_cost = int(words[30])
blueprints += [(num, ore_bot_cost, clay_bot_cost,
obsidian_bot_ore_cost, obsidian_bot_clay_cost,
geode_bot_ore_cost, geode_bot_obsidian_cost)]
return blueprints
class Build(Enum):
NOTHING = 0
ORE_BOT = 1
CLAY_BOT = 2
OBSIDIAN_BOT = 3
GEODE_BOT = 4
def possible_builds(blueprint, resources, minutes_left, is_part_2):
_, ore_bot_cost, clay_bot_cost, \
obsidian_bot_ore_cost, obsidian_bot_clay_cost, \
geode_bot_ore_cost, geode_bot_obsidian_cost = blueprint
ore, clay, obsidian, geode = resources
builds = []
# limits found by crazy guessing and a lot of trial and error
geode_minute_limit = 0
obsidian_minute_limit = 1
clay_minute_limit = 10 if is_part_2 else 5
ore_minute_limit = 20 if is_part_2 else 12
if ore >= geode_bot_ore_cost and obsidian >= geode_bot_obsidian_cost \
and minutes_left > geode_minute_limit:
if optimize_for_geode_bots:
return [Build.GEODE_BOT]
builds += [Build.GEODE_BOT]
if ore >= obsidian_bot_ore_cost and clay >= obsidian_bot_clay_cost \
and minutes_left > obsidian_minute_limit:
builds += [Build.OBSIDIAN_BOT]
if ore >= clay_bot_cost \
and minutes_left > clay_minute_limit:
builds += [Build.CLAY_BOT]
if ore >= ore_bot_cost \
and minutes_left > ore_minute_limit:
builds += [Build.ORE_BOT]
# try to avoid building nothing
if is_sample:
if is_part_2:
crazy_sample_part2_hack = obsidian_bot_clay_cost > 10
if crazy_sample_part2_hack or len(builds) == 0:
builds += [Build.NOTHING]
else:
builds += [Build.NOTHING]
else:
magic_value_found_by_trial_and_error = 12
crazy_part1_hack = not is_part_2 and \
obsidian_bot_clay_cost < magic_value_found_by_trial_and_error
if crazy_part1_hack or len(builds) == 0:
builds += [Build.NOTHING]
return builds
def mine_resources(bots, resources):
ore, clay, obsidian, geode = resources
ore_bot, clay_bot, obsidian_bot, geode_bot = bots
ore += ore_bot
clay += clay_bot
obsidian += obsidian_bot
geode += geode_bot
return (ore, clay, obsidian, geode)
def step(minute, build, bots, resources, blueprint):
resources = mine_resources(bots, resources)
ore, clay, obsidian, geode = resources
ore_bot, clay_bot, obsidian_bot, geode_bot = bots
_, ore_bot_cost, clay_bot_cost, \
obsidian_bot_ore_cost, obsidian_bot_clay_cost, \
geode_bot_ore_cost, geode_bot_obsidian_cost = blueprint
if build == Build.NOTHING:
return (minute, bots, resources, build)
if build == Build.ORE_BOT:
ore_bot += 1
ore -= ore_bot_cost
bots = (ore_bot, clay_bot, obsidian_bot, geode_bot)
resources = (ore, clay, obsidian, geode)
return (minute, bots, resources, build)
if build == Build.CLAY_BOT:
clay_bot += 1
ore -= clay_bot_cost
bots = (ore_bot, clay_bot, obsidian_bot, geode_bot)
resources = (ore, clay, obsidian, geode)
return (minute, bots, resources, build)
if build == Build.OBSIDIAN_BOT:
obsidian_bot += 1
ore -= obsidian_bot_ore_cost
clay -= obsidian_bot_clay_cost
bots = (ore_bot, clay_bot, obsidian_bot, geode_bot)
resources = (ore, clay, obsidian, geode)
return (minute, bots, resources, build)
if build == Build.GEODE_BOT:
geode_bot += 1
ore -= geode_bot_ore_cost
obsidian -= geode_bot_obsidian_cost
bots = (ore_bot, clay_bot, obsidian_bot, geode_bot)
resources = (ore, clay, obsidian, geode)
return (minute, bots, resources, build)
def reduce_states(states, minutes_left, max_geode):
# group states per bots, but skip those that cannot reach a new max geode
bots_to_states = {}
for state in states:
_, bots, resources, _ = state
_, _, _, geode = resources
_, _, _, geode_bot = bots
potential_max_geode = geode
additional_geode_bots = 0
for i in range(1, minutes_left + 1):
# every minute, we open as many geodes as we have geode bots
potential_max_geode += geode_bot + additional_geode_bots
# best-case, we also produce a new geode bot every minute
additional_geode_bots += 1
if potential_max_geode < max_geode:
# we can never reach a higher max geode with this state, so skip it
continue
if bots not in bots_to_states:
bots_to_states[bots] = []
bots_to_states[bots].append(state)
best_states_for_all_bots = set()
for bots, states in bots_to_states.items():
# Group items that have the same clay, geodes, and obsidian (but different
# ore) together
clay_geode_obsidian_to_states = {}
for state in states:
_, _, (_, clay, obsidian, geode), _ = state
key = (clay, geode, obsidian)
if key not in clay_geode_obsidian_to_states:
clay_geode_obsidian_to_states[key] = []
clay_geode_obsidian_to_states[key].append(state)
# In each clay/geode/obsidian group, keep only the one with the highest
# ore count
for _, states in clay_geode_obsidian_to_states.items():
if len(states) <= 1:
continue
states.sort(key=lambda state: state[2][0], reverse=True)
states = states[:1]
# group items that have the same ore, geodes, and obsidian (but different
# clay) together
ore_geode_obsidian_to_states = {}
for state in states:
_, _, (ore, _, obsidian, geode), _ = state
key = (ore, geode, obsidian)
if key not in ore_geode_obsidian_to_states:
ore_geode_obsidian_to_states[key] = []
ore_geode_obsidian_to_states[key].append(state)
# in each ore/geode/obsidian group, keep only the one with the highest
# clay count
for _, states in ore_geode_obsidian_to_states.items():
if len(states) <= 1:
continue
states.sort(key=lambda state: state[2][1], reverse=True)
states = states[:1]
# group items that have the same ore, clay, and geodes (but different
# obsidian) together
ore_clay_geode_to_states = {}
for state in states:
_, _, (ore, clay, _, geode), _ = state
key = (ore, clay, geode)
if key not in ore_clay_geode_to_states:
ore_clay_geode_to_states[key] = []
ore_clay_geode_to_states[key].append(state)
# in each ore/clay/geode group, keep only the one with the highest
# obsidian count
for _, states in ore_clay_geode_to_states.items():
if len(states) <= 1:
continue
states.sort(key=lambda state: state[2][2], reverse=True)
states = states[:1]
# group items that have the same ore, clay, and obsidian (but different
# geodes) together
ore_clay_obsidian_to_states = {}
for state in states:
_, _, (ore, clay, obsidian, _), _ = state
key = (ore, clay, obsidian)
if key not in ore_clay_obsidian_to_states:
ore_clay_obsidian_to_states[key] = []
ore_clay_obsidian_to_states[key].append(state)
# in each ore/clay/obsidian group, keep only the one with the highest
# geode count
for _, states in ore_clay_obsidian_to_states.items():
if len(states) <= 1:
continue
states.sort(key=lambda state: state[2][3], reverse=True)
states = states[:1]
# merge values of clay_geode_obsidian_to_states, ore_geode_obsidian_to_states,
# ore_clay_geode_to_states, and ore_clay_obsidian_to_states into list
best_states = set()
for _, states in clay_geode_obsidian_to_states.items():
for state in states:
best_states.add(state)
for _, states in ore_geode_obsidian_to_states.items():
for state in states:
best_states.add(state)
for _, states in ore_clay_geode_to_states.items():
for state in states:
best_states.add(state)
for _, states in ore_clay_obsidian_to_states.items():
for state in states:
best_states.add(state)
best_states_for_all_bots |= best_states
return list(best_states_for_all_bots)
def simulate(blueprints, max_minutes, is_part_2=False):
quality_levels = []
max_geodes = []
for blueprint in blueprints:
num = blueprint[0]
max_geode = 0
if not is_sample and not is_part_2 and use_pre_calc and num in pre_calc_quality_levels:
pre_calc_quality_level = pre_calc_quality_levels[num]
if verbose:
print(f"Blueprint {num}/{len(blueprints)}: quality level {pre_calc_quality_level} (pre-calc)")
quality_levels.append(pre_calc_quality_level)
continue
states = [(0, (1, 0, 0, 0), (0, 0, 0, 0), Build.NOTHING)] # initial
for minute in range(1, max_minutes + 1):
minutes_left = max_minutes - minute
next_states = []
state_iter = states
if verbose and len(states) > 10000:
state_iter = tqdm(states, f"States ({minute}/{max_minutes}")
for state in state_iter:
_, bots, resources, factory = state
builds = possible_builds(blueprint, resources, minutes_left, is_part_2)
possibilities = [step(minute, build, bots, resources,
blueprint) for build in builds]
for possibility in possibilities:
_, _, _, geode = possibility[2]
if geode > max_geode:
max_geode = geode
next_states.append(possibility)
reduced_states = reduce_states(next_states, minutes_left, max_geode)
# print(f"min: {minute} - reduced states: {len(next_states)} → {len(reduced_states)}")
states = reduced_states
max_geodes.append(max_geode)
quality_level = max_geode * num
quality_levels.append(quality_level)
if verbose:
print("===================================================================")
print(f"Blueprint {num}/{len(blueprints)}: {len(states)} final states, max geode: {max_geode}, quality level: {quality_level}")
print("===================================================================")
print()
return max_geodes if is_part_2 else quality_levels
def part1():
blueprints = parse()
max_minutes = 24
quality_levels = simulate(blueprints, max_minutes)
if verbose:
print("Quality levels:", quality_levels)
return sum(quality_levels)
def part2():
blueprints = parse()
if not is_sample:
blueprints = blueprints[:3]
max_minutes = 32
max_geodes = simulate(blueprints, max_minutes, is_part_2=True)
if verbose:
print("Max geodes:", max_geodes)
result = 1
for max_geode in max_geodes:
result *= max_geode
return result
class TestDay19(unittest.TestCase):
def test_parse(self):
blueprints = parse()
if is_sample:
self.assertEqual(2, len(blueprints))
self.assertEqual((1, 4, 2, 3, 14, 2, 7), blueprints[0])
self.assertEqual((2, 2, 3, 3, 8, 3, 12), blueprints[1])
else:
self.assertEqual(30, len(blueprints))
use_pre_calc = False
pre_calc_quality_levels = {
1: 3,
2: 0,
3: 0,
4: 8,
5: 45,
6: 12,
7: 7,
8: 0,
9: 54,
10: 10,
11: 165,
12: 0,
13: 117,
14: 154,
15: 0,
16: 0,
17: 204,
18: 0,
19: 0,
20: 60,
21: 63,
22: 22,
23: 368,
24: 0,
25: 175,
26: 234,
27: 135,
28: 0,
29: 435,
30: 30,
}
if __name__ == '__main__':
unittest.main(exit=False)
print()
if not verbose:
expected_run_time = 90 if is_sample else 35
if optimize_for_geode_bots:
expected_run_time = 12 if is_sample else 25
print(f"Attention, this stuff takes a while to run (about {expected_run_time}s).")
print("To see some progress bars, enable verbose mode (verbose = True)")
print()
res1 = part1()
assert(res1 == 33 if is_sample else res1 == 2301)
res2 = part2()
assert(res2 == 56 * 62 if is_sample else res2 == 10336)
print(f"Part 1: {res1}", "(sample)" if is_sample else "")
print(f"Part 2: {res2}", "(sample)" if is_sample else "")