aoc2024/day-11/internal/one/solve.go
onyx-and-iris 2dc1d494af add result struct to package one
remove unnecessary assignments in util.splitNumber
2024-12-12 02:53:33 +00:00

59 lines
963 B
Go

package one
import (
"bytes"
log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/aoc2024/day-11/internal/queue"
)
const blinks = 25
const magic = 2024
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
numbers, err := parseLines(r)
if err != nil {
return 0, err
}
old := queue.New[int]()
for _, n := range numbers {
old.Enqueue(n)
}
log.Debug(old)
for range blinks {
next := queue.New[int]()
for !old.IsEmpty() {
n := old.Dequeue()
r := applyRules(n)
switch r.kind {
case flip, multiply:
next.Enqueue(r.right)
case split:
next.Enqueue(r.left)
next.Enqueue(r.right)
}
}
old = next
log.Debug(old)
}
return old.Len(), nil
}
func applyRules(num int) result {
switch num := num; {
case num == 0:
return result{flip, 0, 1}
case lenItoa(num)%2 == 0:
left, right := splitNumber(num)
return result{split, left, right}
default:
return result{multiply, 0, num * magic}
}
}