Skip to content

Commit 687063d

Browse files
committed
feat(2025): add day 11 and bench day 10
1 parent 32002ba commit 687063d

5 files changed

Lines changed: 163 additions & 11 deletions

File tree

go/2025/README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,26 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
2727
| [Day 7: Laboratories](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day07/main.go) | 🌟 | 1711 | 🌟 | 36706966158365 |
2828
| [Day 8: Playground](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day08/main.go) | 🌟 | 69192 | 🌟 | 7264308110 |
2929
| [Day 9: Movie Theater](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day09/main.go) | 🌟 | 4772103936 | 🌟 | 1529675217 |
30+
| [Day 10: Factory](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day10/main.go) | 🌟 | 455 | | |
31+
| [Day 11: Reactor](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day11/main.go) | 🌟 | 431 | 🌟 | 358458157650450 |
3032

3133
## Benchmarks
3234

3335
Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#hdr-Benchmarks) package. Computer is a 2021 MacBook Pro M1 Pro, 32 GB RAM.
3436

35-
| Day | #1 | #2 | Improvement\* |
36-
| --- | -------: | --------: | -------------------- |
37-
| 1 | 340532 | 338234 | `52,30%` / `52,86%` |
38-
| 2 | 39771972 | 778769562 | `-14,63%` / `46,44%` |
39-
| 3 | 209025 | 276642 | `5,57%` |
40-
| 4 | 214451 | 5483916 | |
41-
| 5 | 116258 | 68155 | `96,29%` / - |
42-
| 6 | 149919 | 986335 | `30,99%` / - |
43-
| 7 | 671799 | 508323 | |
44-
| 8 | 66756016 | 64329155 | |
45-
| 9 | 593637 | 375621306 | - / `90,43%` |
37+
| Day | #1 | #2 | Improvement\* |
38+
| --- | --------: | --------: | -------------------- |
39+
| 1 | 340532 | 338234 | `52,30%` / `52,86%` |
40+
| 2 | 39771972 | 778769562 | `-14,63%` / `46,44%` |
41+
| 3 | 209025 | 276642 | `5,57%` |
42+
| 4 | 214451 | 5483916 | |
43+
| 5 | 116258 | 68155 | `96,29%` / - |
44+
| 6 | 149919 | 986335 | `30,99%` / - |
45+
| 7 | 671799 | 508323 | |
46+
| 8 | 66756016 | 64329155 | |
47+
| 9 | 593637 | 375621306 | - / `90,43%` |
48+
| 10 | 256756802 | - | |
49+
| 11 | 106015 | 311170 | |
4650

4751
All values are ns/op. \* compared to first solution.
4852

go/2025/puzzles/day11/main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/believer/aoc-utils/files"
8+
)
9+
10+
// Pretty simple DP solution. Nice breather after yesterday.
11+
// The thing I didn't know was how to create a good cache
12+
// representation for multiple values and recursion. So, I
13+
// (actually) asked Gemini about how to solve that part.
14+
15+
func main() {
16+
fmt.Println("Part 1: ", part1("input.txt"))
17+
fmt.Println("Part 2: ", part2("input.txt"))
18+
}
19+
20+
func part1(name string) int {
21+
paths := createPaths(name)
22+
23+
// DAC and FFT are not needed in part 1, set to true
24+
return countPaths(paths, "you", true, true)
25+
}
26+
27+
func part2(name string) int {
28+
paths := createPaths(name)
29+
30+
return countPaths(paths, "svr", false, false)
31+
}
32+
33+
type cacheKey struct {
34+
from string
35+
dac bool
36+
fft bool
37+
}
38+
39+
func countPaths(paths map[string][]string, start string, dac, fft bool) int {
40+
cache := make(map[cacheKey]int)
41+
42+
var solve func(from string, dac, fft bool) int
43+
44+
solve = func(from string, dac, fft bool) int {
45+
key := cacheKey{from, dac, fft}
46+
47+
if result, ok := cache[key]; ok {
48+
return result
49+
}
50+
51+
switch from {
52+
case "out":
53+
// Has visited both DAC and FFT
54+
if dac && fft {
55+
return 1
56+
}
57+
return 0
58+
case "dac":
59+
dac = true
60+
case "fft":
61+
fft = true
62+
}
63+
64+
sum := 0
65+
66+
for _, to := range paths[from] {
67+
sum += solve(to, dac, fft)
68+
}
69+
70+
cache[key] = sum
71+
72+
return sum
73+
}
74+
75+
return solve(start, dac, fft)
76+
}
77+
78+
// Create a map of all devices and their outputs
79+
func createPaths(name string) map[string][]string {
80+
lines := files.ReadLines(name)
81+
paths := map[string][]string{}
82+
83+
for _, l := range lines {
84+
device, outputs, _ := strings.Cut(l, ": ")
85+
86+
paths[device] = strings.Fields(outputs)
87+
}
88+
89+
return paths
90+
}

go/2025/puzzles/day11/main_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPart1(t *testing.T) {
10+
t.Run("Part 1", func(t *testing.T) {
11+
expected := 5
12+
actual := part1("test-input.txt")
13+
assert.Equal(t, expected, actual)
14+
})
15+
}
16+
17+
func TestPart2(t *testing.T) {
18+
t.Run("Part 2", func(t *testing.T) {
19+
expected := 2
20+
actual := part2("test-input-part2.txt")
21+
assert.Equal(t, expected, actual)
22+
})
23+
}
24+
25+
func BenchmarkPart1(b *testing.B) {
26+
for b.Loop() {
27+
part1("input.txt")
28+
}
29+
}
30+
31+
func BenchmarkPart2(b *testing.B) {
32+
for b.Loop() {
33+
part2("input.txt")
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
svr: aaa bbb
2+
aaa: fft
3+
fft: ccc
4+
bbb: tty
5+
tty: ccc
6+
ccc: ddd eee
7+
ddd: hub
8+
hub: fff
9+
eee: dac
10+
dac: fff
11+
fff: ggg hhh
12+
ggg: out
13+
hhh: out
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aaa: you hhh
2+
you: bbb ccc
3+
bbb: ddd eee
4+
ccc: ddd eee fff
5+
ddd: ggg
6+
eee: out
7+
fff: out
8+
ggg: out
9+
hhh: ccc fff iii
10+
iii: out

0 commit comments

Comments
 (0)