-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
178 lines (117 loc) · 4.63 KB
/
22.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
from lib import *
input = read_input(2015, 22)
(*_, boss_hp), (*_, boss_damage) = map(str.split, input.splitlines())
boss_hp, boss_damage = map(int, [boss_hp, boss_damage])
player_hp = 50
player_mana = 500
def tick(*timers):
return [max(0, t - 1) for t in timers]
def fight_boss(player, boss, mana, shield_timer, poison_timer, recharge_timer):
armor = 7 if shield_timer else 0
if poison_timer:
boss -= 3
if recharge_timer:
mana += 101
shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)
if boss <= 0:
return 0
return fight_player(player - max(1, boss_damage - armor), boss, mana, shield_timer, poison_timer, recharge_timer)
def fight_player(player, boss, mana, shield_timer, poison_timer, recharge_timer):
if player <= 0:
return None
if poison_timer:
boss -= 3
if recharge_timer:
mana += 101
shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)
if boss <= 0:
return 0
out = []
# Magic Missile costs 53 mana. It instantly does 4 damage.
if (
mana >= 53
and (x := fight_boss(player, boss - 4, mana - 53, shield_timer, poison_timer, recharge_timer)) is not None
):
out.append(x + 53)
# Drain costs 73 mana. It instantly does 2 damage and heals you for 2 hit points.
if (
mana >= 73
and (x := fight_boss(player + 2, boss - 2, mana - 73, shield_timer, poison_timer, recharge_timer)) is not None
):
out.append(x + 73)
# Shield costs 113 mana. It starts an effect that lasts for 6 turns.
# While it is active, your armor is increased by 7.
if (
mana >= 113
and not shield_timer
and (x := fight_boss(player, boss, mana - 113, 6, poison_timer, recharge_timer)) is not None
):
out.append(x + 113)
# Poison costs 173 mana. It starts an effect that lasts for 6 turns.
# At the start of each turn while it is active, it deals the boss 3 damage.
if (
mana >= 173
and not poison_timer
and (x := fight_boss(player, boss, mana - 173, shield_timer, 6, recharge_timer)) is not None
):
out.append(x + 173)
# Recharge costs 229 mana. It starts an effect that lasts for 5 turns.
# At the start of each turn while it is active, it gives you 101 new mana.
if (
mana >= 229
and not recharge_timer
and (x := fight_boss(player, boss, mana - 229, shield_timer, poison_timer, 5)) is not None
):
out.append(x + 229)
return min(out, default=None)
print(fight_player(player_hp, boss_hp, player_mana, 0, 0, 0))
def fight_player(player, boss, mana, shield_timer, poison_timer, recharge_timer):
player -= 1
if player <= 0:
return None
if poison_timer:
boss -= 3
if recharge_timer:
mana += 101
shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)
if boss <= 0:
return 0
out = []
# Magic Missile costs 53 mana. It instantly does 4 damage.
if (
mana >= 53
and (x := fight_boss(player, boss - 4, mana - 53, shield_timer, poison_timer, recharge_timer)) is not None
):
out.append(x + 53)
# Drain costs 73 mana. It instantly does 2 damage and heals you for 2 hit points.
if (
mana >= 73
and (x := fight_boss(player + 2, boss - 2, mana - 73, shield_timer, poison_timer, recharge_timer)) is not None
):
out.append(x + 73)
# Shield costs 113 mana. It starts an effect that lasts for 6 turns.
# While it is active, your armor is increased by 7.
if (
mana >= 113
and not shield_timer
and (x := fight_boss(player, boss, mana - 113, 6, poison_timer, recharge_timer)) is not None
):
out.append(x + 113)
# Poison costs 173 mana. It starts an effect that lasts for 6 turns.
# At the start of each turn while it is active, it deals the boss 3 damage.
if (
mana >= 173
and not poison_timer
and (x := fight_boss(player, boss, mana - 173, shield_timer, 6, recharge_timer)) is not None
):
out.append(x + 173)
# Recharge costs 229 mana. It starts an effect that lasts for 5 turns.
# At the start of each turn while it is active, it gives you 101 new mana.
if (
mana >= 229
and not recharge_timer
and (x := fight_boss(player, boss, mana - 229, shield_timer, poison_timer, 5)) is not None
):
out.append(x + 229)
return min(out, default=None)
print(fight_player(player_hp, boss_hp, player_mana, 0, 0, 0))