|
| 1 | +import re, numpy, math, itertools, copy, random |
| 2 | + |
| 3 | +finder = re.compile(r'Hit Points: (\d+)[\S\s]+Damage: (\d+)') |
| 4 | + |
| 5 | +with open('data/day22.txt') as fp: |
| 6 | + b_hp, b_dmg = [int(x) for x in finder.findall(fp.read())[0]] |
| 7 | + |
| 8 | +# cost, gain, dmg, life, shield, turns |
| 9 | +missle = [53, 0, 4, 0, 0, 1] # 0 |
| 10 | +drain = [73, 0, 2, 2, 0, 1] # 1 |
| 11 | +shield = [113, 0, 0, 0, 7, 6] # 2 |
| 12 | +poison = [173, 0, 3, 0, 0, 6] # 3 |
| 13 | +regen = [229, 101, 0, 0, 0, 5] # 4 |
| 14 | +spells = [missle, drain, shield, poison, regen] |
| 15 | + |
| 16 | +# DEBUG |
| 17 | +# listme = ([3, 4, 2, 3, 4, 2, 3, 0, 0]) |
| 18 | +best = 99999999 |
| 19 | + |
| 20 | +def go(p_hp, mana, b_hp, effects = [None, None, None, None, None], turn = 0, spent = 0): |
| 21 | + global b_dmg, spells, listme, best |
| 22 | + arm = 0 |
| 23 | + turn += 1 |
| 24 | + |
| 25 | + # DEBUG |
| 26 | + # if turn % 2 == 0: |
| 27 | + # print("\nBoss's turn") |
| 28 | + # else: |
| 29 | + # print("\nPlayer's turn") |
| 30 | + # print("Upkeep effects:", effects) |
| 31 | + |
| 32 | + for idx, e in enumerate(effects): |
| 33 | + if e != None: |
| 34 | + cost, gain, dmg, life, shield, lasts = e |
| 35 | + mana += gain |
| 36 | + b_hp -= dmg |
| 37 | + p_hp += life |
| 38 | + arm += shield |
| 39 | + |
| 40 | + e[5] -= 1 |
| 41 | + effects[idx] = e if e[5] > 0 else None |
| 42 | + |
| 43 | + # DEBUG |
| 44 | + # print("Main phase effects:", effects) |
| 45 | + |
| 46 | + if spent >= best: |
| 47 | + return [(False, spent, turn)] |
| 48 | + |
| 49 | + # DEBUG |
| 50 | + # print("Boss hp after effects", b_hp) |
| 51 | + |
| 52 | + if b_hp <= 0: |
| 53 | + print ("WIN", spent, casted) |
| 54 | + best = spent |
| 55 | + return [(True, spent, turn)] |
| 56 | + |
| 57 | + if turn % 2 == 0: # even turns are boss' turns |
| 58 | + p_hp -= (b_dmg - arm) |
| 59 | + |
| 60 | + # DEBUG |
| 61 | + # print("Player's hp after fight", p_hp) |
| 62 | + |
| 63 | + if p_hp <= 0: |
| 64 | + return [(False, spent, turn)] |
| 65 | + |
| 66 | + if turn % 2 == 1: |
| 67 | + rr = [] |
| 68 | + for idx, s in enumerate(spells): |
| 69 | + eff_now = copy.deepcopy(effects) |
| 70 | + mana_now = mana |
| 71 | + spent_now = spent |
| 72 | + casted_now = casted[:] |
| 73 | + |
| 74 | + # DEBUG |
| 75 | + # idx = random.randint(0, 2) |
| 76 | + # while eff_now[idx] != None: |
| 77 | + # idx = random.randint(0, 2) |
| 78 | + |
| 79 | + i = math.floor(turn/2) |
| 80 | + if i >= len(listme): |
| 81 | + print("out of scope") |
| 82 | + exit() |
| 83 | + |
| 84 | + idx = listme[i] |
| 85 | + |
| 86 | + if eff_now[idx] == None: |
| 87 | + mana_now -= spells[idx][0] |
| 88 | + spent_now += spells[idx][0] |
| 89 | + eff_now[idx] = spells[idx][:] |
| 90 | + casted_now += [idx] |
| 91 | + # else: |
| 92 | + # print("you can't") |
| 93 | + # exit() |
| 94 | + |
| 95 | + # DEBUG |
| 96 | + # print("Player casts", spells[idx][0], "remaining mana:", mana_now) |
| 97 | + |
| 98 | + if mana_now < 0: |
| 99 | + # print ("L - mana") |
| 100 | + rr += [(False, spent_now, turn)] |
| 101 | + # return [(False, spent_now, turn)] |
| 102 | + else: |
| 103 | + # print(eff_now) |
| 104 | + rr += go(p_hp, mana_now, b_hp, eff_now, turn, spent_now, casted_now) |
| 105 | + # return go(p_hp, mana_now, b_hp, eff_now, turn, spent_now) |
| 106 | + |
| 107 | + return rr |
| 108 | + else: |
| 109 | + return go(p_hp, mana, b_hp, copy.deepcopy(effects), turn, spent, casted[:]) |
| 110 | + |
| 111 | +print([x for x in go(50, 500, b_hp) if x[0] == True]) |
0 commit comments