structs get *not* handed over do funcs as references (good!)

This commit is contained in:
Stefan Harmuth 2021-01-14 08:09:53 +01:00
parent f67d5f141c
commit cffbd23a11
3 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package day22 package day22
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"tools" "tools"
@ -36,15 +37,19 @@ func (e Entity) copy() Entity {
return Entity{e.hitpoints, e.mana, e.damage, e.armor, e.currentEffects} return Entity{e.hitpoints, e.mana, e.damage, e.armor, e.currentEffects}
} }
func (e Entity) printStats() {
fmt.Println("HP:", e.hitpoints, "Mana:", e.mana, "Damage:", e.damage, "Armor:", e.armor)
}
var spells = map[string]Spell{ var spells = map[string]Spell{
"Magic Missile": {53, 4, 0, nil}, "Magic Missile": {53, 4, 0, Effect{}},
"Drain": {73, 2, 2, nil}, "Drain": {73, 2, 2, Effect{}},
"Shield": {113, 0, 0, Effect{0, 7, 0, 6}}, "Shield": {113, 0, 0, Effect{0, 7, 0, 6}},
"Poison": {173, 0, 0, Effect{3, 0, 0, 6}}, "Poison": {173, 0, 0, Effect{3, 0, 0, 6}},
"Recharge": {229, 0, 0, Effect{0, 0, 101, 5}}, "Recharge": {229, 0, 0, Effect{0, 0, 101, 5}},
} }
func applyEffects(player, boss Entity) { func applyEffects(player, boss *Entity) {
for eName, eData := range player.currentEffects { for eName, eData := range player.currentEffects {
switch eName { switch eName {
case "Shield": case "Shield":
@ -67,7 +72,7 @@ func applyEffects(player, boss Entity) {
} }
} }
func fightRound(player, boss Entity, spell Spell) (outcome int) { func fightRound(player, boss *Entity, spell Spell) (outcome int) {
// outcome will be -1 on boss win, 0 on both living and 1 on player win // outcome will be -1 on boss win, 0 on both living and 1 on player win
applyEffects(player, boss) applyEffects(player, boss)
@ -102,9 +107,15 @@ func Part1(puzzle tools.AoCPuzzle) interface{} {
case "Hit Points": case "Hit Points":
boss.hitpoints, _ = strconv.Atoi(parts[1]) boss.hitpoints, _ = strconv.Atoi(parts[1])
case "Damage": case "Damage":
boss.damage, _ = strconv.Atoi(parts[0]) boss.damage, _ = strconv.Atoi(parts[1])
} }
} }
player.printStats()
boss.printStats()
fightRound(&player, &boss, spells["Magic Missile"])
player.printStats()
boss.printStats()
return 0 return 0
} }

2
inputs/22 Normal file
View File

@ -0,0 +1,2 @@
Hit Points: 55
Damage: 8

2
inputs/22_test Normal file
View File

@ -0,0 +1,2 @@
Hit Points: 55
Damage: 8