Skip to content

Commit bde2daf

Browse files
committed
2024-Day11
1 parent 9320da3 commit bde2daf

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

2024/day11/.bench

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Lines":[{"Name":"Part 1","N":1000000000,"NsPerOp":0.5847,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0},{"Name":"Part 2","N":1000000000,"NsPerOp":0.5906,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0}],"Measured":1}

2024/day11/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- You can add some comments here if you want to :D -->
2+
For a change, with 2024 Day11 I did not need to make use of a different code set for part 2, and could re-use the code from part 1.

2024/day11/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"aocli/utils"
5+
_ "embed"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
//go:embed input.txt
12+
var input string
13+
14+
//go:embed input_test.txt
15+
var inputTest string
16+
17+
type combo struct {
18+
num, count int
19+
}
20+
21+
var seen = make(map[combo]int, 0)
22+
var nums = []int{}
23+
24+
func main() {
25+
// Check argv if we use test input or not
26+
if len(os.Args) > 1 && os.Args[1] == "test" {
27+
input = inputTest
28+
}
29+
30+
input = strings.ReplaceAll(input, "\n", "")
31+
for _, n := range strings.Split(input, " ") {
32+
nums = append(nums, utils.Atoi(n))
33+
}
34+
35+
answer := solveAll(nums, 25)
36+
println(answer)
37+
38+
answer = solveAll(nums, 75)
39+
println(answer)
40+
}
41+
42+
func solveAll(nums []int, count int) int {
43+
var res int
44+
for _, n := range nums {
45+
res += solve(n, count)
46+
}
47+
return res
48+
}
49+
50+
func solve(num int, count int) int {
51+
if _, ok := seen[combo{num, count}]; ok {
52+
return seen[combo{num, count}]
53+
}
54+
if count == 0 {
55+
return 1
56+
}
57+
if num == 0 {
58+
return solve(1, count-1)
59+
}
60+
s := strconv.Itoa(num)
61+
if len(s)%2 == 0 {
62+
return solve(utils.Atoi(s[:len(s)/2]), count-1) + solve(utils.Atoi(s[len(s)/2:]), count-1)
63+
}
64+
res := solve(num*2024, count-1)
65+
seen[combo{num, count}] = res
66+
return res
67+
}

2024/day11/main_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "testing"
4+
5+
func BenchmarkPartOne(b *testing.B) {
6+
for n := 0; n < b.N; n++ {
7+
solveAll(nums, 25)
8+
}
9+
}
10+
11+
func BenchmarkPartTwo(b *testing.B) {
12+
for n := 0; n < b.N; n++ {
13+
solveAll(nums, 75)
14+
}
15+
}

0 commit comments

Comments
 (0)