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

59 lines
963 B
Go
Raw Permalink 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()
r := applyRules(n)
switch r.kind {
case flip, multiply:
next.Enqueue(r.right)
case split:
next.Enqueue(r.left)
next.Enqueue(r.right)
2024-12-11 19:26:56 +00:00
}
}
old = next
log.Debug(old)
}
return old.Len(), nil
}
func applyRules(num int) result {
2024-12-11 19:26:56 +00:00
switch num := num; {
case num == 0:
return result{flip, 0, 1}
2024-12-11 19:26:56 +00:00
case lenItoa(num)%2 == 0:
left, right := splitNumber(num)
return result{split, left, right}
2024-12-11 19:26:56 +00:00
default:
return result{multiply, 0, num * magic}
2024-12-11 19:26:56 +00:00
}
}