feat: wip on day 10 part 2

This commit is contained in:
Stevan Freeborn
2025-12-24 18:13:07 -06:00
parent b1fed493b3
commit a2024af1b4
3 changed files with 72 additions and 0 deletions
+50
View File
@@ -2,6 +2,7 @@
package machine package machine
import ( import (
"fmt"
"math" "math"
"regexp" "regexp"
"slices" "slices"
@@ -13,6 +14,7 @@ import (
type Machine interface { type Machine interface {
ConfigureLights() int ConfigureLights() int
ConfigureJoltages() int
} }
type machine struct { type machine struct {
@@ -115,3 +117,51 @@ func (m machine) ConfigureLights() int {
return minPresses return minPresses
} }
func (m machine) ConfigureJoltages() int {
minPresses := 0
// counters := make([]int, len(m.joltageSettings))
// given the target joltage of a counter
// how many times can I press a particular button
// before making one of the counters that the button
// affects invalid
// 3,5,4,7
// 0 (3)
// 1 (1,3)
// 2 (2)
// 3 (2,3)
// 4 (0,2)
// 5 (0,1)
// (0n * 1) + (1n * 0) + (1n * 1) + (3n * 1) = 7
// 2n + 3n + 4n = 4
// TODO: I need to use Gausian elimination
// to solve this
// TODO: Or matrix method maybe?
rows := len(m.desiredJoltages)
cols := len(m.buttons)
grid := make([][]int, rows)
for r := range rows {
grid[r] = make([]int, cols+1)
for i, b := range m.buttons {
for _, sw := range b.Switches() {
if sw == r {
grid[r][i] = 1
}
}
}
grid[r][cols] = m.desiredJoltages[r]
fmt.Println(grid[r])
}
fmt.Println()
return minPresses
}
+12
View File
@@ -16,3 +16,15 @@ func SolvePartOne(filePath string) int {
return total return total
} }
func SolvePartTwo(filePath string) int {
total := 0
for line := range file.ReadLines(filePath) {
machine := machine.From(line)
buttonsPressed := machine.ConfigureJoltages()
total += buttonsPressed
}
return total
}
+10
View File
@@ -25,3 +25,13 @@ func TestSolvePartOneWithInput(t *testing.T) {
t.Errorf("got %d but wanted %d", result, expected) t.Errorf("got %d but wanted %d", result, expected)
} }
} }
func TestSolvePartTwoWithExampleInput(t *testing.T) {
expected := 7
result := solution.SolvePartTwo("EXAMPLE.txt")
if result != expected {
t.Errorf("got %d but wanted %d", result, expected)
}
}