2018 Day 24 Done
This commit is contained in:
@@ -12,45 +12,57 @@ const (
|
||||
|
||||
func main() {
|
||||
inp := StdinToStringSlice()
|
||||
immune, infect := ParseInput(inp)
|
||||
|
||||
b := NewBattle(immune, infect)
|
||||
//for !b.IsOver() {
|
||||
if Debug {
|
||||
b.PrintStatus()
|
||||
fmt.Println("")
|
||||
}
|
||||
b.Fight()
|
||||
//}
|
||||
|
||||
fmt.Println("")
|
||||
fmt.Println("The battle is over!")
|
||||
b.PrintStatus()
|
||||
fmt.Println(ConditionFight(inp))
|
||||
fmt.Println(ImmuneSystemBoost(inp))
|
||||
}
|
||||
|
||||
func ParseInput(inp []string) ([]*Army, []*Army) {
|
||||
var immune, infection []*Army
|
||||
var tp, id int
|
||||
for _, v := range inp {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if v == "Immune System:" {
|
||||
tp, id = ArmyTypeImmune, 0
|
||||
continue
|
||||
} else if v == "Infection:" {
|
||||
tp, id = ArmyTypeInfection, 0
|
||||
continue
|
||||
}
|
||||
id++
|
||||
switch tp {
|
||||
case ArmyTypeImmune:
|
||||
immune = append(immune, NewArmy(v, tp, id))
|
||||
case ArmyTypeInfection:
|
||||
infection = append(infection, NewArmy(v, tp, id))
|
||||
}
|
||||
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++
|
||||
}
|
||||
return immune, infection
|
||||
}
|
||||
|
||||
func StdinToStringSlice() []string {
|
||||
@@ -61,3 +73,12 @@ func StdinToStringSlice() []string {
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func ContainsString(l []string, t string) bool {
|
||||
for _, s := range l {
|
||||
if s == t {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user