-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwenty-two.go
365 lines (331 loc) · 9.89 KB
/
twenty-two.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"fmt"
"maps"
"math"
"os"
"strconv"
"strings"
pq "github.com/emirpasic/gods/queues/priorityqueue"
utils "github.com/emirpasic/gods/utils"
)
type Player struct {
Name string
Hitpoints int
Damage int
Mana int
Armor int
}
type Spell struct {
Name string
Damage int
Cost int
Turns int
TickDamage int
Heal int
Armor int
ManaRecharge int
}
// This will be the State of the simulation at each point,
// Which will allow us to perform Dijkstra's shortest path to
// Victory, ie, less mana spent
type GameState struct {
Boss Player
Player Player
ManaSpent int
// Keep track of the turns. True = us, False means boss
Turn bool
isFinished bool
// Keep track of the effects currently active, and how many turns
onGoingStatusEffects map[string]int
}
func main() {
f, _ := os.ReadFile("input.txt")
l := string(f)
lines := strings.Split(l, "\n")
// Dont mind this nonsense, just getting the values from the file in a compact way
// Not very readable
hp, _ := strconv.Atoi(strings.TrimSpace(strings.Split(lines[0], ":")[1]))
damage, _ := strconv.Atoi(strings.TrimSpace(strings.Split(lines[1], ":")[1]))
boss := Player{Name: "Boss", Hitpoints: hp, Damage: damage, Armor: 0}
// boss = Player{Name: "Boss", Hitpoints: 14, Damage: 8, Armor: 0}
// Create myself as a player to fight the boss
myself := Player{Name: "Player", Hitpoints: 50, Mana: 500, Armor: 0}
spells := createSpells()
partOne(myself, boss, spells)
partTwo(myself, boss, spells)
}
func partOne(myself, boss Player, spells map[string]Spell) {
// Part 1
winningGames := simulateGame(myself, boss, spells, 1)
minCost := math.MaxInt32
minGame := GameState{}
for game, cost := range winningGames {
if cost < minCost {
minGame = deepCopy(*game)
minCost = cost
}
}
fmt.Printf("Part1: Gamestate: %+v, with cost: %d\n", minGame, minCost)
}
func partTwo(myself, boss Player, spells map[string]Spell) {
// Part 1
winningGames := simulateGame(myself, boss, spells, 2)
minCost := math.MaxInt32
minGame := GameState{}
for game, cost := range winningGames {
if cost < minCost {
minGame = deepCopy(*game)
minCost = cost
}
}
fmt.Printf("Part2: Gamestate: %+v, with cost: %d\n", minGame, minCost)
}
// Simulate game and return all the games the player wins
func simulateGame(player, boss Player, spells map[string]Spell, part int) map[*GameState]int {
// Turn = true, means its our turn
gameState := GameState{Boss: boss, Player: player, ManaSpent: 0, Turn: true, isFinished: false, onGoingStatusEffects: map[string]int{}}
pqueue := pq.NewWith(byPriority)
fmt.Println("---")
// Save the gamestates where the player wins
playerWins := map[*GameState]int{}
minManaCost := math.MaxInt32
pqueue.Enqueue(gameState)
fmt.Printf("Start: %+v\n", gameState)
for !pqueue.Empty() {
i, _ := pqueue.Dequeue()
lowestGameState := i.(GameState)
// fmt.Printf("Original: %+v\n", lowestGameState)
listPossibleGameState := calculateNextGameState(lowestGameState, spells, part)
for _, gs := range listPossibleGameState {
// fmt.Printf("PossibleGS: %+v\n", gs)
// Enqueue not finished games
if !gs.isFinished {
// The games can continue on til infinity,
// so only continue enqueueing if they have lower cost than the min
if gs.ManaSpent < minManaCost {
// fmt.Printf("Enqueued: %+v\n", gs)
pqueue.Enqueue(gs)
}
} else {
// Game is finished, and player won, save the mana cost
if gs.Boss.Hitpoints <= 0 {
playerWins[&gs] = gs.ManaSpent
// Save this minimum mana cost
minManaCost = min(minManaCost, gs.ManaSpent)
}
}
}
}
return playerWins
}
// From a given game state, calculate all the next possible
// game moves, meaning, spells.
func calculateNextGameState(gamestate GameState, spells map[string]Spell, part int) []GameState {
possibleGameState := []GameState{}
// At the start of each of my turns i take 1hp damage
if part == 2 && gamestate.Turn {
gamestate.Player.Hitpoints--
// Check if game ends. The previous damage can kill me
if gamestate.Player.Hitpoints <= 0 {
gamestate.isFinished = true
possibleGameState = append(possibleGameState, gamestate)
return possibleGameState
}
}
tickStatusEffects(&gamestate, spells)
// Check if game ends. The effect tick can kill the boss
if gamestate.Boss.Hitpoints <= 0 {
gamestate.isFinished = true
possibleGameState = append(possibleGameState, gamestate)
return possibleGameState
}
checkEndEffects(&gamestate)
// If its our turn
if gamestate.Turn {
// For each spell available, check if its a valid move, i.e, if enough mana
for name, spell := range spells {
// Create a new gamestate for each move
newGS := deepCopy(gamestate)
newGS.Turn = !newGS.Turn
switch name {
case "Magic Missile":
// Can cast
if newGS.Player.Mana-spell.Cost >= 0 {
newGS.Player.Mana -= spell.Cost
newGS.Boss.Hitpoints -= spell.Damage
newGS.ManaSpent += spell.Cost
// Check if game ends
if newGS.Boss.Hitpoints <= 0 {
newGS.isFinished = true
}
possibleGameState = append(possibleGameState, newGS)
} else {
// The player didn't have mana to cast anything. lost
newGS.isFinished = true
possibleGameState = append(possibleGameState, newGS)
}
case "Drain":
// Can cast
if newGS.Player.Mana-spell.Cost >= 0 {
newGS.Player.Mana -= spell.Cost
newGS.Player.Hitpoints += spell.Heal
newGS.Boss.Hitpoints -= spell.Damage
newGS.ManaSpent += spell.Cost
// Check if game ends
if newGS.Boss.Hitpoints <= 0 {
newGS.isFinished = true
}
possibleGameState = append(possibleGameState, newGS)
} else {
// The player didn't have mana to cast anything. lost
newGS.isFinished = true
possibleGameState = append(possibleGameState, newGS)
}
case "Shield":
// Can cast
if newGS.Player.Mana-spell.Cost >= 0 {
// Check if doesnt exist, meaning it can cast
if _, ok := newGS.onGoingStatusEffects[name]; !ok {
// Start the Effect and increase armor
newGS.onGoingStatusEffects[name] = 6
newGS.Player.Armor = spell.Armor
newGS.Player.Mana -= spell.Cost
newGS.ManaSpent += spell.Cost
possibleGameState = append(possibleGameState, newGS)
}
} else {
// The player didn't have mana to cast anything. lost
newGS.isFinished = true
possibleGameState = append(possibleGameState, newGS)
}
case "Poison":
// Can cast
if newGS.Player.Mana-spell.Cost >= 0 {
// Check if doesnt exist, meaning it can cast
if _, ok := newGS.onGoingStatusEffects[name]; !ok {
// Start the Effect and increase armor
newGS.onGoingStatusEffects[name] = 6
newGS.Player.Mana -= spell.Cost
newGS.ManaSpent += spell.Cost
possibleGameState = append(possibleGameState, newGS)
}
} else {
// The player didn't have mana to cast anything. lost
newGS.isFinished = true
possibleGameState = append(possibleGameState, newGS)
}
case "Recharge":
// Check if doesnt exist, meaning it can cast
if newGS.Player.Mana-spell.Cost >= 0 {
// Check if exists and is bigger than 0
if _, ok := newGS.onGoingStatusEffects[name]; !ok {
// Start the Effect and increase armor
newGS.onGoingStatusEffects[name] = 5
newGS.Player.Mana -= spell.Cost
newGS.ManaSpent += spell.Cost
possibleGameState = append(possibleGameState, newGS)
}
} else {
// The player didn't have mana to cast anything. lost
newGS.isFinished = true
possibleGameState = append(possibleGameState, newGS)
}
}
}
} else {
// Boss's turn
newGS := deepCopy(gamestate)
newGS.Turn = !newGS.Turn
newGS.Player.Hitpoints -= (newGS.Boss.Damage - newGS.Player.Armor)
// Check if game ends. The effect tick can kill the boss
if newGS.Player.Hitpoints <= 0 {
newGS.isFinished = true
}
possibleGameState = append(possibleGameState, newGS)
}
return possibleGameState
}
func tickStatusEffects(gamestate *GameState, spells map[string]Spell) {
for name := range gamestate.onGoingStatusEffects {
switch name {
case "Shield":
gamestate.onGoingStatusEffects[name]--
case "Poison":
gamestate.onGoingStatusEffects[name]--
gamestate.Boss.Hitpoints -= spells[name].TickDamage
case "Recharge":
gamestate.onGoingStatusEffects[name]--
gamestate.Player.Mana += spells[name].ManaRecharge
}
}
}
func checkEndEffects(gamestate *GameState) {
for name, turns := range gamestate.onGoingStatusEffects {
if turns == 0 {
delete(gamestate.onGoingStatusEffects, name)
if name == "Shield" {
gamestate.Player.Armor = 0
}
}
}
}
func createSpells() map[string]Spell {
var spellsMap = map[string]Spell{
"Magic Missile": {
Name: "Magic Missile",
Cost: 53,
Damage: 4,
},
"Drain": {
Name: "Drain",
Cost: 73,
Damage: 2,
Heal: 2,
},
"Shield": {
Name: "Shield",
Cost: 113,
Turns: 6,
Armor: 7,
},
"Poison": {
Name: "Poison",
Cost: 173,
Turns: 6,
TickDamage: 3,
},
"Recharge": {
Name: "Recharge",
Cost: 229,
Turns: 5,
ManaRecharge: 101,
},
}
return spellsMap
}
// The priority of each game state will be given by its mana spent
func byPriority(a, b interface{}) int {
priorityA := a.(GameState).ManaSpent
priorityB := b.(GameState).ManaSpent
return utils.IntComparator(priorityA, priorityB) // "-" descending order
}
func deepCopy(gs GameState) GameState {
return GameState{
Boss: deepCopyPlayer(gs.Boss),
Player: deepCopyPlayer(gs.Player),
ManaSpent: gs.ManaSpent,
Turn: gs.Turn,
isFinished: gs.isFinished,
onGoingStatusEffects: maps.Clone(gs.onGoingStatusEffects),
}
}
func deepCopyPlayer(p Player) Player {
return Player{
Name: p.Name,
Hitpoints: p.Hitpoints,
Damage: p.Damage,
Mana: p.Mana,
Armor: p.Armor,
}
}