-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path21_RPG_Simulator_20XX.py
82 lines (65 loc) · 1.98 KB
/
21_RPG_Simulator_20XX.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
from itertools import combinations, product
WEAPONS = [
('Dagger', 8, 4, 0),
('Shortsword', 10, 5, 0),
('Warhammer', 25, 6, 0),
('Longsword', 40, 7, 0),
('Greataxe', 74, 8, 0)
]
ARMORS = [
('Fake', 0, 0, 0),
('Leather', 13, 0, 1),
('Chainmail', 31, 0, 2),
('Splintmail', 53, 0, 3),
('Bandedmail', 75, 0, 4),
('Platemail', 102, 0, 5)
]
RINGS = [
('Fake', 0, 0, 0),
('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)
]
NAME, COST, DMG, ARMOR = 0, 1, 2, 3
def read_boss():
boss = dict()
for line in open('21_input.txt'):
n, x = line.strip().split(': ')
boss[n] = int(x)
return boss
boss = read_boss()
def simulate(weapon, armor, rings):
health = 100
total_dmg = weapon[DMG]
total_armor = 0
boss_health = boss['Hit Points']
total_dmg += armor[DMG]
total_armor += armor[ARMOR]
for r in rings:
total_dmg += r[DMG]
total_armor += r[ARMOR]
while True:
boss_health -= max(total_dmg - boss['Armor'], 1)
if boss_health <= 0:
return True
health -= max(boss['Damage'] - total_armor, 1)
if health <= 0:
return False
def my_combinations():
for w, a, r in product(WEAPONS, ARMORS, product(RINGS, repeat=2)):
if r[0] == RINGS[0] or r[0] != r[1]:
cost = w[COST] + a[COST] + sum(rr[COST] for rr in r)
yield cost, w, a, r
min_cost = 1000*1000*1000
for cost, w, a, r in my_combinations():
if cost < min_cost and simulate(w, a, r):
min_cost = cost
print('Star 1:', min_cost)
max_cost = 0
for cost, w, a, r in my_combinations():
if cost > max_cost and not simulate(w, a, r):
max_cost = cost
print('Star 2:', max_cost)