Skip to content

Commit 84a38da

Browse files
committed
Advent of Code (Go): solve 2025/06
1 parent 7320ab9 commit 84a38da

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

  • advent_of_code.go/y2025

advent_of_code.go/y2025/06.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package y2025
2+
3+
import (
4+
"isaacgood.com/aoc/helpers"
5+
"strings"
6+
)
7+
8+
// Day06 solves 2025/06.
9+
type Day06 struct {
10+
op map[string]func(int, int) int
11+
initial map[string]int
12+
}
13+
14+
// Solve returns the solution for one part.
15+
func (p *Day06) Solve(data string, part int) string {
16+
p.op = map[string]func(int, int) int{
17+
"+": func(a, b int) int { return a + b },
18+
"*": func(a, b int) int { return a * b },
19+
}
20+
p.initial = map[string]int{
21+
"+": 0,
22+
"*": 1,
23+
}
24+
return []func(string) string{p.p1, p.p2}[part-1](data)
25+
}
26+
27+
func (p *Day06) p1(data string) string {
28+
lines := helpers.ParseMultiWordsPerLine(data)
29+
maxLine := len(lines) - 1
30+
var total int
31+
for col := range len(lines[0]) {
32+
tally := p.initial[lines[maxLine][col]]
33+
op := p.op[lines[maxLine][col]]
34+
for _, line := range lines[:maxLine] {
35+
tally = op(tally, helpers.Atoi(line[col]))
36+
}
37+
total += tally
38+
}
39+
return helpers.Itoa(total)
40+
}
41+
42+
func (p *Day06) p2(data string) string {
43+
lines := strings.Split(data, "\n")
44+
maxLine := len(lines) - 1
45+
46+
var colStarts []int
47+
var operators []string
48+
for idx, char := range lines[len(lines)-1] {
49+
if char != ' ' {
50+
operators = append(operators, string(char))
51+
colStarts = append(colStarts, idx)
52+
}
53+
}
54+
55+
var longest int
56+
for _, line := range lines {
57+
if l := len(line); l > longest {
58+
longest = l
59+
}
60+
}
61+
colEnds := colStarts[1:]
62+
colEnds = append(colEnds, longest+1)
63+
64+
var total int
65+
for idx, opChar := range operators {
66+
tally := p.initial[opChar]
67+
op := p.op[opChar]
68+
69+
for col := colStarts[idx]; col < colEnds[idx]-1; col++ {
70+
var number []byte
71+
for _, line := range lines[:maxLine] {
72+
if char := line[col]; char != ' ' {
73+
number = append(number, line[col])
74+
}
75+
}
76+
tally = op(tally, helpers.Atoi(string(number)))
77+
}
78+
total += tally
79+
}
80+
return helpers.Itoa(total)
81+
}
82+
83+
func init() {
84+
helpers.AocRegister(2025, 6, &Day06{})
85+
}

0 commit comments

Comments
 (0)