This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (132 loc) · 2.53 KB
/
main.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"math"
"github.com/derat/advent-of-code/lib"
)
func main() {
init := state{hp: 50, mana: 500} // given in description
lib.Extract(lib.Input("2015/22"), `^Hit Points: (\d+)\nDamage: (\d+)\n$`, &init.bossHP, &init.bossDamage)
best := state{spent: math.MaxInt32}
var playerTurn, bossTurn func(state)
playerTurn = func(s state) {
// Part 2: Hard mode
s.hp -= s.turnDamage
if s.lost() {
return
}
s.update()
if s.won() {
if s.spent < best.spent {
best = s
}
return
}
for _, sp := range []spell{magicMissle, drain, shield, poison, recharge} {
if ns, ok := sp(s); ok && ns.spent < best.spent {
bossTurn(ns)
}
}
}
bossTurn = func(s state) {
s.update()
if s.won() {
if s.spent < best.spent {
best = s
}
return
}
s.hp -= lib.Max(s.bossDamage-s.armor(), 1)
if !s.lost() {
playerTurn(s)
}
}
playerTurn(init)
fmt.Println(best.spent)
// Part 2: Player loses one HP at the beginning of each player turn.
init.turnDamage = 1
best = state{spent: math.MaxInt32}
playerTurn(init)
fmt.Println(best.spent)
}
type state struct {
hp, mana, spent int
bossHP, bossDamage int
shield, poison, recharge int // turns remaining for spells (0 if inactive)
turnDamage int // hp lost at start of player turns (part 2)
}
func (s *state) won() bool {
return s.hp > 0 && s.bossHP <= 0
}
func (s *state) lost() bool {
return s.hp <= 0
}
func (s *state) spend(mana int) bool {
if s.mana < mana {
return false
}
s.mana -= mana
s.spent += mana
return true
}
func (s *state) armor() int {
if s.shield > 0 {
return 7
}
return 0
}
// update updates s for the start of a turn.
func (s *state) update() {
if s.poison > 0 {
s.bossHP -= 3
}
if s.recharge > 0 {
s.mana += 101
}
if s.shield > 0 {
s.shield--
}
if s.poison > 0 {
s.poison--
}
if s.recharge > 0 {
s.recharge--
}
}
type spell func(s state) (state, bool)
func magicMissle(s state) (state, bool) {
if !s.spend(53) {
return s, false
}
s.bossHP -= 4
return s, true
}
func drain(s state) (state, bool) {
if !s.spend(73) {
return s, false
}
s.hp += 2
s.bossHP -= 2
return s, true
}
func shield(s state) (state, bool) {
if s.shield > 0 || !s.spend(113) {
return s, false
}
s.shield = 6
return s, true
}
func poison(s state) (state, bool) {
if s.poison > 0 || !s.spend(173) {
return s, false
}
s.poison = 6
return s, true
}
func recharge(s state) (state, bool) {
if s.recharge > 0 || !s.spend(229) {
return s, false
}
s.recharge = 5
return s, true
}