adventofcode/2018/day24/day24.go

85 lines
1.2 KiB
Go
Raw Normal View History

2018-12-16 07:26:17 +00:00
package main
2019-11-07 23:20:20 +00:00
import (
"bufio"
"fmt"
"os"
)
2018-12-16 07:26:17 +00:00
2019-11-07 23:20:20 +00:00
const (
2019-11-08 17:52:19 +00:00
Debug = true
2019-11-07 23:20:20 +00:00
)
2018-12-16 07:26:17 +00:00
func main() {
2019-11-07 23:20:20 +00:00
inp := StdinToStringSlice()
2019-11-08 18:40:42 +00:00
fmt.Println(ConditionFight(inp))
fmt.Println(ImmuneSystemBoost(inp))
}
func ConditionFight(input []string) int {
battle, initiative := PrepareForBattle(input)
2019-11-08 17:52:19 +00:00
2019-11-08 18:40:42 +00:00
for battle.Active() {
battle.FindTargets()
initiative.Attack()
battle.Clean()
initiative.Clean()
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
_, units := battle.Result()
return units
2019-11-07 23:20:20 +00:00
}
2019-11-08 18:40:42 +00:00
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()
2019-11-07 23:20:20 +00:00
}
2019-11-08 18:40:42 +00:00
if !stalemate {
winner, units := battle.Result()
if winner == ArmyImmuneSystem {
return units
}
2019-11-07 23:20:20 +00:00
}
2019-11-08 18:40:42 +00:00
boost++
2019-11-07 23:20:20 +00:00
}
2018-12-16 07:26:17 +00:00
}
2019-11-07 23:20:20 +00:00
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
2018-12-16 07:26:17 +00:00
}
2019-11-08 18:40:42 +00:00
func ContainsString(l []string, t string) bool {
for _, s := range l {
if s == t {
return true
}
}
return false
}