summaryrefslogtreecommitdiffstats
path: root/22/solution.py
blob: 6df8379956a502782486b06e12375484d0a8c855 (plain)
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
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))