-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·596 lines (553 loc) · 23.7 KB
/
part_a.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python3
import itertools
import re
from dataclasses import dataclass
from typing import Tuple
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
23385
"""
group_set = GroupSet.from_groups_text(_input)
group_set.step_many(debug=debug)
return group_set.get_unit_count()
class GroupSet:
group_class = NotImplemented
re_faction_name = re.compile(r"^(.+):$")
@classmethod
def from_groups_text(cls, groups_text):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> len(group_set_a.groups)
4
>>> [group.faction for group in group_set_a.groups]
['Immune System', 'Immune System', 'Infection', 'Infection']
>>> [group.initiative for group in group_set_a.groups]
[2, 3, 1, 4]
>>> [group.weaknesses for group in group_set_a.groups]
[('radiation', 'bludgeoning'), ('bludgeoning', 'slashing'),
('radiation',), ('fire', 'cold')]
"""
faction_strs = groups_text.strip().split("\n\n")
groups = []
for faction_str in faction_strs:
non_empty_lines = list(filter(None, faction_str.splitlines()))
name_str, group_strs = non_empty_lines[0], non_empty_lines[1:]
faction_name, = cls.re_faction_name.match(name_str).groups()
groups.extend(
cls.group_class.from_group_text(faction_name, group_str)
for group_str in group_strs
)
initiatives = [group.initiative for group in groups]
if len(initiatives) != len(set(initiatives)):
raise Exception(
f"Got {len(initiatives) - len(set(initiatives))} "
f"duplicate initiatives")
return cls(groups)
def __init__(self, groups):
self.groups = groups
def get_unit_count(self):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.get_unit_count()
6292
>>> group_set_a.step_many()
True
>>> group_set_a.get_unit_count()
5216
"""
return sum(
group.size
for group in self.groups
)
def step_many(self, count=None, debug=False, report_count=100000):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.step_many(3)
False
>>> [(group.initiative, group.size) for group in group_set_a.groups]
[(3, 618), (1, 789), (4, 4434)]
>>> group_set_a.step_many(4)
False
>>> [(group.initiative, group.size) for group in group_set_a.groups]
[(3, 49), (1, 782), (4, 4434)]
>>> group_set_a.step_many()
True
>>> [(group.initiative, group.size) for group in group_set_a.groups]
[(1, 782), (4, 4434)]
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.step_many()
True
>>> [(group.initiative, group.size) for group in group_set_a.groups]
[(1, 782), (4, 4434)]
"""
if count is None:
steps = itertools.count()
else:
steps = range(count)
finished = False
for step in steps:
finished = self.step()
if finished:
break
if debug and step % report_count == 0:
print(
f"Step {step} with {self.get_unit_count()} units in "
f"{len(self.groups)} groups")
return finished
def step(self):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.step()
False
>>> [(group.initiative, group.size) for group in group_set_a.groups]
[(3, 905), (1, 797), (4, 4434)]
"""
attacking_pairs = self.get_attacking_pairs()
if not attacking_pairs:
return True
unit_count_before = self.get_unit_count()
for attacker, defender in attacking_pairs:
if attacker.size <= 0:
continue
units_killed = attacker.get_units_killed(defender)
defender.size -= units_killed
self.groups = [group for group in self.groups if group.size > 0]
unit_count_after = self.get_unit_count()
if unit_count_after == unit_count_before:
return True
return False
def get_attacking_pairs(self):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> [
... (attacker.initiative, defender.initiative)
... for attacker, defender in group_set_a.get_attacking_pairs()
... ]
[(4, 3), (3, 1), (2, 4), (1, 2)]
"""
targeted_pairs = self.get_targeted_pairs()
return sorted(targeted_pairs, key=self.attack_sort_key, reverse=True)
def attack_sort_key(self, pair):
attacker, _ = pair
return attacker.initiative
def get_targeted_pairs(self):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> [
... (attacker.initiative, defender.initiative)
... for attacker, defender in group_set_a.get_targeted_pairs()
... ] # [1, 2, 4, 3]
[(1, 2), (2, 4), (4, 3), (3, 1)]
"""
pairs = []
sorted_groups_for_selection = self.sort_groups_for_selection()
unopposed_groups = set(self.groups)
for group in sorted_groups_for_selection:
chosen_enemy = self.choose_enemy(group, unopposed_groups)
if not chosen_enemy:
continue
unopposed_groups.remove(chosen_enemy)
pairs.append((group, chosen_enemy))
return pairs
def choose_enemy(self, group, unopposed_groups):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.choose_enemy(
... group_set_a.groups[0],
... {group_set_a.groups[2], group_set_a.groups[3]})
Group(..., initiative=4, ...)
"""
enemies = group.filter_enemies(unopposed_groups)
chosen_enemy = max(
enemies, key=self.get_group_enemy_sort_key(group), default=None)
return chosen_enemy
def get_group_enemy_sort_key(self, group):
def group_enemy_sort_key(enemy):
return (
group.get_potential_attack(enemy),
enemy.effective_power,
enemy.initiative,
)
return group_enemy_sort_key
def sort_groups_for_selection(self):
"""
>>> def get_sorted_order(groups_attributes):
... group_set = GroupSet([
... Group(**{
... "faction": 'a', "hit_points": 1,
... "attack_type": 'r', "weaknesses": (), "immunities": (),
... **group_attributes,
... })
... for group_attributes in groups_attributes
... ])
... sorted_groups = group_set.sort_groups_for_selection()
... return [
... group.initiative
... for group in sorted_groups
... ]
>>> get_sorted_order([
... {"size": 1, "attack": 1, "initiative": 0},
... {"size": 1, "attack": 2, "initiative": 1},
... ])
[1, 0]
>>> get_sorted_order([
... {"size": 1, "attack": 2, "initiative": 0},
... {"size": 1, "attack": 2, "initiative": 1},
... ])
[1, 0]
>>> get_sorted_order([
... {"size": 1, "attack": 2, "initiative": 2},
... {"size": 1, "attack": 2, "initiative": 1},
... ])
[2, 1]
>>> get_sorted_order([
... {"size": 1, "attack": 3, "initiative": 2},
... {"size": 1, "attack": 2, "initiative": 1},
... ])
[2, 1]
>>> get_sorted_order([
... {"size": 1, "attack": 3, "initiative": 0},
... {"size": 1, "attack": 2, "initiative": 1},
... ])
[0, 1]
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> [
... group.initiative
... for group in group_set_a.sort_groups_for_selection()
... ] # {1: 92916, 2: 76619, 4: 53820, 3: 24725}
[1, 2, 4, 3]
"""
return sorted(
self.groups, key=self.group_selection_sort_key, reverse=True)
def group_selection_sort_key(self, group):
return group.effective_power, group.initiative
@dataclass
class Group:
faction: str
size: int
hit_points: int
initiative: int
attack: int
attack_type: str
weaknesses: Tuple[str, ...]
immunities: Tuple[str, ...]
re_group = re.compile(
r"(\d+) units each with (\d+) hit points "
r"(?:\((?:(weak|immune) to ([^;)]+))?"
r"(?:; )?"
r"(?:(weak|immune) to ([^;)]+))?\) )?"
r"with an attack that does (\d+) (\w+) damage at initiative (\d+)")
@classmethod
def from_group_text(cls, faction, group_text):
"""
>>> Group.from_group_text("Immune System",
... "18 units each with 729 hit points "
... "(weak to fire, radiation; immune to cold, slashing) "
... "with an attack that does 23 radiation damage at initiative 10")
Group(faction='Immune System', size=18, hit_points=729, initiative=10,
attack=23, attack_type='radiation',
weaknesses=('fire', 'radiation'), immunities=('cold', 'slashing'))
>>> Group.from_group_text("Immune System",
... "2 units each with 2 hit points "
... "(weak to fire; immune to cold) "
... "with an attack that does 8 radiation damage at initiative 1")
Group(faction='Immune System', size=2, hit_points=2, initiative=1,
attack=8, attack_type='radiation',
weaknesses=('fire',), immunities=('cold',))
>>> Group.from_group_text("Immune System",
... "2 units each with 2 hit points "
... "(immune to cold) "
... "with an attack that does 8 radiation damage at initiative 1")
Group(faction='Immune System', size=2, hit_points=2, initiative=1,
attack=8, attack_type='radiation',
weaknesses=(), immunities=('cold',))
>>> Group.from_group_text("Immune System",
... "2 units each with 2 hit points "
... "(weak to fire) "
... "with an attack that does 8 radiation damage at initiative 1")
Group(faction='Immune System', size=2, hit_points=2, initiative=1,
attack=8, attack_type='radiation',
weaknesses=('fire',), immunities=())
>>> Group.from_group_text("Immune System",
... "2 units each with 2 hit points "
... "with an attack that does 8 radiation damage at initiative 1")
Group(faction='Immune System', size=2, hit_points=2, initiative=1,
attack=8, attack_type='radiation',
weaknesses=(), immunities=())
>>> Group.from_group_text("Infection",
... "4485 units each with 2961 hit points "
... "(immune to radiation; weak to fire, cold) "
... "with an attack that does 12 slashing damage at initiative 4")
Group(faction='Infection', size=4485, hit_points=2961, initiative=4,
attack=12, attack_type='slashing',
weaknesses=('fire', 'cold'), immunities=('radiation',))
"""
match = cls.re_group.match(group_text)
if not match:
raise Exception(f"Could not parse '{group_text}'")
size_str, hit_points_str, list_a_name, list_a_str, list_b_name, \
list_b_str, attack_str, attack_type, initiative_str = \
match.groups()
unknown_list_names = \
{list_a_name, list_b_name} - {'weak', 'immune', None}
if unknown_list_names:
raise Exception(
f"Unknown list types {sorted(unknown_list_names)}: expected "
f"'weak' or 'immune'")
if list_a_name == 'weak':
weaknesses_str = list_a_str
immunities_str = list_b_str
else:
weaknesses_str = list_b_str
immunities_str = list_a_str
if weaknesses_str:
weaknesses = tuple(weaknesses_str.split(", "))
else:
weaknesses = ()
if immunities_str:
immunities = tuple(immunities_str.split(", "))
else:
immunities = ()
return cls(
faction=faction,
size=int(size_str), hit_points=int(hit_points_str),
initiative=int(initiative_str), attack=int(attack_str),
attack_type=attack_type, weaknesses=weaknesses,
immunities=immunities)
@property
def effective_power(self):
"""
>>> Group(
... faction='Immune System', size=18, hit_points=729, initiative=10,
... attack=23, attack_type='radiation',
... weaknesses=('fire', 'radiation'),
... immunities=('cold', 'slashing')).effective_power
414
"""
return self.size * self.attack
def __hash__(self):
return hash(self.initiative)
def get_units_killed(self, enemy):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.groups[3].get_units_killed(group_set_a.groups[1])
84
"""
potential_attack = self.get_potential_attack(enemy)
return min(potential_attack // enemy.hit_points, enemy.size)
def filter_enemies(self, groups):
return (
group
for group in groups
if group.faction != self.faction
)
def get_potential_attack(self, enemy):
"""
>>> group_set_a = GroupSet.from_groups_text(
... "Immune System:\\n"
... "17 units each with 5390 hit points (weak to radiation, "
... "bludgeoning) with an attack that does 4507 fire damage at "
... "initiative 2\\n"
... "989 units each with 1274 hit points (immune to fire; weak to "
... "bludgeoning, slashing) with an attack that does 25 slashing "
... "damage at "
... "initiative 3\\n"
... "\\n"
... "Infection:\\n"
... "801 units each with 4706 hit points (weak to radiation) with "
... "an attack that does 116 bludgeoning damage at initiative 1\\n"
... "4485 units each with 2961 hit points (immune to radiation; "
... "weak to fire, cold) with an attack that does 12 slashing "
... "damage at "
... "initiative 4\\n"
... )
>>> group_set_a.groups[0].get_potential_attack(group_set_a.groups[2])
76619
>>> group_set_a.groups[0].attack_type
'fire'
>>> sorted(group_set_a.groups[3].weaknesses)
['cold', 'fire']
>>> group_set_a.groups[0].get_potential_attack(group_set_a.groups[3])
153238
"""
if self.attack_type in enemy.immunities:
return 0
if self.attack_type in enemy.weaknesses:
return 2 * self.effective_power
return self.effective_power
GroupSet.group_class = Group
Challenge.main()
challenge = Challenge()