Skip to content

Commit c32f421

Browse files
committed
2025 day06
1 parent 37c632f commit c32f421

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed

2025/day06/.bench

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

2025/day06/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- You can add some comments here if you want to :D -->

2025/day06/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"os"
6+
)
7+
8+
//go:embed input.txt
9+
var input string
10+
11+
//go:embed input_test.txt
12+
var inputTest string
13+
14+
func main() {
15+
// Check argv if we use test input or not
16+
if len(os.Args) > 1 && os.Args[1] == "test" {
17+
input = inputTest
18+
}
19+
20+
answer := doPartOne(input)
21+
println(answer)
22+
23+
answer = doPartTwo(input)
24+
println(answer)
25+
}

2025/day06/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+
doPartOne(input)
8+
}
9+
}
10+
11+
func BenchmarkPartTwo(b *testing.B) {
12+
for n := 0; n < b.N; n++ {
13+
doPartTwo(input)
14+
}
15+
}

2025/day06/part1.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"aocli/utils"
5+
"strings"
6+
)
7+
8+
func doPartOne(input string) int {
9+
lines := strings.Split(strings.TrimSpace(input), "\n")
10+
numbers := make([][]int, len(lines[0]))
11+
for x := 0; x < len(lines)-1; x++ {
12+
for y, v := range strings.Fields(lines[x]) {
13+
numbers[y] = append(numbers[y], utils.Atoi(v))
14+
}
15+
}
16+
ans := 0
17+
for y, v := range strings.Fields(lines[len(lines)-1]) {
18+
subAns := numbers[y][0]
19+
if v[0] == '+' {
20+
for i := 1; i < len(numbers[y]); i++ {
21+
subAns += numbers[y][i]
22+
}
23+
} else {
24+
for i := 1; i < len(numbers[y]); i++ {
25+
subAns *= numbers[y][i]
26+
}
27+
}
28+
ans += subAns
29+
}
30+
// mapper, rect := maps.MakeImagePointMapRect(strings.Split(strings.TrimSpace(input), "\n"))
31+
32+
// ans := 0
33+
// for x := 0; x < rect.Max.X; x++ {
34+
// subAns := utils.Atoi(string(mapper[image.Point{x, 0}]))
35+
// if mapper[image.Point{x, rect.Max.Y - 1}] == '+' {
36+
// for y := 1; y < rect.Max.Y-1; y++ {
37+
// subAns += utils.Atoi(string(mapper[image.Point{x, y}]))
38+
// }
39+
// ans += subAns
40+
// continue
41+
// }
42+
// for y := 1; y < rect.Max.Y-1; y++ {
43+
// subAns *= utils.Atoi(string(mapper[image.Point{x, y}]))
44+
// }
45+
// ans += subAns
46+
// }
47+
return ans
48+
}

2025/day06/part2.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"aocli/utils"
5+
"strings"
6+
)
7+
8+
func doPartTwo(input string) int {
9+
lines := strings.Split(input, "\n")
10+
// Remove trailing empty line if present
11+
if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
12+
lines = lines[:len(lines)-1]
13+
}
14+
15+
if len(lines) == 0 {
16+
return 0
17+
}
18+
19+
ans := 0
20+
startCol := 0
21+
lastLine := len(lines) - 1
22+
lineWidth := len(lines[0])
23+
24+
// Iterate through each column
25+
for x := 0; x < lineWidth; x++ {
26+
// Check if this column is blank (separator between problems)
27+
isBlank := true
28+
for y := 0; y < lastLine; y++ {
29+
if x < len(lines[y]) && lines[y][x] != ' ' {
30+
isBlank = false
31+
break
32+
}
33+
}
34+
35+
if isBlank {
36+
// Process the problem from startCol to x-1
37+
operator := lines[lastLine][startCol]
38+
result := 0
39+
40+
if operator == '+' {
41+
result = 0
42+
} else {
43+
result = 1
44+
}
45+
46+
// Read each column in this problem
47+
for col := startCol; col < x; col++ {
48+
var numBuilder strings.Builder
49+
// Read vertically from top to bottom
50+
for y := 0; y < lastLine; y++ {
51+
if col < len(lines[y]) && lines[y][col] != ' ' {
52+
numBuilder.WriteByte(lines[y][col])
53+
}
54+
}
55+
56+
if numBuilder.Len() > 0 {
57+
num := utils.Atoi(numBuilder.String())
58+
if operator == '+' {
59+
result += num
60+
} else {
61+
result *= num
62+
}
63+
}
64+
}
65+
66+
ans += result
67+
startCol = x + 1
68+
}
69+
}
70+
71+
// Handle last problem if it doesn't end with a blank column
72+
if startCol < lineWidth {
73+
operator := lines[lastLine][startCol]
74+
result := 0
75+
76+
if operator == '+' {
77+
result = 0
78+
} else {
79+
result = 1
80+
}
81+
82+
for col := startCol; col < lineWidth; col++ {
83+
var numBuilder strings.Builder
84+
for y := 0; y < lastLine; y++ {
85+
if col < len(lines[y]) && lines[y][col] != ' ' {
86+
numBuilder.WriteByte(lines[y][col])
87+
}
88+
}
89+
90+
if numBuilder.Len() > 0 {
91+
num := utils.Atoi(numBuilder.String())
92+
if operator == '+' {
93+
result += num
94+
} else {
95+
result *= num
96+
}
97+
}
98+
}
99+
100+
ans += result
101+
}
102+
103+
return ans
104+
}

0 commit comments

Comments
 (0)