-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·560 lines (467 loc) · 16.6 KB
/
part_a.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python3
import re
from copy import deepcopy
from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, List, Generic, Type, Optional, Any, Callable
import click
from aox.challenge import Debugger
from utils import BaseChallenge, TV, get_type_argument_class
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
953
"""
return Game.from_boss_text(_input).find_min_mana_necessary(debugger)
def play(self):
Game.from_boss_text(self.input).play()
PlayerT = TV["Player"]
BossT = TV["Boss"]
class CharacterEnum(Enum):
Player = "Player"
Boss = "Boss"
def __repr__(self):
return f"{type(self).__name__}.{self.name}"
@dataclass
class Game(Generic[PlayerT, BossT]):
player: PlayerT
boss: BossT
next_turn_character_type: CharacterEnum
winner: Optional[CharacterEnum]
options_played: List[Any]
@classmethod
def get_player_class(cls) -> Type[PlayerT]:
return get_type_argument_class(cls, PlayerT)
@classmethod
def get_boss_class(cls) -> Type[BossT]:
return get_type_argument_class(cls, BossT)
@classmethod
def from_boss_text(cls, boss_text: str) -> "Game":
"""
>>> Game.from_boss_text('''
... Hit Points: 55
... Damage: 8
... ''')
Game(player=Player(hit_points=50, mana=500, mana_spent=0,
active_spell_timers={}),
boss=Boss(hit_points=55, damage=8),
next_turn_character_type=CharacterEnum.Player,
winner=None, options_played=[])
"""
player_class = cls.get_player_class()
boss_class = cls.get_boss_class()
return cls(
player=player_class(),
boss=boss_class.from_boss_text(boss_text),
next_turn_character_type=CharacterEnum.Player,
winner=None,
options_played=[],
)
def __str__(self):
"""
>>> print(Game.from_boss_text('''
... Hit Points: 55
... Damage: 8
... '''))
Next to play: Player
* Player: 50HP/500M (0M spent), no spells
* Boss: 55HP/8DMG
"""
if self.winner:
next_to_play_str = f"Winner: {self.winner.name}"
else:
next_to_play_str = (
f"Next to play: {self.next_turn_character_type.name}"
)
return (
f"{next_to_play_str}"
f"\n* {self.player}"
f"\n* {self.boss}"
)
def play(self):
while not self.finished:
click.echo(self)
result = self.pre_play_turn()
if result:
click.echo(
f"Pre-play result:\n{click.style(result, fg='blue')}"
)
options = self.get_play_options()
if options:
for index, option in enumerate(options, 1):
click.echo(
f"{index}. {click.style(option.name, fg='green')}"
)
user_choices = (
list(map(str, range(1, len(options) + 1)))
+ ["a"]
)
user_choice = click.prompt(
click.style("Select a choice", fg='yellow'),
type=click.Choice(user_choices),
default=1 if len(options) == 1 else None,
)
if user_choice == "a":
option = None
else:
option = options[int(user_choice) - 1]
else:
click.echo(f"{click.style('No options', fg='red')}")
user_choice = None
option = None
if user_choice == "a":
for option in options:
game_copy = deepcopy(self)
result = game_copy.play_turn(option)
if result:
click.echo(f"Result:\n{click.style(result, fg='blue')}")
click.echo(game_copy)
else:
result = self.play_turn(option)
if result:
click.echo(f"Result:\n{click.style(result, fg='blue')}")
click.echo(self)
click.echo(f"Winner: {click.style(self.winner.name, fg='blue')}")
@property
def finished(self) -> bool:
return self.winner is not None
def find_min_mana_necessary(
self, debugger: Debugger = Debugger(enabled=False),
) -> int:
def reporting_format(_: Debugger, message: str) -> str:
if min_mana_spent is None:
min_mana_spent_str = "no winner yet"
else:
min_mana_spent_str = f"best is {min_mana_spent}"
return f"{message} ({min_mana_spent_str}, {len(stack)} in stack)"
with debugger.adding_extra_report_format(reporting_format):
stack = [deepcopy(self)]
min_mana_spent = None
min_mana_game = None
debugger.default_report_if("Searching for a winning game...")
while debugger.step_if(stack):
debugger.default_report_if("Searching for a winning game...")
game = stack.pop(0)
if min_mana_spent is not None \
and game.player.mana_spent >= min_mana_spent:
continue
next_games = game.get_next_games(debugger)
for next_game in next_games:
if (
min_mana_spent is not None
and next_game.player.mana_spent >= min_mana_spent
):
continue
if next_game.winner == CharacterEnum.Player:
min_mana_spent = next_game.player.mana_spent
min_mana_game = next_game
debugger.report(f"Better game found: {min_mana_spent}")
stack.append(next_game)
debugger.default_report(f"Finished searching")
if min_mana_spent is None:
raise Exception(f"Could not find a winning game")
options_played_str = ', '.join(
option.name
for option in min_mana_game.options_played
if isinstance(option, SpellEnum)
)
debugger.report(f"Min mana game moves: {options_played_str}")
return min_mana_spent
def get_next_games(
self, debugger: Debugger = Debugger(enabled=False),
) -> List["Game"]:
def play_turn(option: Any) -> Game:
next_game = deepcopy(game)
next_game.play_turn(option)
return next_game
game = deepcopy(self)
game.pre_play_turn()
return list(map(play_turn, game.get_play_options()))
def get_play_options(self) -> List[Any]:
if self.winner:
return []
character = self.next_turn_character
return character.get_play_options()
def pre_play_turn(self) -> Optional[str]:
if self.finished:
return None
character = self.next_turn_character
other = self.next_turn_other_character
steps = self.get_pre_play_steps(character, other)
return self.apply_play_steps(steps)
def play_turn(self, option: Any) -> Optional[str]:
if self.finished:
return None
self.options_played.append(option)
if option is None:
self.winner = self.next_turn_other_character_type
return None
character = self.next_turn_character
other = self.next_turn_other_character
steps = self.get_play_steps(character, other, option)
result = self.apply_play_steps(steps)
if self.finished:
return result
self.next_turn_character_type = self.next_turn_other_character_type
return result
def apply_play_steps(
self, steps: List[Callable[[], Optional[str]]],
) -> Optional[str]:
character = self.next_turn_character
other = self.next_turn_other_character
actions = []
for step in steps:
actions.append(step())
if character.is_dead or other.is_dead:
break
actions = list(filter(None, actions))
if actions:
result = "\n".join(actions)
else:
result = None
if character.is_dead:
self.winner = self.next_turn_other_character_type
return result
elif other.is_dead:
self.winner = self.next_turn_character_type
return result
return result
def get_pre_play_steps(
self, character: "Character", other: "Character",
) -> List[Callable[[], Optional[str]]]:
return [
lambda: character.apply_spells(other),
lambda: other.apply_spells(character),
]
def get_play_steps(
self, character: "Character", other: "Character", option: Any,
) -> List[Callable[[], Optional[str]]]:
return [
lambda: character.play(other, option),
]
@property
def next_turn_character(self) -> "Character":
return self.get_character_by_type(self.next_turn_character_type)
@property
def next_turn_other_character_type(self) -> CharacterEnum:
return {
CharacterEnum.Player: CharacterEnum.Boss,
CharacterEnum.Boss: CharacterEnum.Player,
}[self.next_turn_character_type]
@property
def next_turn_other_character(self) -> "Character":
return self.get_character_by_type(self.next_turn_other_character_type)
def get_character_by_type(self, _type: CharacterEnum) -> "Character":
return {
CharacterEnum.Player: self.player,
CharacterEnum.Boss: self.boss,
}[_type]
class SpellEnum(Enum):
MagicMissile = "MagicMissile"
Drain = "Drain"
Shield = "Shield"
Poison = "Poison"
Recharge = "Recharge"
def __lt__(self, other):
return self.name < other.name
class Character:
def get_play_options(self) -> List[Any]:
raise NotImplementedError()
def play(self, other: "Character", option: Any) -> str:
raise NotImplementedError()
@property
def is_dead(self) -> bool:
raise NotImplementedError()
@property
def shield(self) -> int:
raise NotImplementedError()
def attack(self, amount: int, force: bool = False) -> int:
raise NotImplementedError()
def apply_spells(self, other: "Character") -> Optional[str]:
raise NotImplementedError()
@dataclass
class Player(Character):
hit_points: int = 50
mana: int = 500
mana_spent: int = 0
active_spell_timers: Dict[SpellEnum, int] = field(default_factory=dict)
SPELL_COST = {
SpellEnum.MagicMissile: 53,
SpellEnum.Drain: 73,
SpellEnum.Shield: 113,
SpellEnum.Poison: 173,
SpellEnum.Recharge: 229,
}
SPELL_DURATION = {
SpellEnum.Shield: 6,
SpellEnum.Poison: 6,
SpellEnum.Recharge: 5,
}
def __str__(self):
"""
>>> print(Player())
Player: 50HP/500M (0M spent), no spells
>>> print(Player(
... active_spell_timers={SpellEnum.Poison: 2, SpellEnum.Shield: 3},
... ))
Player: 50HP/500M (0M spent), Poison: 2r, Shield: 3r
"""
if any(self.active_spell_timers.values()):
spells_str = ", ".join(
f"{spell.name}: {timer}r"
for spell, timer in self.active_spell_timers.items()
)
else:
spells_str = "no spells"
return (
f"Player: {self.hit_points}HP/{self.mana}M "
f"({self.mana_spent}M spent), {spells_str}"
)
def get_available_spells(self) -> List[SpellEnum]:
mana = self.mana
if self.active_spell_timers.get(SpellEnum.Recharge, 0) > 0:
mana += 101
return sorted(
spell
for spell in SpellEnum
if self.active_spell_timers.get(spell, 0) <= 0
and self.SPELL_COST[spell] <= mana
)
@property
def shield(self) -> int:
if self.active_spell_timers.get(SpellEnum.Shield, 0) <= 0:
return 0
return 7
def get_play_options(self) -> List[Any]:
return self.get_available_spells()
def play(self, other: "Character", option: Any) -> str:
assert isinstance(other, Boss)
assert isinstance(option, SpellEnum)
assert self.active_spell_timers.get(option, 0) <= 0
assert self.SPELL_COST[option] <= self.mana
self.mana -= self.SPELL_COST[option]
self.mana_spent += self.SPELL_COST[option]
if option == SpellEnum.MagicMissile:
attack_amount = other.attack(4)
return f"Magic missile attacked for {attack_amount}DMG"
elif option == SpellEnum.Drain:
attack_amount = other.attack(2)
self.hit_points += 2
return f"Drain attacked for {attack_amount}DMG, healed 2HP"
else:
self.active_spell_timers[option] = self.SPELL_DURATION[option]
return (
f"Casted spell {option.name} for {self.SPELL_DURATION[option]} "
f"rounds"
)
@property
def is_dead(self) -> bool:
"""
>>> Player().is_dead
False
>>> Player(hit_points=1).is_dead
False
>>> Player(hit_points=0).is_dead
True
>>> Player(hit_points=-5).is_dead
True
"""
return self.hit_points <= 0
def attack(self, amount: int, force: bool = False) -> int:
if force:
attack_amount = 1
else:
attack_amount = max(amount - self.shield, 1)
self.hit_points -= attack_amount
return attack_amount
def apply_spells(self, other: "Character") -> Optional[str]:
actions = []
if self.active_spell_timers.get(SpellEnum.Poison, 0) > 0:
attack_amount = other.attack(3)
actions.append(f"Poison attacked for {attack_amount}DMG")
if self.active_spell_timers.get(SpellEnum.Recharge, 0) > 0:
self.mana += 101
actions.append("Recharged 101M")
spell_timer_actions = ", ".join(
f"{spell}'s timer is now {timer - 1}"
for spell, timer in self.active_spell_timers.items()
if timer > 0
)
self.active_spell_timers = {
spell: timer - 1
for spell, timer in self.active_spell_timers.items()
if timer > 1
}
if spell_timer_actions:
actions.append(spell_timer_actions)
if actions:
return f", ".join(actions)
else:
return None
class BossChoicesEnum(Enum):
Attack = "Attack"
@dataclass
class Boss(Character):
hit_points: int
damage: int
re_boss = re.compile(
r"^\s*Hit Points:\s*(\d+)\s*Damage:\s*(\d+)\s*$"
)
@classmethod
def from_boss_text(cls, boss_text: str) -> "Boss":
"""
>>> Boss.from_boss_text('''
... Hit Points: 55
... Damage: 8
... ''')
Boss(hit_points=55, damage=8)
"""
hit_points_str, damage_str = cls.re_boss.match(boss_text).groups()
return cls(
hit_points=int(hit_points_str),
damage=int(damage_str),
)
def __str__(self):
"""
>>> print(Boss.from_boss_text('''
... Hit Points: 55
... Damage: 8
... '''))
Boss: 55HP/8DMG
"""
return f"Boss: {self.hit_points}HP/{self.damage}DMG"
def get_play_options(self) -> List[Any]:
return [BossChoicesEnum.Attack]
def play(self, other: "Character", option: Any) -> str:
assert isinstance(other, Player)
assert option == BossChoicesEnum.Attack
attack_amount = other.attack(self.damage)
return f"Attacked for {attack_amount}DMG"
@property
def is_dead(self) -> bool:
"""
>>> Boss(hit_points=50, damage=8).is_dead
False
>>> Boss(hit_points=1, damage=8).is_dead
False
>>> Boss(hit_points=0, damage=8).is_dead
True
>>> Boss(hit_points=-5, damage=8).is_dead
True
"""
return self.hit_points <= 0
@property
def shield(self) -> int:
return 0
def attack(self, amount: int, force: bool = False) -> int:
if force:
attack_amount = 1
else:
attack_amount = max(amount - self.shield, 1)
self.hit_points -= attack_amount
return attack_amount
def apply_spells(self, other: "Character") -> Optional[str]:
return None
Challenge.main()
challenge = Challenge()