Skip to content

Commit 1131bdd

Browse files
committed
2024.11 Part 2
1 parent 8129aaa commit 1131bdd

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

2024/11-pebbles/main.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,40 @@ import (
99
func main() {
1010
stones := util.StringToInts(util.ReadFileFromArgs(), " ")
1111

12-
for i := 0; i < 25; i++ {
13-
stones = blink(stones)
14-
fmt.Println(len(stones))
12+
pebbles := make(map[int]int, len(stones))
13+
for i := range stones {
14+
pebbles[stones[i]] += 1
15+
}
16+
17+
for i := 0; i < 75; i++ {
18+
pebbles = blink(pebbles)
1519
}
1620

17-
fmt.Println(len(stones))
21+
var count int
22+
for i := range pebbles {
23+
count += pebbles[i]
24+
}
25+
26+
fmt.Println(count)
1827
}
1928

20-
func blink(stones []int) []int {
21-
result := make([]int, 0, len(stones))
29+
func blink(stones map[int]int) map[int]int {
30+
pebbles := map[int]int{}
2231
var l, left, mod int
2332

24-
for i := range stones {
25-
if stones[i] == 0 {
26-
result = append(result, 1)
27-
} else if l = util.IntLength(stones[i]); l%2 == 0 {
33+
for k, v := range stones {
34+
if k == 0 {
35+
pebbles[1] += v
36+
} else if l = util.IntLength(k); l%2 == 0 {
2837
mod = int(math.Pow(10, float64(l/2)))
29-
left = int(stones[i] / mod)
30-
result = append(result, left)
31-
result = append(result, stones[i]-left*mod)
38+
left = int(k / mod)
39+
40+
pebbles[left] += v
41+
pebbles[k-left*mod] += v
3242
} else {
33-
result = append(result, stones[i]*2024)
43+
pebbles[k*2024] += v
3444
}
3545
}
3646

37-
return result
47+
return pebbles
3848
}

util/math.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ func Abs[I constraints.Integer](v I) I {
1010
return v
1111
}
1212

13-
func IntLength(v int) int {
14-
x, count := 10, 1
13+
func IntLength[I constraints.Integer](v I) int {
14+
var x I = 10
15+
count := 1
1516

1617
for x <= v {
1718
x *= 10
@@ -21,7 +22,7 @@ func IntLength(v int) int {
2122
return count
2223
}
2324

24-
func IntPow(n, m int) int {
25+
func IntPow[I constraints.Integer](n, m I) I {
2526
if m == 0 {
2627
return 1
2728
}
@@ -31,7 +32,8 @@ func IntPow(n, m int) int {
3132
}
3233

3334
result := n
34-
for i := 2; i <= m; i++ {
35+
var i I
36+
for i = 2; i <= m; i++ {
3537
result *= n
3638
}
3739
return result

0 commit comments

Comments
 (0)