2018-12-16 07:26:17 +00:00
package main
2019-11-07 23:20:20 +00:00
import (
"bufio"
"fmt"
"os"
"strings"
)
2018-12-16 07:26:17 +00:00
2019-11-07 23:20:20 +00:00
const (
ArmyTypeImmune = 1 << iota
ArmyTypeInfection
)
2018-12-16 07:26:17 +00:00
func main ( ) {
2019-11-07 23:20:20 +00:00
inp := StdinToStringSlice ( )
immune , infect := ParseInput ( inp )
}
func ParseInput ( inp [ ] string ) ( [ ] Army , [ ] Army ) {
var immune , infection [ ] Army
var tp int
for _ , v := range inp {
if v == "" {
continue
}
if v == "Immune System:" {
tp = ArmyTypeImmune
continue
} else if v == "Infection:" {
tp = ArmyTypeInfection
continue
}
switch tp {
case ArmyTypeImmune :
immune = append ( immune , * NewArmy ( v , tp ) )
case ArmyTypeInfection :
infection = append ( infection , * NewArmy ( v , tp ) )
}
}
return immune , infection
2018-12-16 07:26:17 +00:00
}
type AttackType int
type Army struct {
2019-11-07 23:20:20 +00:00
tp int
2018-12-16 07:26:17 +00:00
units int
hp int
2019-11-07 23:20:20 +00:00
immunities [ ] string
2018-12-16 07:26:17 +00:00
weaknesses [ ] string
2019-11-07 23:20:20 +00:00
damageType string
2018-12-16 07:26:17 +00:00
strength int
2019-11-07 23:20:20 +00:00
init int
}
func NewArmy ( inp string , tp int ) * Army {
a := new ( Army )
a . tp = tp
// Pull the parenthetical out, if one is there
var prnth , other string
var inPrnth bool
var ptsOfOther int
for _ , v := range strings . Fields ( inp ) {
if len ( v ) > 0 && v [ len ( v ) - 1 ] == ')' {
prnth = prnth + " " + v
inPrnth = false
continue
} else if len ( v ) > 0 && v [ 0 ] == '(' {
inPrnth = true
prnth = v
continue
}
if inPrnth {
prnth = prnth + " " + v
} else {
if len ( other ) > 0 {
other = other + " "
}
other = other + v
ptsOfOther ++
}
}
_ , err := fmt . Sscanf ( other , "%d units each with %d hit points with an attack that does %d %s damage at initiative %d" , & a . units , & a . hp , & a . strength , & a . damageType , & a . init )
if err != nil {
panic ( err )
}
return a
}
func ( a * Army ) Power ( ) int {
return a . units * a . strength
}
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
}