-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
332 lines (300 loc) · 5.39 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
_ "embed"
"fmt"
"math"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
type Spell int
const (
MagicMissile Spell = iota
Drain
Shield
Poison
Recharge
)
func (s Spell) String() string {
switch s {
case MagicMissile:
return "Magic Missile"
case Drain:
return "Drain"
case Shield:
return "Shield"
case Poison:
return "Poison"
case Recharge:
return "Recharge"
default:
return "Unknown Spell"
}
}
func (s Spell) Cost() int {
switch s {
case MagicMissile:
return 53
case Drain:
return 73
case Shield:
return 113
case Poison:
return 173
case Recharge:
return 229
default:
return 0
}
}
func (s Spell) Damage() int {
switch s {
case MagicMissile:
return 4
case Drain:
return 2
case Poison:
return 3
default:
return 0
}
}
func (s Spell) Turns() int {
switch s {
case Shield:
return 6
case Poison:
return 6
case Recharge:
return 5
default:
return 0
}
}
func (s Spell) Heal() int {
switch s {
case Drain:
return 2
default:
return 0
}
}
func (s Spell) Mana() int {
switch s {
case Recharge:
return 101
default:
return 0
}
}
func (s Spell) Armor() int {
if s == Shield {
return 7
}
return 0
}
var AllSpells = []Spell{MagicMissile, Drain, Shield, Poison, Recharge}
// Shield = Spell{name: "Shield", cost: 113, turns: 6, armor: 7}
// MagicMissile = Spell{name: "Magic Missile", cost: 53, damage: 4}
// Drain = Spell{name: "Drain", cost: 73, damage: 2, heal: 2}
// Poison = Spell{name: "Poison", cost: 173, turns: 6, damage: 3}
// Recharge = Spell{name: "Recharge", cost: 229, turns: 5, mana: 101}
type ActiveSpells [5]int
type Me struct {
hp int
armor int
mana int
manaSpent int
active ActiveSpells
}
type Boss struct {
hp, damage int
}
type Game struct {
bossHp, bossDamage int
debug bool
}
func NewGame(in []int) *Game {
return &Game{
bossHp: in[0],
bossDamage: in[1],
debug: false,
}
}
type State struct {
me *Me
boss *Boss
debug bool
}
func (s *State) Clone() *State {
nm := Me{
hp: s.me.hp, armor: s.me.armor, mana: s.me.mana,
manaSpent: s.me.manaSpent,
}
for k, v := range s.me.active {
nm.active[k] = v
}
return &State{
me: &nm,
boss: &Boss{hp: s.boss.hp, damage: s.boss.damage},
debug: s.debug}
}
func (s *State) Turn(sp Spell) {
if s.debug {
fmt.Printf("-- Player turn --\n")
fmt.Printf("- Player has %d hit points, %d armor, %d mana\n",
s.me.hp, s.me.armor, s.me.mana)
fmt.Printf("- Boss has %d hit points\n", s.boss.hp)
}
for i, v := range s.me.active {
if v == 0 {
continue
}
k := Spell(i)
s.me.hp += k.Heal()
s.me.mana += k.Mana()
s.boss.hp -= k.Damage()
s.me.active[i]--
if s.debug {
fmt.Printf("%s active\n", k)
}
if s.me.active[i] == 0 {
if k.Armor() != 0 {
s.me.armor -= k.Armor()
}
if s.debug {
fmt.Printf("%s wears off\n", k)
}
}
}
s.me.mana -= sp.Cost()
s.me.manaSpent += sp.Cost()
if sp.Turns() > 0 {
if s.debug {
fmt.Printf("Player casts %s\n", sp)
}
s.me.active[sp] = sp.Turns()
if sp.Armor() != 0 {
s.me.armor += sp.Armor()
}
} else {
if s.debug {
fmt.Printf("Player casts %s with instant effects\n", sp)
}
s.me.hp += sp.Heal()
s.me.mana += sp.Mana()
s.boss.hp -= sp.Damage()
}
if s.boss.hp <= 0 {
if s.debug {
fmt.Printf("Boss is dead\n")
}
return
}
if s.debug {
fmt.Printf("\n")
fmt.Printf("-- Boss turn --\n")
fmt.Printf("- Player has %d hit points, %d armor, %d mana\n",
s.me.hp, s.me.armor, s.me.mana)
fmt.Printf("- Boss has %d hit points\n", s.boss.hp)
}
for i, v := range s.me.active {
if v == 0 {
continue
}
k := Spell(i)
s.me.hp += k.Heal()
s.me.mana += k.Mana()
s.boss.hp -= k.Damage()
s.me.active[i]--
if s.debug {
fmt.Printf("%s active (%d)\n", k, s.me.active[k])
}
if s.me.active[i] == 0 {
if k.Armor() != 0 {
s.me.armor -= k.Armor()
}
if s.debug {
fmt.Printf("%s wears off\n", k)
}
}
}
if s.boss.hp <= 0 {
if s.debug {
fmt.Printf("Boss is dead\n")
}
return
}
damage := MaxInt(1, s.boss.damage-s.me.armor)
if s.debug {
fmt.Printf("Boss attacks for %d damage\n\n", damage)
}
s.me.hp -= damage
if s.me.hp <= 0 {
if s.debug {
fmt.Printf("Player is dead\n")
}
}
}
func (g *Game) Calc(hardMode bool) int {
minCost := math.MaxInt32
todo := []*State{{
&Me{hp: 50, mana: 500},
&Boss{hp: g.bossHp, damage: g.bossDamage},
g.debug,
}}
for len(todo) > 0 {
cur := todo[0]
todo = todo[1:]
if hardMode {
cur.me.hp--
if cur.me.hp <= 0 {
continue
}
}
for _, spell := range AllSpells {
if cur.me.active[spell] > 1 {
continue // currently active
}
if cur.me.mana < spell.Cost() {
continue // can't afford spell
}
new := cur.Clone()
new.Turn(spell)
if new.boss.hp <= 0 {
if g.debug {
fmt.Printf("Player wins %d (%d)\n",
new.me.manaSpent, minCost)
}
if minCost > new.me.manaSpent {
minCost = new.me.manaSpent
}
continue
}
if new.me.hp <= 0 {
continue
}
todo = append(todo, new)
}
}
return minCost
}
func (g *Game) Part1() int {
return g.Calc(false)
}
func (g *Game) Part2() int {
return g.Calc(true)
}
func main() {
in := InputInts(input)
g := NewGame(in)
p1 := g.Part1()
if !benchmark {
fmt.Printf("Part 1: %d\n", p1)
}
p2 := g.Part2()
if !benchmark {
fmt.Printf("Part 2: %d\n", p2)
}
}
var benchmark bool