From cffbd23a11b4999a6d56ec623572ba11d7198889 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Thu, 14 Jan 2021 08:09:53 +0100 Subject: [PATCH] structs get *not* handed over do funcs as references (good!) --- day22/day.go | 21 ++++++++++++++++----- inputs/22 | 2 ++ inputs/22_test | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 inputs/22 create mode 100644 inputs/22_test diff --git a/day22/day.go b/day22/day.go index e06a042..7448ce6 100644 --- a/day22/day.go +++ b/day22/day.go @@ -1,6 +1,7 @@ package day22 import ( + "fmt" "strconv" "strings" "tools" @@ -36,15 +37,19 @@ func (e Entity) copy() Entity { 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{ - "Magic Missile": {53, 4, 0, nil}, - "Drain": {73, 2, 2, nil}, + "Magic Missile": {53, 4, 0, Effect{}}, + "Drain": {73, 2, 2, Effect{}}, "Shield": {113, 0, 0, Effect{0, 7, 0, 6}}, "Poison": {173, 0, 0, Effect{3, 0, 0, 6}}, "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 { switch eName { 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 applyEffects(player, boss) @@ -102,9 +107,15 @@ func Part1(puzzle tools.AoCPuzzle) interface{} { case "Hit Points": boss.hitpoints, _ = strconv.Atoi(parts[1]) 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 } diff --git a/inputs/22 b/inputs/22 new file mode 100644 index 0000000..02c2d08 --- /dev/null +++ b/inputs/22 @@ -0,0 +1,2 @@ +Hit Points: 55 +Damage: 8 \ No newline at end of file diff --git a/inputs/22_test b/inputs/22_test new file mode 100644 index 0000000..02c2d08 --- /dev/null +++ b/inputs/22_test @@ -0,0 +1,2 @@ +Hit Points: 55 +Damage: 8 \ No newline at end of file