}
func solveSecond(in []string) int {
var res int
for _, st := range in {
var p packet
p.parsePacket(st)
res = p.evaluate()
}
return res
}
func (p packet) evaluate() int {
switch p.typeID {
case 0: // sum
var sum int
for _, subs := range p.subs {
sum += subs.evaluate()
}
return sum
case 1: // product
var prod = 1
for _, subs := range p.subs {
prod *= subs.evaluate()
}
return prod
case 2: // min
var min int = math.MaxInt
for _, subs := range p.subs {
if subs.evaluate() < min {
min = subs.evaluate()
}
}
return min
case 3: // max
var max int
for _, subs := range p.subs {
if subs.evaluate() > max {
max = subs.evaluate()
}
}
return max
case 4: // literal
return int(p.number)
case 5: // greater
if p.subs[0].evaluate() > p.subs[1].evaluate() {
return 1
}
return 0
case 6: // less
if p.subs[0].evaluate() < p.subs[1].evaluate() {
return 1
}
return 0
case 7: // equal
if p.subs[0].evaluate() == p.subs[1].evaluate() {
return 1
}
return 0
default:
log.Panicf("unknown typeID: %d", p.typeID)
return 0
}