From d8e15a5d5199aef6f543ec47a3cad9379cc652bd Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 7 Nov 2019 17:20:20 -0600 Subject: [PATCH] 2018 Day 24 Work --- 2018/day24/day24.go | 93 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/2018/day24/day24.go b/2018/day24/day24.go index d514d9b..8c07e21 100644 --- a/2018/day24/day24.go +++ b/2018/day24/day24.go @@ -1,19 +1,104 @@ package main -import "fmt" +import ( + "bufio" + "fmt" + "os" + "strings" +) -const () +const ( + ArmyTypeImmune = 1 << iota + ArmyTypeInfection +) func main() { - fmt.Println("vim-go") + inp := StdinToStringSlice() + immune, infect := ParseInput(inp) + +} + +func ParseInput(inp []string) ([]Army, []Army) { + var immune, infection []Army + var tp int + for _, v := range inp { + if v == "" { + continue + } + if v == "Immune System:" { + tp = ArmyTypeImmune + continue + } else if v == "Infection:" { + tp = ArmyTypeInfection + continue + } + switch tp { + case ArmyTypeImmune: + immune = append(immune, *NewArmy(v, tp)) + case ArmyTypeInfection: + infection = append(infection, *NewArmy(v, tp)) + } + } + return immune, infection } type AttackType int type Army struct { + tp int units int hp int + immunities []string weaknesses []string + damageType string strength int - dam + init int +} + +func NewArmy(inp string, tp int) *Army { + a := new(Army) + a.tp = tp + + // Pull the parenthetical out, if one is there + var prnth, other string + var inPrnth bool + var ptsOfOther int + for _, v := range strings.Fields(inp) { + if len(v) > 0 && v[len(v)-1] == ')' { + prnth = prnth + " " + v + inPrnth = false + continue + } else if len(v) > 0 && v[0] == '(' { + inPrnth = true + prnth = v + continue + } + if inPrnth { + prnth = prnth + " " + v + } else { + if len(other) > 0 { + other = other + " " + } + other = other + v + ptsOfOther++ + } + } + _, err := fmt.Sscanf(other, "%d units each with %d hit points with an attack that does %d %s damage at initiative %d", &a.units, &a.hp, &a.strength, &a.damageType, &a.init) + if err != nil { + panic(err) + } + return a +} + +func (a *Army) Power() int { + return a.units * a.strength +} + +func StdinToStringSlice() []string { + var input []string + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + input = append(input, scanner.Text()) + } + return input }