aoc2024/day-11/internal/two/solve.go
2024-12-11 19:40:45 +00:00

69 lines
1.1 KiB
Go

package two
import (
"bytes"
log "github.com/sirupsen/logrus"
)
const blinks = 75
const magic = 2024
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
numbers, err := parseLines(r)
if err != nil {
return 0, err
}
old := make(map[int]int)
for _, n := range numbers {
old[n]++
}
log.Debug(old)
memo := make(map[int]result)
for range blinks {
next := make(map[int]int)
for stone, count := range old {
var r result
r, ok := memo[stone]
if !ok {
r = applyRules(stone)
memo[stone] = r
}
switch r.kind {
case flip, multiply:
next[r.right] += count
case split:
next[r.left] += count
next[r.right] += count
}
}
old = next
log.Debug(old)
}
var totalCount int
for _, count := range old {
totalCount += count
}
return totalCount, 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}
}
}