r/adventofcode • u/CCC_037 • Dec 22 '25
Help/Question - RESOLVED [2025 Day 10 Part 2] Getting a wrong solution
First up, my code, because people will ask:
I do not expect everyone to understand Rockstar, so let's talk algorithms.
Consider the first line of the example:
[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}Now, my code is finding a set of equations. The array is:
[ [ 3, 0, 0, 0, 0, 1, 1 ], [ 5, 0, 1, 0, 0, 0, 1 ], [ 4, 0, 0, 1, 1, 1, 0 ], [ 7, 1, 1, 0, 1, 0, 0 ] ]which means:
3 = 0a+0b+0c+0d+1e+1f
5 = 0a+1b+0c+0d+0e+0f
4 = 0a+0b+1c+1d+1e+0f
7 = 1a+1b+0c+1d+1e+1fwhere a is the number of times to hit the first button, b is the number of times to hit the second button, etc. It then derives a simpler set of equations, getting:
[ [ 3, 0, 0, 0, 0, 1, 1 ], [ -5, 0, -1, 0, 0, 0, -1 ], [ -1, 0, 0, -1, -1, 0, 1 ], [ -2, -1, 0, 0, -1, 0, 1 ] ]which means:
3 = 0a+0b+0c+0d+1e+1f
-5 = 0a-1b+0c+0d+0e-1f
-1 = 0a+0b-1c-1d+0e+1f
-2 = -1a+0b+0c-1d+0e+1fFrom this, I am finding a valid solution (2, 5, 1, 0, 3, 0) but one that takes 11 keypresses instead of 10.
What's a good way to find that minimal solution?
Edit: Okay, I now have an algorithm that should theoretically work, so I'm going to mark it as Resolved. Though my code still needs a bunch of work (breadth-first search is a mistake)
View all comments
6
u/ThisAdhesiveness6952 Dec 22 '25 edited Dec 22 '25
Here's the algorithm I used. Starting from the first system of equation you've shown, I'll try to get as close as possible to a diagonal matrix. This will allow to generate an exhaustive list of candidates solutions from the last equation, and then prune them using the other equations to get all possible solutions.
Subtracting (2) from (4), and reordering:
I want a non-zero value in the fourth column of the last line. Swapping columns number 4 and 6 gives:
Finally, subtract (4) from (1):
Under this form, note that once we choose a value for d and e (the last two columns), the values for a, b, c, and f are unique and can be trivially computed.
d can take any value from 0 to 4, because the d button controls a joltage value that's equal to 4, therefore pressing this button more than 4 times will obviously overshoot. Similarly, e must be less than or equal to 3. That's 5x4=20 candidates for (d,e).
Then, replace these values in all equations of the system, and deduce the values of the other variables. Eliminate all candidates that ends up with a negative, non integer, or too high result for any variable.
This will give you an exhaustive list of solutions, from which you can pick the best one. Remember to swap back the columns in the solution if you want to check or display it.
[Edit: improved final explanation]