Refactor code for API (#3)
Co-authored-by: Mikael CAPELLE <mikael.capelle@thalesaleniaspace.com> Co-authored-by: Mikaël Capelle <capelle.mikael@gmail.com> Reviewed-on: #3
This commit is contained in:
		| @@ -1,8 +1,9 @@ | ||||
| from __future__ import annotations | ||||
|  | ||||
| import heapq | ||||
| import sys | ||||
| from typing import Literal, TypeAlias, cast | ||||
| from typing import Any, Iterator, Literal, TypeAlias, cast | ||||
|  | ||||
| from ..base import BaseSolver | ||||
|  | ||||
| PlayerType: TypeAlias = Literal["player", "boss"] | ||||
| SpellType: TypeAlias = Literal["magic missile", "drain", "shield", "poison", "recharge"] | ||||
| @@ -62,17 +63,6 @@ def play( | ||||
|             continue | ||||
|  | ||||
|         visited.add((player, player_hp, player_mana, player_armor, boss_hp, buffs)) | ||||
|  | ||||
|         if hard_mode and player == "player": | ||||
|             player_hp = max(0, player_hp - 1) | ||||
|  | ||||
|         if player_hp == 0: | ||||
|             continue | ||||
|  | ||||
|         if boss_hp == 0: | ||||
|             winning_node = spells | ||||
|             continue | ||||
|  | ||||
|         new_buffs: list[tuple[BuffType, int]] = [] | ||||
|         for buff, length in buffs: | ||||
|             length = length - 1 | ||||
| @@ -88,6 +78,16 @@ def play( | ||||
|             if length > 0: | ||||
|                 new_buffs.append((buff, length)) | ||||
|  | ||||
|         if hard_mode and player == "player": | ||||
|             player_hp = player_hp - 1 | ||||
|  | ||||
|         if player_hp <= 0: | ||||
|             continue | ||||
|  | ||||
|         if boss_hp <= 0: | ||||
|             winning_node = spells | ||||
|             continue | ||||
|  | ||||
|         buffs = tuple(new_buffs) | ||||
|  | ||||
|         if player == "boss": | ||||
| @@ -155,23 +155,28 @@ def play( | ||||
|     return winning_node | ||||
|  | ||||
|  | ||||
| lines = sys.stdin.read().splitlines() | ||||
| class Solver(BaseSolver): | ||||
|     def solve(self, input: str) -> Iterator[Any]: | ||||
|         lines = input.splitlines() | ||||
|  | ||||
| player_hp = 50 | ||||
| player_mana = 500 | ||||
| player_armor = 0 | ||||
|         player_hp = 50 | ||||
|         player_mana = 500 | ||||
|         player_armor = 0 | ||||
|  | ||||
| boss_hp = int(lines[0].split(":")[1].strip()) | ||||
| boss_attack = int(lines[1].split(":")[1].strip()) | ||||
|         boss_hp = int(lines[0].split(":")[1].strip()) | ||||
|         boss_attack = int(lines[1].split(":")[1].strip()) | ||||
|  | ||||
| answer_1 = sum( | ||||
|     c | ||||
|     for _, c in play(player_hp, player_mana, player_armor, boss_hp, boss_attack, False) | ||||
| ) | ||||
| print(f"answer 1 is {answer_1}") | ||||
|         yield sum( | ||||
|             c | ||||
|             for _, c in play( | ||||
|                 player_hp, player_mana, player_armor, boss_hp, boss_attack, False | ||||
|             ) | ||||
|         ) | ||||
|  | ||||
| # 1242 (not working) | ||||
| answer_2 = sum( | ||||
|     c for _, c in play(player_hp, player_mana, player_armor, boss_hp, boss_attack, True) | ||||
| ) | ||||
| print(f"answer 2 is {answer_2}") | ||||
|         # 1242 (not working) | ||||
|         yield sum( | ||||
|             c | ||||
|             for _, c in play( | ||||
|                 player_hp, player_mana, player_armor, boss_hp, boss_attack, True | ||||
|             ) | ||||
|         ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user