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

56 lines
882 B
Go
Raw Normal View History

2024-12-11 19:26:56 +00:00
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()
left, right, ok := applyRules(n)
if ok {
next.Enqueue(left)
}
next.Enqueue(right)
}
old = next
log.Debug(old)
}
return old.Len(), nil
}
func applyRules(num int) (int, int, bool) {
switch num := num; {
case num == 0:
return 0, 1, false
case lenItoa(num)%2 == 0:
left, right := splitNumber(num)
return left, right, true
default:
return 0, num * magic, false
}
}