-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.py
150 lines (113 loc) · 4.02 KB
/
day24.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
import re
import operator as op
import copy
class Group:
def __init__(self, army, num, units, hp, dmg_type, dmg, weaknesses, immunities, initiative):
self.army = army
self.num = num
self.units = units
self.hp = hp
self.dmg_type = dmg_type
self.dmg = dmg
self.weaknesses = weaknesses
self.immunities = immunities
self.initiative = initiative
def effective_power(self):
return self.units * self.dmg
def calc_damage(self, dmg, dmg_type):
if dmg_type in self.weaknesses:
return dmg*2
if dmg_type in self.immunities:
return 0
return dmg
def take_damage(self, dmg, dmg_type):
eff_dmg = self.calc_damage(dmg, dmg_type)
killed = min(self.units, eff_dmg // self.hp)
self.units -= killed
return eff_dmg, killed
def is_dead(self):
return self.units <= 0
def __str__(self):
return f"{self.army}-{self.num} {self.units} with {self.dmg_type}:{self.hp}hp +{self.immunities}"
def __hash__(self):
return hash(self.army+str(self.num))
def parse_group(line, army, num):
weaknesses = []
immunities = []
main = re.compile(
r"(\d+) units each with (\d+) hit points (\(.*\) )?with an attack that does (\d+) ([a-z]+) damage at initiative (\d+)")
m = main.match(line)
if not m:
print(f"cannot parse \"{line}\"")
if m.group(3):
wm = re.search(r"weak to ((?:[a-z]+,? ?)+)", m.group(3))
if wm:
weaknesses = [i.strip() for i in wm.group(1).split(",")]
im = re.search(r"immune to ((?:[a-z]+,? ?)+)", m.group(3))
if im:
immunities = [i.strip() for i in im.group(1).split(",")]
return Group(army, num, int(m.group(1)), int(m.group(2)), m.group(5), int(m.group(4)), weaknesses, immunities, int(m.group(6)))
def load_file(filename):
immune_sys = []
infection = []
army = "immune"
curr = immune_sys
num = 1
for line in open(filename).readlines():
line = line.strip("\n")
if not line:
continue
if line.startswith("Immune System:"):
pass
elif line == "Infection:":
army = "infection"
curr = infection
num = 1
else:
curr.append(parse_group(line, army, num))
num += 1
return infection, immune_sys
def battle(inf, imm):
units = inf + imm
pairs = []
kills = 0
targeted = set()
for unit in sorted(units, key=lambda u: (u.effective_power(), u.initiative), reverse=True):
enemies = list(filter(lambda u: u.army !=
unit.army and u not in targeted, units))
if enemies:
enemy = max(enemies, key=lambda e: (e.calc_damage(
unit.effective_power(), unit.dmg_type), e.effective_power(), e.initiative))
if enemy.calc_damage(unit.dmg, unit.dmg_type) != 0:
pairs.append((unit, enemy))
targeted.add(enemy)
for a, d in sorted(pairs, key=lambda a: a[0].initiative, reverse=True):
if a.is_dead():
continue
_, killed = d.take_damage(a.effective_power(), a.dmg_type)
kills += killed
return list(filter(lambda g: not g.is_dead(), inf)), list(filter(lambda g: not g.is_dead(), imm)), kills
def print_state(inf, imm):
print("Infection army:")
print("\n".join(map(str, inf)))
print("Immunity army:")
print("\n".join(map(str, imm)))
oinf, oimm = load_file("day24.txt")
print_state(oinf, oimm)
boost = 0
while True:
inf = copy.deepcopy(oinf)
imm = copy.deepcopy(oimm)
for g in imm:
g.dmg += boost
while inf and imm:
inf, imm, kills = battle(inf, imm)
# sometimes we get a tie, nobody can hurt anyone
if kills == 0:
break
if boost == 0:
print("Solution 1:", sum(map(lambda g: g.units, inf+imm)))
if imm and not inf:
print("Solution 2:", sum(map(lambda g: g.units, imm)))
break
boost += 1