Skip to content

Commit ac94495

Browse files
committed
2025 day 6
1 parent bf8910d commit ac94495

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

2025/6/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2025/day/6/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2025/6/common.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+
"bufio"
5+
"os"
6+
)
7+
8+
var neutral = map[byte]int{
9+
'+': 0,
10+
'*': 1,
11+
}
12+
13+
var fn = map[byte]func(int, int) int{
14+
'+': func(a, b int) int { return a + b },
15+
'*': func(a, b int) int { return a * b },
16+
}
17+
18+
func parseInput() []string {
19+
var lines []string
20+
scanner := bufio.NewScanner(os.Stdin)
21+
for scanner.Scan() {
22+
lines = append(lines, scanner.Text())
23+
}
24+
return lines
25+
}

2025/6/main1.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
lines := parseInput()
7+
ops, lines := lines[len(lines)-1], lines[:len(lines)-1]
8+
9+
grandTotal := 0
10+
for i := 0; i < len(ops); i++ {
11+
if ops[i] == '*' || ops[i] == '+' {
12+
grandTotal += applyHorizontal(lines, i, ops[i])
13+
}
14+
}
15+
fmt.Println(grandTotal)
16+
}
17+
18+
func applyHorizontal(lines []string, idx int, op byte) int {
19+
result := neutral[op]
20+
for _, line := range lines {
21+
num := readHorizontal(line, idx)
22+
result = fn[op](result, num)
23+
}
24+
return result
25+
}
26+
27+
func readHorizontal(line string, idx int) int {
28+
num := 0
29+
for line[idx] == ' ' {
30+
idx++
31+
}
32+
for i := idx; i < len(line); i++ {
33+
if line[i] < '0' || line[i] > '9' {
34+
break
35+
}
36+
num = 10*num + int(line[i]-'0')
37+
}
38+
return num
39+
}

2025/6/main2.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
lines := parseInput()
7+
ops, lines := lines[len(lines)-1], lines[:len(lines)-1]
8+
9+
grandTotal := 0
10+
for i := 0; i < len(ops); i++ {
11+
if ops[i] == '*' || ops[i] == '+' {
12+
var j int
13+
for j = i + 1; j < len(ops); j++ {
14+
if ops[j] == '*' || ops[j] == '+' {
15+
j--
16+
break
17+
}
18+
}
19+
grandTotal += applyVertical(lines, i, j, ops[i])
20+
}
21+
}
22+
23+
fmt.Println(grandTotal)
24+
}
25+
26+
func applyVertical(lines []string, i, j int, op byte) int {
27+
result := neutral[op]
28+
for k := i; k < j; k++ {
29+
num := readVertical(lines, k)
30+
result = fn[op](result, num)
31+
}
32+
return result
33+
}
34+
35+
func readVertical(lines []string, idx int) int {
36+
num := 0
37+
for _, line := range lines {
38+
if line[idx] == ' ' {
39+
continue
40+
}
41+
num = num*10 + int(line[idx]-'0')
42+
}
43+
return num
44+
}

0 commit comments

Comments
 (0)