Skip to content
advent of code 2015

day 22

wizard simulator 20xx

approach

the whole fight is a tuple: mana spent, whose turn it is, both hit points, current mana, armor and the three effect timers. states sit in a heap ordered by mana spent, so the first state popped with the boss dead is the cheapest win. it is dijkstra, even though the function is called bfs.

each pop applies the active effects first. on the player's turn neighbors yields one new state per affordable spell, skipping effects that are still running; on the boss's turn the single follow-up state takes the hit. part 2 is the same search with a flag that costs the player one hit point every time effects are applied. the starting stats are hardcoded.

the trick

put mana spent first in the heap tuple and the first winning state out is the answer.

python
def neighbors(sm, bhp, php, pmp, se, pe, re):
    if pmp >= 53:
        yield sm + 53, bhp - 4, php, pmp - 53, se, pe, re
    if pmp >= 73:
        yield sm + 73, bhp - 2, php + 2, pmp - 73, se, pe, re
    if pmp >= 113 and se == 0:
        yield sm + 113, bhp, php, pmp - 113, 6, pe, re
    if pmp >= 173 and pe == 0:
        yield sm + 173, bhp, php, pmp - 173, se, 6, re
    if pmp >= 229 and re == 0:
        yield sm + 229, bhp, php, pmp - 229, se, pe, 5