Solve 2015 days 20-22.
This commit is contained in:
89
2015/d21.py
Normal file
89
2015/d21.py
Normal file
@@ -0,0 +1,89 @@
|
||||
from itertools import combinations
|
||||
|
||||
data = open(0).read().strip()
|
||||
|
||||
# hit points, damage, armor
|
||||
enemy = [100, 8, 2]
|
||||
|
||||
weapons = [
|
||||
[8, 4, 0],
|
||||
[10, 5, 0],
|
||||
[25, 6, 0],
|
||||
[40, 7, 0],
|
||||
[74, 8, 0],
|
||||
]
|
||||
|
||||
# Armor: Cost Damage Armor
|
||||
armor = [
|
||||
[13, 0, 1],
|
||||
[31, 0, 2],
|
||||
[53, 0, 3],
|
||||
[75, 0, 4],
|
||||
[102, 0, 5],
|
||||
]
|
||||
|
||||
rings = [
|
||||
[25, 1, 0],
|
||||
[50, 2, 0],
|
||||
[100, 3, 0],
|
||||
[20, 0, 1],
|
||||
[40, 0, 2],
|
||||
[80, 0, 3],
|
||||
]
|
||||
|
||||
|
||||
def player_configs():
|
||||
for wc, wd, wa in weapons:
|
||||
for ac, ad, aa in armor:
|
||||
for i in range(1, 3):
|
||||
for rgs in combinations(rings, i):
|
||||
c = wc + ac
|
||||
d = wd + ad
|
||||
a = wa + aa
|
||||
for rc, rd, ra in rgs:
|
||||
c += rc
|
||||
d += rd
|
||||
a += ra
|
||||
yield (c, d, a)
|
||||
c = wc + ac
|
||||
d = wd + ad
|
||||
a = wa + aa
|
||||
yield (c, d, a)
|
||||
for i in range(1, 3):
|
||||
for rgs in combinations(rings, i):
|
||||
c = wc
|
||||
d = wd
|
||||
a = wa
|
||||
for rc, rd, ra in rgs:
|
||||
c += rc
|
||||
d += rd
|
||||
a += ra
|
||||
yield (c, d, a)
|
||||
|
||||
|
||||
def simulate(player, enemey):
|
||||
ppoints, pdamage, parmor = player
|
||||
epoints, edamage, earmor = enemey
|
||||
|
||||
while ppoints > 0 and epoints > 0:
|
||||
epoints -= max(pdamage - earmor, 1)
|
||||
ppoints -= max(edamage - parmor, 1)
|
||||
|
||||
if epoints <= 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
part_1 = False
|
||||
if part_1:
|
||||
min_win = float("inf")
|
||||
for c, d, a in player_configs():
|
||||
if simulate([100, d, a], list(enemy)):
|
||||
min_win = min(c, min_win)
|
||||
print(min_win)
|
||||
else:
|
||||
max_loss = 0
|
||||
for c, d, a in player_configs():
|
||||
if not simulate([100, d, a], list(enemy)):
|
||||
max_loss = max(c, max_loss)
|
||||
print(max_loss)
|
||||
Reference in New Issue
Block a user