-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21_2.py
75 lines (68 loc) · 1.99 KB
/
day21_2.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
import re
import itertools
# Name Cost Damage Armor
weapons = [
"Dagger 8 4 0",
"Shortsword 10 5 0",
"Warhammer 25 6 0",
"Longsword 40 7 0",
"Greataxe 74 8 0"
]
armor = [
"Leather 13 0 1",
"Chainmail 31 0 2",
"Splintmail 53 0 3",
"Bandedmail 75 0 4",
"Platemail 102 0 5"
]
rings = [
"Damage+1 25 1 0",
"Damage+2 50 2 0",
"Damage+3 100 3 0",
"Defense+1 20 0 1",
"Defense+2 40 0 2",
"Defense+3 80 0 3"
]
def simulate(damage, armor, bossHp, bossDamage, bossArmor):
hp = 100
currentBossHp = bossHp
myTurn = True
won = False
while hp > 0 :
if myTurn:
hit = damage - bossArmor
currentBossHp -= hit if hit > 0 else 1
else:
hit = bossDamage - armor
hp -= hit if hit > 0 else 1
if currentBossHp <= 0:
won = True
break
myTurn = not myTurn
return won
def evaluate(partialSet):
totalCost = 0
totalDamage = 0
totalArmor = 0
for item in partialSet:
name, cost, damage, armor = item.split(" ")
totalCost += int(cost)
totalDamage += int(damage)
totalArmor += int(armor)
return (totalCost, totalDamage, totalArmor)
def solve(bossHp, bossDamage, bossArmor):
pricesThatLost = []
for combo in list(itertools.product(weapons, armor, rings, rings)):
fullSet = list(combo)
combos = []
for i in range(len(fullSet[1:])):
combos.append(list(itertools.combinations(fullSet[1:], i)))
combos.append([tuple(fullSet[1:])])
for combinationSet in combos:
for combination in combinationSet:
partialSet = list(set([fullSet[0]] + list(combination)))
setPrice, setDamage, setArmor = evaluate(partialSet)
if not simulate(setDamage, setArmor, bossHp, bossDamage, bossArmor):
pricesThatLost.append(setPrice)
print max(pricesThatLost) if len(pricesThatLost) else 0
solve(109, 8, 2)