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) visited := make(map[int]int) for stone, count := range old { var r result _, ok := visited[stone] if !ok { r = applyRules(stone) memo[stone] = r } visited[stone] += count switch r.kind { case flip, multiply: next[r.right] += visited[stone] case split: next[r.left] += visited[stone] next[r.right] += visited[stone] } } 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} } }