from functools import cache from collections import namedtuple Opponent = namedtuple('Opponent', ['hp', 'damage']) def part1(opponent: Opponent) -> int: @cache def min_cost(php: int, pmana: int, ohp: int, sld_timer: int, psn_timer: int, rch_timer: int) -> int | float: psld = 0 def effects(): nonlocal ohp, pmana, psld, psn_timer, rch_timer, sld_timer if sld_timer > 0: sld_timer -= 1 psld = 7 else: psld = 0 if psn_timer > 0: psn_timer -= 1 ohp -= 3 if rch_timer > 0: rch_timer -= 1 pmana += 101 def opponent_turn(cost: int) -> int | float: effects() nonlocal php if ohp <= 0: return cost php -= min(1, opponent.attack - psld) if php <= 0: return float('inf') return cost + min_cost(php, pmana, ohp, sld_timer, psn_timer, rch_timer) effects() if ohp <= 0: return 0 if __name__ == '__main__': opponent = Opponent(51, 9) print(part1(opponent)) #print(part2(opponent))