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