adventofcode/2018/day24/group.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-11-08 18:40:42 +00:00
package main
import (
"fmt"
"strings"
)
type Group struct {
Units int
HitPoints int
AttackDamage int
AttackType string
Initiative int
Immunities []string
Weaknesses []string
Attacker *Group
Target *Group
}
func (g Group) EffectivePower() int {
return g.Units * g.AttackDamage
}
func (g Group) DamageDealt(e *Group) int {
if ContainsString(e.Immunities, g.AttackType) {
return 0
}
if ContainsString(e.Weaknesses, g.AttackType) {
return g.EffectivePower() * 2
}
return g.EffectivePower()
}
func (g Group) String() string {
out := fmt.Sprintf("%d units each with %d hit points", g.Units, g.HitPoints)
if len(g.Immunities) > 0 || len(g.Weaknesses) > 0 {
out += " ("
if len(g.Immunities) > 0 {
out += "immune to " + strings.Join(g.Immunities, " and ")
if len(g.Weaknesses) > 0 {
out += "; "
}
}
if len(g.Weaknesses) > 0 {
out += "weak to " + strings.Join(g.Weaknesses, " and ")
}
out += ")"
}
out += fmt.Sprintf(" with an attack that does %d %s damage at initiative %d", g.AttackDamage, g.AttackType, g.Initiative)
return out
}