diff --git a/README.md b/README.md index a786aa6..076360b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,6 @@ go test ./cmd/01 | 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | FIFO for life. 🤣. Fuck me...part 2 I found really difficult and the use of DFS was obvious, but doing the memoizing was not. | | 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | Part 1 had me down bad. I was on the right track with my original solution, but ended up doing it in a brute force way then figuring out my mistake with the original. Fuck this guy. Part 2 was so much easier once part 1 was sorted out. | | 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | Part 1 was such a nice break from the madness of day 8. Basically I need to go build some video games. | -| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | +| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | Part 1 took me way to look cause I couldn't figure out how to generate all button press combinations. | | 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | | 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | diff --git a/cmd/10/main.go b/cmd/10/main.go new file mode 100644 index 0000000..e1cd8ea --- /dev/null +++ b/cmd/10/main.go @@ -0,0 +1,167 @@ +package main + +import ( + "fmt" + "math" + "regexp" + "slices" + "strconv" + "strings" + + "github.com/StevanFreeborn/advent-of-code-2025/internal/file" +) + +type machine struct { + switches []bool + buttons []button +} + +func NewMachine(numberOfLights int, buttonsAsStrings []string) machine { + buttons := []button{} + + for _, bs := range buttonsAsStrings { + b := NewButton(bs) + buttons = append(buttons, b) + } + + switches := []bool{} + + for range numberOfLights { + switches = append(switches, false) + } + + return machine{ + buttons: buttons, + switches: switches, + } +} + +func (m machine) String() string { + var str strings.Builder + + str.WriteString("[MACHINE]\n") + + switchStr := fmt.Sprintf("[SWITCHES]: %v", m.switches) + str.WriteString(switchStr + "\n") + + str.WriteString("[BUTTONS]:\n") + + for _, b := range m.buttons { + str.WriteString(b.String() + "\n") + } + + str.WriteString("\n") + + return str.String() +} + +type button struct { + switchesControlled []int +} + +func (b button) String() string { + return fmt.Sprintf("%v", b.switchesControlled) +} + +func NewButton(line string) button { + buttonRegex := regexp.MustCompile(`\d+`) + matches := buttonRegex.FindAllString(line, -1) + + switchesControlled := []int{} + + for _, m := range matches { + num, _ := strconv.Atoi(m) + switchesControlled = append(switchesControlled, num) + } + + return button{ + switchesControlled: switchesControlled, + } +} + +func (m machine) Configure(desiredState []bool) int { + combinations := [][]bool{} + minPresses := math.MaxInt + numberOfButtons := len(m.buttons) + numberOfCombinations := int(math.Pow(2, float64(numberOfButtons))) + currentCombination := make([]bool, numberOfButtons) + + for range numberOfCombinations { + temp := make([]bool, numberOfButtons) + copy(temp, currentCombination) + + combinations = append(combinations, temp) + + for j := range numberOfButtons { + if currentCombination[j] == false { + currentCombination[j] = true + break + } else { + currentCombination[j] = false + } + } + } + + for _, currentCombination := range combinations { + currentPresses := 0 + switchesCopy := []bool{} + + for _, v := range m.switches { + switchesCopy = append(switchesCopy, v) + } + + for bi, bs := range currentCombination { + if bs == false { + continue + } + + currentPresses++ + switchesToToggle := m.buttons[bi].switchesControlled + + for _, switchToToggle := range switchesToToggle { + switchesCopy[switchToToggle] = !switchesCopy[switchToToggle] + } + } + + if slices.Equal(switchesCopy, desiredState) == false { + continue + } + + if currentPresses < minPresses { + minPresses = currentPresses + } + } + + return minPresses +} + +func SolvePartOne(filePath string) int { + total := 0 + + for line := range file.ReadLines(filePath) { + parts := strings.Split(line, " ") + lightDiagramPart := parts[0] + buttonsPart := parts[1 : len(parts)-1] + // NOTE: Parse joltage here + + lightDiagram := []bool{} + + for _, c := range lightDiagramPart { + if c == '.' { + lightDiagram = append(lightDiagram, false) + } + + if c == '#' { + lightDiagram = append(lightDiagram, true) + } + } + + numberOfLights := len(lightDiagram) + + machine := NewMachine(numberOfLights, buttonsPart) + buttonsPressed := machine.Configure(lightDiagram) + total += buttonsPressed + } + + return total +} diff --git a/cmd/10/main_test.go b/cmd/10/main_test.go new file mode 100644 index 0000000..9b813b0 --- /dev/null +++ b/cmd/10/main_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/10" +) + +func TestSolvePartOneWithExampleInput(t *testing.T) { + expected := 7 + + result := solution.SolvePartOne("EXAMPLE.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +} + +func TestSolvePartOneWithInput(t *testing.T) { + expected := -1 + + result := solution.SolvePartOne("INPUT.txt") + + if result != expected { + t.Errorf("got %d but wanted %d", result, expected) + } +}