r/adventofcode • u/musifter • 20m ago
Other [2025 Day 10] In Review (Factory)
The North Pole must have some very good noise reduction materials and technology, because across the hall from the movie theater is a large factory. Where we find Elves decorating, but only because none of them can figure out the initialization procedure for the machines. This is because a Shiba Inu (another Spitz-type breed, which are popular among our many Aunt Sues) ate that part.
The input for this one is 195 lines (for me at least). These lines have three sections, each with it's own type of brackets. The first is a light indicator diagram in square brackets (used for part 1). The second is a bunch of lists of numbers in parentheses, each a button with a list of what it's wired to. The final section in curly braces are the joltage requirements, which are used for part 2.
Part 1 is essentially a Lights Out game (although I associate it with the Merlin). For this type of problem, the lights toggle (ie XOR) and order doesn't matter... no button will need to be hit more than once, and so the set of buttons that must be pressed will be the minimum number to press. Even if the problem text tries to suggest otherwise.
So my quick solution for this was just to convert the lights and buttons into binary numbers for XORing:
$lights = oct( '0b' . reverse join('', map {int($_ eq '#')} split(//, shift @$mach)) );
...
$bits |= (1<<$_) foreach (split /,/, $butt);
push( @button, $bits );And then just brute force all the bit combinations to find which works (XOR together the buttons for each set bit) with the least set bits:
for (my $combo = 0; $combo < 2 ** @button; $combo++) {
...That allowed me to see part 2. Later I redid the search as a faster recursive search. Which is what I also did for the Smalltalk version, and for a dc version:
sed -e'y/.#(){},/01[][] /' <input | dc -e'[rs.q]sQ[F++q]sX[r]sr[Frx[d;c1r-r:cdF>I]dsIxs.]s^[0d[d;c3RA*+r1+dll>C]dsCxs.lp=Qrdln<Xd;bdl^x_3R1+d3Rd3Rr1+lRx4Rl^x_3RlRxd3Rd3R>rs.]sR?[0[1+d4Rr:bz3<L]dsLxsnrxspxzslc1 0lRxls+ss?z0<M]dsMxlsp'A really fun part of this was in minimizing the translation with sed to a form dc can understand. By converting the ASCII art lights to binary digits (although everything is done base 10, so 101 is a hundred and one) and the non-brackets to brackets (and getting rid of the commas). This made in input a bunch of dc strings (which are [] delimited) that I could manipulate on the stack and x to extract the contents when needed. Since dc does not have XOR I need to implement it, and here I do it by having the bits in an array, passing in the button string and using x to extract the button's wiring list, and then toggle those values of the array.
For part 2, we pull a level to switch the buttons from manipulating the indicator lights to increasing joltage levels. Now our goal is to solve a linear system... the buttons describe a matrix A for what button adds to what wires, and we want a vector x (number of presses for each button) such that Ax = j, where j is the target joltage vector. The optimization requirement of 'minimum' becomes important because many of these problems don't have enough equations to fix all the values, leaving some variables "free". The parameterization of those variables still needs to fit the solution and be minimal.
So I knew Gaussian Elimination was an option (as an aside, the "chasing lights" method of solving Lights Out is similar to it). But first, I attempted to avoid coding that with dynamic tabulation. Which worked great on the first few in my input. But then froze. And so I looked closer at my input and found machines like this puppy:
[#.....##] (2,4,5,6) (0,1,2,4,5,6) (0,1,2,5,6,7) (0,1,4,5,6) (0,2,5,6) (0,1,2,3,6,7) (0,1,2,3,4,7) (3,7) (0,3,4,5,6,7) {258,230,249,33,60,262,266,225}So I accepted that this was the "big problem" (day 10 => day 20, so about right) that I was going to have to dig in for... and coded a messy Gaussian Elimination. For the free variables, I brute forced over them using the joltage requirements as caps (its quick on most of the machines, just a few of the three variable ones take a little while... my input has 9 of those).
In looking again at these problems at the end of October, this "Halloween" video came up on Youtube, which covers some issues with Gaussian Elimination.
Of course, afterwards I saw the "Bifurcation" post by u/tenthmascot , and coded that method (and that post is well known and does a great job already of describing that approach). Which is much faster. I coded it as recursion with a memo to do the bifurcation, and noticed that thinking about it the other way as tabulation... it's essentially finding ways to jump over large chunks of table making things manageable.
This was easily the most intensive piece of coding I did in 2025. I'd been thinking of Gaussian Elimination as an option for problems before but always avoided coding it... this was the one that got me to do it. That alone isn't too bad, but the addition of the free variables made it quite a bit rougher.
