Skip to content

Commit 44468be

Browse files
committed
feat(2025): add day 6
1 parent 5028852 commit 44468be

File tree

4 files changed

+181
-7
lines changed

4 files changed

+181
-7
lines changed

go/2025/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
1616

1717
## Days
1818

19-
| Day | #1 | #1 Answer | #2 | #2 Answer |
20-
| ------------------------------------------------------------------------------------------------------------------ | --- | ---------: | --- | --------------: |
21-
| [Day 1: Secret Entrance](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day01/main.go) | 🌟 | 997 | 🌟 | 5978 |
22-
| [Day 2: Gift Shop](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day02/main.go) | 🌟 | 9188031749 | 🌟 | 11323661261 |
23-
| [Day 3: Lobby](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day03/main.go) | 🌟 | 16993 | 🌟 | 168617068915447 |
24-
| [Day 4: Printing Department](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day04/main.go) | 🌟 | 1320 | 🌟 | 8354 |
25-
| [Day 5: Cafeteria](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day05/main.go) | 🌟 | 885 | 🌟 | 348115621205535 |
19+
| Day | #1 | #1 Answer | #2 | #2 Answer |
20+
| ------------------------------------------------------------------------------------------------------------------ | --- | ------------: | --- | --------------: |
21+
| [Day 1: Secret Entrance](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day01/main.go) | 🌟 | 997 | 🌟 | 5978 |
22+
| [Day 2: Gift Shop](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day02/main.go) | 🌟 | 9188031749 | 🌟 | 11323661261 |
23+
| [Day 3: Lobby](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day03/main.go) | 🌟 | 16993 | 🌟 | 168617068915447 |
24+
| [Day 4: Printing Department](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day04/main.go) | 🌟 | 1320 | 🌟 | 8354 |
25+
| [Day 5: Cafeteria](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day05/main.go) | 🌟 | 885 | 🌟 | 348115621205535 |
26+
| [Day 6: Trash Compactor](https://github.com/believer/advent-of-code/blob/master/go/2025/puzzles/day06/main.go) | 🌟 | 6209956042374 | 🌟 | 12608160008022 |
2627

2728
## Benchmarks
2829

@@ -35,6 +36,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
3536
| 3 | 209025 | 276642 | `5,57%` |
3637
| 4 | 214451 | 5483916 | |
3738
| 5 | 116258 | 68155 | `96,29%` / - |
39+
| 6 | 217234 | 986335 | |
3840

3941
\* compared to first solution
4042

go/2025/puzzles/day06/main.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"strings"
7+
8+
"github.com/believer/aoc-utils/files"
9+
"github.com/believer/aoc-utils/utils"
10+
)
11+
12+
// Holy crap did I overcomplicate part 2. I had so many loops just
13+
// trying to get the values in the correct order. The spacing really
14+
// threw me off. I looked into potentially getting values using
15+
// regex, but no. Started over from scratch and tried just getting
16+
// the values column by column. Once I did that it turned
17+
// out it wasn't so hard after all...
18+
//
19+
// Part 1 is also a bit of a mess :')
20+
//
21+
// Performance is good enough to never going back and trying
22+
// to improve this problem again.
23+
24+
func main() {
25+
fmt.Println("Part 1: ", part1("input.txt"))
26+
fmt.Println("Part 2: ", part2("input.txt"))
27+
}
28+
29+
func part1(name string) (grandTotal int) {
30+
lines := files.ReadLines(name)
31+
problems := [][]int{}
32+
signs := []string{}
33+
data := [][]string{}
34+
35+
last := len(lines) - 1
36+
for l := range strings.SplitSeq(lines[last], " ") {
37+
if l == "" {
38+
continue
39+
}
40+
signs = append(signs, l)
41+
}
42+
43+
for _, l := range lines[:last] {
44+
inner := []string{}
45+
46+
for r := range strings.SplitSeq(l, " ") {
47+
if r == "" {
48+
continue
49+
}
50+
51+
inner = append(inner, r)
52+
}
53+
54+
data = append(data, inner)
55+
}
56+
57+
for range data[0] {
58+
problems = append(problems, make([]int, len(data)))
59+
}
60+
61+
for i, d := range data {
62+
for j, v := range d {
63+
problems[j][i] = utils.MustIntFromString(v)
64+
}
65+
}
66+
67+
for i, p := range problems {
68+
sign := signs[i]
69+
total := 0
70+
71+
for _, v := range p {
72+
switch sign {
73+
case "+":
74+
total += v
75+
case "*":
76+
if total == 0 {
77+
total = v
78+
} else {
79+
total *= v
80+
}
81+
}
82+
}
83+
84+
grandTotal += total
85+
}
86+
87+
return
88+
}
89+
90+
func part2(name string) (grandTotal int) {
91+
lines := files.ReadLines(name)
92+
cols := [][]string{}
93+
94+
// Transpose
95+
for i := range len(lines[0]) {
96+
col := []string{}
97+
98+
for _, line := range lines {
99+
col = append(col, string(line[i]))
100+
}
101+
102+
cols = append(cols, col)
103+
}
104+
105+
// Reverse to get operation on last line
106+
slices.Reverse(cols)
107+
current := []int{}
108+
109+
for _, v := range cols {
110+
last := len(v) - 1
111+
d := strings.TrimSpace(strings.Join(v[:last], ""))
112+
113+
if d == "" {
114+
continue
115+
}
116+
117+
digits := utils.MustIntFromString(d)
118+
current = append(current, digits)
119+
operation := v[last]
120+
121+
// Perform calculation and reset current
122+
switch operation {
123+
case "+":
124+
grandTotal += utils.Sum(current)
125+
current = []int{}
126+
case "*":
127+
grandTotal += utils.Prod(current)
128+
current = []int{}
129+
}
130+
}
131+
132+
return
133+
}

go/2025/puzzles/day06/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 := 4277556
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 := 3263827
20+
actual := part2("test-input.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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
123 328 51 64
2+
45 64 387 23
3+
6 98 215 314
4+
* + * +

0 commit comments

Comments
 (0)