aoc2024/day-07/internal/one/solve.go

63 lines
1014 B
Go
Raw Normal View History

2024-12-07 23:39:28 +00:00
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
equations, err := parseLines(r)
if err != nil {
return 0, err
}
conc := len(equations)
sumChan := make(chan int)
for _, equation := range equations {
go func() {
var total int
2025-01-07 17:23:33 +00:00
res, _ := next(equation.target, equation.operands, total, sumOp)
sumChan <- res
2024-12-07 23:39:28 +00:00
}()
}
var sum int
for range conc {
2025-01-07 17:23:33 +00:00
sum += <-sumChan
2024-12-07 23:39:28 +00:00
}
return sum, nil
}
func next(target int, operands []int, total int, operator string) (int, bool) {
if total > target {
return 0, false
}
2024-12-07 23:39:28 +00:00
if len(operands) == 0 {
if total == target {
log.Debug(total)
return total, true
}
return 0, false
}
switch operator {
case sumOp:
total += operands[0]
case prodOp:
total *= operands[0]
}
if res, ok := next(target, operands[1:], total, sumOp); ok {
return res, ok
}
if res, ok := next(target, operands[1:], total, prodOp); ok {
return res, ok
}
return 0, false
}