Skip to content

Commit ce7b5b0

Browse files
committed
2025 day10 (not fully working)
1 parent a64cfdc commit ce7b5b0

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

2025/day10/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/day10/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/day10/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/day10/part1.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 (
4+
"aocli/utils"
5+
"aocli/utils/xor"
6+
"strings"
7+
)
8+
9+
func doPartOne(input string) int {
10+
lines := strings.Split(strings.TrimSpace(input), "\n")
11+
ans := 0
12+
for _, line := range lines {
13+
s := strings.Fields(line)
14+
goal := 0
15+
for i, c := range strings.Trim(s[0], "[]") {
16+
goal += utils.Ter(c == '#', utils.IntPow(2, i), 0)
17+
}
18+
19+
buttons := s[1 : len(s)-1]
20+
buttonscore := make([]int, 0, len(buttons))
21+
for _, b := range buttons {
22+
s := strings.Split(strings.Trim(b, "()"), ",")
23+
button := 0
24+
for _, n := range s {
25+
button += utils.IntPow(2, utils.Atoi(n))
26+
}
27+
buttonscore = append(buttonscore, button)
28+
}
29+
30+
// Use optimized XOR solver
31+
score, possible := xor.SolveMinXORAuto(buttonscore, goal)
32+
if !possible {
33+
score = len(buttonscore) // Fallback
34+
}
35+
36+
ans += score
37+
}
38+
return ans
39+
}

2025/day10/part2.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"aocli/utils"
5+
"aocli/utils/minpresses"
6+
"strings"
7+
"sync"
8+
)
9+
10+
func doPartTwo(input string) int {
11+
lines := strings.Split(strings.TrimSpace(input), "\n")
12+
13+
type result struct {
14+
index int
15+
score int
16+
line string
17+
}
18+
19+
results := make(chan result, len(lines))
20+
var wg sync.WaitGroup
21+
22+
// Process each line in parallel
23+
for idx, line := range lines {
24+
wg.Add(1)
25+
go func(index int, line string) {
26+
defer wg.Done()
27+
28+
s := strings.Fields(line)
29+
30+
// Parse buttons (middle section)
31+
buttonstring := s[1 : len(s)-1]
32+
buttonslen := len(buttonstring)
33+
buttonPositions := make([][]int, 0, buttonslen)
34+
for _, b := range buttonstring {
35+
s := strings.Split(strings.Trim(b, "()"), ",")
36+
positions := make([]int, 0, len(s))
37+
for _, n := range s {
38+
positions = append(positions, utils.Atoi(n))
39+
}
40+
buttonPositions = append(buttonPositions, positions)
41+
}
42+
43+
// Parse target joltages (last section)
44+
targetJoltages := []int{}
45+
for _, c := range strings.Split(strings.Trim(s[len(s)-1], "{}"), ",") {
46+
targetJoltages = append(targetJoltages, utils.Atoi(c))
47+
}
48+
49+
// Solve for minimum presses to reach joltages
50+
score, possible := minpresses.SolveMinPresses(buttonPositions, targetJoltages)
51+
if !possible {
52+
score = 0 // No solution
53+
}
54+
55+
results <- result{index: index, score: score, line: line}
56+
}(idx, line)
57+
}
58+
59+
// Close results channel when all goroutines are done
60+
go func() {
61+
wg.Wait()
62+
close(results)
63+
}()
64+
65+
// Collect results in order
66+
collected := make([]result, len(lines))
67+
for r := range results {
68+
collected[r.index] = r
69+
}
70+
71+
// Sum results
72+
ans := 0
73+
for _, r := range collected {
74+
ans += r.score
75+
}
76+
77+
return ans
78+
}

0 commit comments

Comments
 (0)