File tree Expand file tree Collapse file tree 5 files changed +122
-0
lines changed
Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ input :
2+ http " https://adventofcode.com/2024/day/11/input" " Cookie:session=${AOC_SESSION} ;" > input
3+
4+ main1 :
5+ go build -o main1 main1.go common.go utils.go
6+
7+ main2 :
8+ go build -o main2 main2.go common.go utils.go
9+
10+ .PHONY : run1 run2 clean
11+
12+ run1 : main1 input
13+ ./main1 < input
14+
15+ run2 : main2 input
16+ ./main2 < input
17+
18+ clean :
19+ rm -f main1 main2 input
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func main () {
6+ sum := 0
7+ for _ , num := range readInput () {
8+ sum += numChildren (num , Blinks )
9+ }
10+ fmt .Println (sum )
11+ }
12+
13+ func blink (n int ) []int {
14+ if n == 0 {
15+ return []int {1 }
16+ }
17+
18+ digits := numDigits (n )
19+ if digits & 1 == 0 {
20+ pow := intPow (10 , digits / 2 )
21+ return []int {n / pow , n % pow }
22+ }
23+
24+ return []int {n * 2024 }
25+ }
26+
27+ // table for memoization
28+ var cache = map [args ]int {}
29+
30+ func numChildren (n , blinks int ) int {
31+
32+ if res , ok := cache [args {n , blinks }]; ok {
33+ return res
34+ }
35+
36+ if blinks == 0 {
37+ return 1
38+ }
39+
40+ sum := 0
41+ for _ , child := range blink (n ) {
42+ sum += numChildren (child , blinks - 1 )
43+ }
44+
45+ cache [args {n , blinks }] = sum
46+ return sum
47+ }
48+
49+ type args struct { a , b int }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ const Blinks = 25
Original file line number Diff line number Diff line change 1+ package main
2+
3+ const Blinks = 75
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "bufio"
5+ "os"
6+ "strconv"
7+ "strings"
8+ )
9+
10+ func numDigits (n int ) int {
11+ if n == 0 {
12+ return 1
13+ }
14+
15+ count := 0
16+ for n != 0 {
17+ n /= 10
18+ count ++
19+ }
20+ return count
21+ }
22+
23+ func intPow (n , m int ) int {
24+ if m == 0 {
25+ return 1
26+ }
27+
28+ if m == 1 {
29+ return n
30+ }
31+
32+ result := n
33+ for i := 2 ; i <= m ; i ++ {
34+ result *= n
35+ }
36+ return result
37+ }
38+
39+ func readInput () []int {
40+ scanner := bufio .NewScanner (os .Stdin )
41+ scanner .Scan ()
42+ nums := []int {}
43+ for _ , num := range strings .Fields (scanner .Text ()) {
44+ n , _ := strconv .Atoi (num )
45+ nums = append (nums , n )
46+ }
47+ return nums
48+ }
You can’t perform that action at this time.
0 commit comments