-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (45 loc) · 857 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"strconv"
"strings"
"github.com/devkvlt/aoc"
)
var stones = strings.Fields(aoc.Lines("input")[0])
func trim(stone string) string {
for len(stone) > 1 && stone[0] == '0' {
stone = stone[1:]
}
return stone
}
var cache = map[string]int{}
func count(stone string, n int) int {
if n == 0 {
return 1
}
key := fmt.Sprintf("%s:%d", stone, n)
c, ok := cache[key]
if ok {
return c
}
if stone == "0" {
c = count("1", n-1)
} else if l := len(stone); l%2 == 0 {
c = count(stone[:l/2], n-1) + count(trim(stone[l/2:]), n-1)
} else {
c = count(strconv.Itoa(aoc.Atoi(stone)*2024), n-1)
}
cache[key] = c
return c
}
func blink(n int) {
sum := 0
for _, stone := range stones {
sum += count(stone, n)
}
fmt.Println(sum)
}
func main() {
blink(25) // Part 1: 183435
blink(75) // Part 2: 218279375708592
}