adventofcode/2018/day24/combat.go

171 lines
3.8 KiB
Go
Raw Permalink Normal View History

2019-11-08 03:30:41 +00:00
package main
2019-11-08 17:52:19 +00:00
import (
"fmt"
"log"
2019-11-08 18:40:42 +00:00
"regexp"
2019-11-08 17:52:19 +00:00
"sort"
"strconv"
2019-11-08 18:40:42 +00:00
"strings"
2019-11-08 17:52:19 +00:00
)
2019-11-08 18:40:42 +00:00
type Battlefield map[int]Army
var (
armyName = regexp.MustCompile(`^(.*):$`)
groupImmunities = regexp.MustCompile(`immune to (.*?)[;)]`)
groupWeaknesses = regexp.MustCompile(`weak to (.*?)[;)]`)
groupDescription = regexp.MustCompile(`^(\d+) units each with (\d+) hit points.*with an attack that does (\d+) (\w+) damage at initiative (\d+)$`)
)
const (
descriptionCount = iota + 1
descriptionHitPoints
descriptionDamage
descriptionDamageType
descriptionInitiative
)
func PrepareForBattle(input []string) (Battlefield, Initiative) {
var initiative Initiative
battle := make(Battlefield)
var currentArmy int
2019-11-08 17:52:19 +00:00
2019-11-08 18:40:42 +00:00
for _, line := range input {
if armyName.MatchString(line) {
if id, ok := StringArmies[armyName.FindStringSubmatch(line)[1]]; ok {
currentArmy = id
} else {
panic(fmt.Errorf("unknown army: %s", armyName.FindStringSubmatch(line)[1]))
}
} else {
if currentArmy <= 0 || currentArmy >= ArmyCount {
panic(fmt.Errorf("tried to assign group to invalid army: %d", currentArmy))
}
description := groupDescription.FindStringSubmatch(line)
if len(description) == 0 {
continue
}
group := &Group{
Units: Atoi(description[descriptionCount]),
HitPoints: Atoi(description[descriptionHitPoints]),
AttackDamage: Atoi(description[descriptionDamage]),
2019-11-08 18:40:42 +00:00
AttackType: description[descriptionDamageType],
Initiative: Atoi(description[descriptionInitiative]),
2019-11-08 18:40:42 +00:00
}
immunities := groupImmunities.FindStringSubmatch(line)
if len(immunities) > 0 {
group.Immunities = strings.Split(immunities[1], ", ")
}
weaknesses := groupWeaknesses.FindStringSubmatch(line)
if len(weaknesses) > 0 {
group.Weaknesses = strings.Split(weaknesses[1], ", ")
}
battle[currentArmy] = append(battle[currentArmy], group)
initiative = append(initiative, group)
}
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
return battle, initiative
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
func (b Battlefield) FindTargets() {
for army, groups := range b {
sort.Sort(groups)
for _, group := range groups {
for enemyArmy, enemyGroups := range b {
if army == enemyArmy || group.Units <= 0 {
continue
}
var mostDamage int
var targetGroup *Group
for _, enemyGroup := range enemyGroups {
if enemyGroup.Units <= 0 || enemyGroup.Attacker != nil || group.DamageDealt(enemyGroup) == 0 || group.DamageDealt(enemyGroup) < mostDamage {
continue
}
if group.DamageDealt(enemyGroup) == mostDamage && targetGroup != nil {
if enemyGroup.EffectivePower() < targetGroup.EffectivePower() {
continue
}
if enemyGroup.EffectivePower() == targetGroup.EffectivePower() && enemyGroup.Initiative < targetGroup.Initiative {
continue
}
}
mostDamage = group.DamageDealt(enemyGroup)
targetGroup = enemyGroup
}
if targetGroup != nil {
group.Target = targetGroup
targetGroup.Attacker = group
}
}
}
2019-11-08 17:52:19 +00:00
}
}
2019-11-08 18:40:42 +00:00
func (b Battlefield) Clean() {
for army := range b {
c := b[army][:0]
for _, g := range b[army] {
if g.Units > 0 {
c = append(c, g)
}
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
b[army] = c
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
}
func (b Battlefield) Active() bool {
for _, a := range b {
if !a.Alive() {
return false
2019-11-08 17:52:19 +00:00
}
}
2019-11-08 18:40:42 +00:00
return true
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
func (b Battlefield) Result() (int, int) {
var winner, units int
for army, groups := range b {
if groups.Alive() {
winner = army
for _, g := range groups {
if g.Units > 0 {
units += g.Units
}
}
}
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
return winner, units
2019-11-08 17:52:19 +00:00
}
2019-11-08 18:40:42 +00:00
func (b Battlefield) TotalUnits() int {
var sum int
for _, groups := range b {
for _, group := range groups {
if group.Units > 0 {
sum += group.Units
}
}
}
return sum
2019-11-08 03:30:41 +00:00
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}