adventofcode/2018/day24/day24.go
2019-11-08 12:40:42 -06:00

85 lines
1.2 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
)
const (
Debug = true
)
func main() {
inp := StdinToStringSlice()
fmt.Println(ConditionFight(inp))
fmt.Println(ImmuneSystemBoost(inp))
}
func ConditionFight(input []string) int {
battle, initiative := PrepareForBattle(input)
for battle.Active() {
battle.FindTargets()
initiative.Attack()
battle.Clean()
initiative.Clean()
}
_, units := battle.Result()
return units
}
func ImmuneSystemBoost(input []string) int {
var boost int
for {
var stalemate bool
battle, initiative := PrepareForBattle(input)
battle[ArmyImmuneSystem].Boost(boost)
for battle.Active() {
before := battle.TotalUnits()
battle.FindTargets()
initiative.Attack()
if battle.TotalUnits() == before {
stalemate = true
break
}
battle.Clean()
initiative.Clean()
}
if !stalemate {
winner, units := battle.Result()
if winner == ArmyImmuneSystem {
return units
}
}
boost++
}
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func ContainsString(l []string, t string) bool {
for _, s := range l {
if s == t {
return true
}
}
return false
}