Skip to content

Commit 32002ba

Browse files
committed
feat(2025): add day 10 part 1
1 parent 7c83c31 commit 32002ba

File tree

1 file changed

+69
-39
lines changed

1 file changed

+69
-39
lines changed

go/2025/puzzles/day10/main.go

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"fmt"
5+
"math"
6+
"slices"
57
"strings"
68

79
"github.com/believer/aoc-utils/files"
@@ -19,29 +21,89 @@ type Machine struct {
1921
joltage []int
2022
}
2123

22-
func part1(name string) int {
24+
func part1(name string) (totalIterations int) {
2325
lines := files.ReadLines(name)
24-
var machines []Machine
26+
machines := createMachines(lines)
2527

28+
for _, m := range machines {
29+
start := []int{}
30+
31+
for i, l := range m.lights {
32+
if l == 1 {
33+
start = append(start, i)
34+
}
35+
}
36+
37+
maskedStart := toBitmask(start)
38+
maskedButtons := []int{}
39+
end := 0
40+
41+
for _, b := range m.buttons {
42+
maskedButtons = append(maskedButtons, toBitmask(b))
43+
}
44+
45+
current := []int{maskedStart}
46+
iterations := 0
47+
48+
for !slices.Contains(current, end) {
49+
next := []int{}
50+
51+
for _, c := range current {
52+
for _, b := range maskedButtons {
53+
next = append(next, c^b)
54+
}
55+
}
56+
57+
current = next
58+
iterations += 1
59+
}
60+
61+
totalIterations += iterations
62+
}
63+
64+
return
65+
}
66+
67+
func part2(name string) int {
68+
return 0
69+
}
70+
71+
func toBitmask(values []int) (mask int) {
72+
for _, i := range values {
73+
mask += int(math.Pow(2, float64(i)))
74+
}
75+
76+
return
77+
}
78+
79+
func createMachines(lines []string) (machines []Machine) {
2680
for _, l := range lines {
2781
lights := strings.Split(l[1:], "] ")
2882
buttonSchema := strings.Split(lights[1], " {")
2983
buttonSchema = strings.Fields(buttonSchema[0])
3084

31-
lightSchema := make([]int, len(lights[0]))
85+
var lightSchema []int
3286
var buttons [][]int
3387

88+
for l := range strings.SplitSeq(lights[0], "") {
89+
switch l {
90+
case ".":
91+
lightSchema = append(lightSchema, 0)
92+
case "#":
93+
lightSchema = append(lightSchema, 1)
94+
}
95+
}
96+
3497
for _, b := range buttonSchema {
3598
// Remove parenthesis
3699
b = b[1:]
37100
b = b[:len(b)-1]
38101

39-
// Create a matrix for the button presses
40-
buttonsSchema := make([]int, len(lightSchema))
102+
var buttonsSchema []int
41103

42104
for b := range strings.SplitSeq(b, ",") {
43105
button := utils.MustIntFromString(b)
44-
buttonsSchema[button] = 1
106+
buttonsSchema = append(buttonsSchema, button)
45107
}
46108

47109
buttons = append(buttons, buttonsSchema)
@@ -53,37 +115,5 @@ func part1(name string) int {
53115
})
54116
}
55117

56-
for _, m := range machines[:1] {
57-
solveMachine(m)
58-
}
59-
60-
return 0
61-
}
62-
63-
func part2(name string) int {
64-
return 0
65-
}
66-
67-
// Gauss elimination
68-
func solveMachine(m Machine) int {
69-
N := len(m.lights)
70-
M := len(m.buttons)
71-
72-
// Create the augmented matrix [A|B]
73-
augmentedMatrix := make([][]int, N)
74-
for i := range N {
75-
augmentedMatrix[i] = make([]int, M+1)
76-
77-
// Copy A (buttons)
78-
for j := range M {
79-
augmentedMatrix[i][j] = m.buttons[j][i]
80-
}
81-
82-
// Copy B (lights)
83-
augmentedMatrix[i][M] = m.lights[i]
84-
}
85-
86-
fmt.Println(augmentedMatrix)
87-
88-
return 0
118+
return
89119
}

0 commit comments

Comments
 (0)