mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
59 lines
963 B
Go
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}
|
|
}
|
|
}
|