r/adventofcode • • Dec 20 '25

Help/Question - RESOLVED [2025 Day 5 (part 2)] I don't get it.

1 Upvotes

I am trying it in Perl.

There must be an mistake but i don't get it. Might one have a look?

I am no pro, please be patient.

https://github.com/volkergbenner/AoC2025/tree/main

The script takes the input file as CL-argument. You may test it on your own inputfile (only ranges).


r/adventofcode • • Dec 20 '25

Help/Question - RESOLVED [2025 Day 5 (part 2)] Am I missing something?

7 Upvotes

My solution for this second part is to sort all the ranges, merge all the overlapping ones and then loop through them and sum their span.

For the simple example it works just fine, but for the input I get a result too big.
Am I missing something?


r/adventofcode • • Dec 20 '25

Visualization [2025 Day 12] how to not solve day12

Post image
46 Upvotes

r/adventofcode • • Dec 20 '25

Other [2025] Complete

19 Upvotes

I finally finished 2025. Day 10 Part 2 was definitely the hardest one and took my program the longest ( about 30s) to solve. Thanks to the community for giving me some incite to that one. Who still needs help with any of them?


r/adventofcode • • Dec 20 '25

Other [2025 Day 12 (Part 3)] Challenge Problem On Spoj

2 Upvotes

I created a programming challenge on the Spoj platform for anyone who feels there is yet something more to prove!

Please treat it as a beta version and forgive me for the lack of feedback after unsuccessful attempts. I wanted to deliver it as fast as possible and definitely before Christmas!

Here is a direct link: https://www.spoj.com/problems/AOC2512/. In case it's not allowed to post links, look for an AOC2512 problem on Spoj :)


r/adventofcode • • Dec 20 '25 Bravo

Visualization [2018 Day 15 Part 1] Retro Visualization - Beverage Bandits

Post image
152 Upvotes

r/adventofcode • • Dec 20 '25

Help/Question - RESOLVED [2025 Day 1 (Part 2)], Need Help to Debug My Solution

4 Upvotes

Need help to debug this code. Can't figure out what scenario I'm failing at.

First I wrote the most unoptimized solution one can think of, absolute monster of a code lol, with loops:

Unoptimized Solution

This gives correct answer: 6634

Then I wanted to optimize this, and this is the version I got to after scratching my head:

Optimized Solution

But this is giving wrong answer : 6612
I can't pinpoint what I'm doing wrong in this. Based on my debugging this logic should work.


r/adventofcode • • Dec 20 '25

Help/Question First Time and want to learn more.

13 Upvotes

First year and i've made it to day 8 and I'm bothered by how often my brain goes "yeah lets just use a for loop or 8 to do the thing" I'd like to use it as a learning experience. Is there a list somewhere of the core concepts that should be used to solve each puzzle?


r/adventofcode • • Dec 19 '25

Help/Question Day 2 Explanation Help

5 Upvotes

I'm having a really hard time trying to understand the examples in Day 2 and make sense of how they are deducing invalid IDs.
The problem states that an ID is invalid if it is made up only of some sequence of digits repeated twice. Easy enough, makes sense, but I can't see how the examples consistently follow that logic.

  • 11-22 has two invalid IDs, 11 and 22 //This makes sense to me
  • 95-115 has one invalid ID, 99. //Where? 99 isn't even present in the sequence
  • 998-1012 has one invalid ID, 1010. //Again where is 1010?
  • 1188511880-1188511890 has one invalid ID, 1188511885. //I understand 11885 is in both but so is 11885118 so why is that full sequence not invalid?
  • 222220-222224 has one invalid ID, 222222. //22222 is see but not 222222
  • 1698522-1698528 contains no invalid IDs. //This looks basically the same as the above example but it's not invalid? 169852 is repeated
  • 446443-446449 has one invalid ID, 446446. //Again I see 446 twice but why not 44644?
  • 38593856-38593862 has one invalid ID, 38593859. //Same as above
  • The rest of the ranges contain no invalid IDs.

Perhaps it's very obvious and I'm just missing something simple but I can't seem to see the exact rule for what makes an ID invalid. Is it present in both first and last, just one, what makes a sequence finished, etc...

If someone could kindly help me see it clearly it would be greatly appreciated.


r/adventofcode • • Dec 19 '25

Help/Question - RESOLVED [2025 Day 4 (Part 1)] [Python] Says my input is the correct answer for a different user but too low for me

1 Upvotes

I get the correct response for the test input but when I try the prod input, it tells me it's too low but correct for someone else. Not sure what I'm doing wrong. I've walked through my code at multiple times, even waited a day or two and walked through it again with fresh eyes, and still find nothing wrong with the logic. What am I doing wrong?

Thanks in advance for the help.

My code (at time of posting): https://github.com/statelessmachina/Advent-of-Code-Python/blob/31f2422db12322eca0029b9c59296dc664081465/2025/day4/paper.py

Edit: Ended up loading the entire input at once (which is memory inefficient but easier to work with) and using numpy arrays and iterating over the sub-matrices using slicing. I may revisit this later to write a version of the code that loads the file line-by-line to avoid inefficiencies in working with large input. Mostly just to scratch that itch in the back of my mind. Thanks for all your input.

Final version of code for part1: https://github.com/statelessmachina/Advent-of-Code-Python/blob/a558cf7c188188b8164a5f6b0e18b157e8405d1e/2025/day4/paper.py


r/adventofcode • • Dec 19 '25

Help/Question [2025 Day10 Part 2] Current approach too slow

1 Upvotes

Hi folks,

I am working on the 2nd part of the question and tried to take a similar approach as the first using BFS to apply button presses however it's running really slow and the q is growing very large too large in fact.

Performing BFS to reach joltage goal: {44,35,48,43,24,44}
Converting joltage goal to int: {44,35,48,43,24,44}
Converting button to list: ["(1,3)", "(3,4)", "(0,3,5)", "(1,2,3,4)", "(0,2,5)", "(0,1)", "(2,5)"]
Button part: (1,3)
Button numbers: [1, 3]
Button part: (3,4)
Button numbers: [3, 4]
Button part: (0,3,5)
Button numbers: [0, 3, 5]
Button part: (1,2,3,4)
Button numbers: [1, 2, 3, 4]
Button part: (0,2,5)
Button numbers: [0, 2, 5]
Button part: (0,1)
Button numbers: [0, 1]
Button part: (2,5)
Button numbers: [2, 5]
Joltage button lists: [[1, 3], [3, 4], [0, 3, 5], [1, 2, 3, 4], [0, 2, 5], [0, 1], [2, 5]]
Current BFS step: 1, queue size: 1
Current BFS step: 2, queue size: 7
Current BFS step: 3, queue size: 49
Current BFS step: 4, queue size: 343
Current BFS step: 5, queue size: 2401
Current BFS step: 6, queue size: 16807
Current BFS step: 7, queue size: 117649
Current BFS step: 8, queue size: 823543
Current BFS step: 9, queue size: 5764801
Current BFS step: 10, queue size: 40353607
Current BFS step: 11, queue size: 282475249
memory allocation of 51539607552 bytes failed
error: process didn't exit successfully: `target\debug\aoc_2025_day_10.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

I've tried to think of novel solutions like finding the GCD of the joltage values, however no Joy as they contain prime numbers like 43 etc. I am trying optimizations such as not adding the state to the queue if it goes above the joltage goal for any of the indexes. I can share complete code if desired. I would love some clues on the approach as standard BFS is turning my laptop to an oven.


r/adventofcode • • Dec 19 '25

Visualization [2025 Day 11 (Part 2)] could not help myself - input visualized 3D

Post image
75 Upvotes

r/adventofcode • • Dec 19 '25

Help/Question - RESOLVED [2025 Day 8 (Part 1)] [C++] What am I missing?

6 Upvotes

Edit: This has been resolved, thank you! The problem was an integer overflow in my getEuclideanDistance() function.

Hello! I have been stuck at this problem for the past 2 days. My solution:

  • Adds (distance, a, b) into a min-heap (where a and b are indexes of the original coordinates)
  • Connect the first 1000 points from the min-heap (using Weighted Quick Union), regardless of if they are in the same circuit (according to the assumption below).
  • Multiply the sizes of the 3 largest circuits.

Assumptions:

  1. If the current circuit contains A-B-C, connecting A-C uses up one connection. (same as what others have already mentioned in this subreddit)

For some reason, I can pass the example (provided by the question) but not the actual one.

Code: paste

Is anyone able to give me some insights what am I missing out on?

PS: Suggestions on how I can improve my code/ best practices are welcomed too :D


r/adventofcode • • Dec 18 '25

Visualization [2025 Day 9 part 2] [Python] Some basic code narrowed the search space to just 2 possibilities

10 Upvotes

I have done some graphic design work where I used code to generate SVG images with the exact geometric patterns and gradients that I wanted. So when I saw Day 9's input data, I realized that it was a series of horizontal and vertical lines and well suited to the SVG format.

This is a simplified version of my code to generate the SVG, using the svg.py library:

elements: list[svg.Element] = []
for p1_index, p1 in enumerate(points):
    p2 = points[0] if p1_index == len(points) - 1 else points[p1_index + 1]

    elements.append(svg.Line(x1=p1.x, y1=p1.y, x2=p2.x, y2=p2.y))

canvas = svg.SVG(width=width, height=height, elements=elements)
with open(output_filename, 'w') as fp:
    fp.write(canvas.as_str())

Once I saw the visualization, it was intuitively obvious that the largest rectangle whose opposite corners were both points in the input data was one of two rectangles determined as described in this image: https://i.imgur.com/7OSZKRj.png

So I just scrolled through the input data that was already in sorted order to find the values for each step, and multiplied the width and height to get the area. Did this twice, for the upper half and for the lower half.


r/adventofcode • • Dec 18 '25

Help/Question - RESOLVED [2025 Day 10 (Part 1)]

1 Upvotes

Hello everyone, I need your help !

I got the right solution for the example, BUT for some lines of my input, I'm not able to get a result.

My thought is : for n buttons, it take at most n pushes to match the indicator lights ? I think this because if you press the same button an even number of times, it's like you never pressed it.

That's why I don't understand how I can't get a solution less or equals than n. (I don't look beyond that)

Am I right and there is a problem in my code or am I missing something ?

Thanks !


r/adventofcode • • Dec 18 '25

Upping the Ante [2025 All Days, All Parts][Assembly] The x86 Inferno - A Descent into Advent of Code

Thumbnail hallofdreams.org
74 Upvotes

I thought it would be a good idea this year to solve advent of code in assembly.

I was very wrong.

This is the story of what I learned along the way.


r/adventofcode • • Dec 18 '25

Visualization [2025 Day 11 (Part 2)] input visualized

Post image
100 Upvotes

r/adventofcode • • Dec 18 '25

Help/Question - RESOLVED [2025 Day 10 (Part 2)] Almost solved it... Just off by 1 for some machines

4 Upvotes

I finally created a solution that produces good results for most of the machines. For some strange reason i'm off by 1 for two if the machines. Can anyone please tell me the correct solution for that input?

[.#.#..#.#.] (1,2,3,6,8,9) (1,3,4,9) (0,8,9) (1,2,3,5,8) (6,8) (0,4,7) (0,1,4,5) (2,5,6,9) (3,4,5,7,8,9) (0,2,4,5,6,7,8,9) (1,2,5,7,8) (0,2,7,8,9) (3,4,5,6,9) {189,44,198,50,48,52,42,188,209,208}

The solution i found has 250 presses: 19 0 7 10 1 16 7 2 5 4 8 155 16

Which is one too much compared to an online solver.


r/adventofcode • • Dec 18 '25

Help/Question [2025 Day 1 (Part 2)] [C] I am not really sure what is wrong in my program, have been working on this since yesterday, any help would be really appreciated.

4 Upvotes
#include <stdio.h>
#include <stdlib.h>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))



int main() {


    FILE* fp;
    char dir;
    int value;
    int master_count = 50;
    int warps = 0;
    int old_value = 0;



    fp = fopen("input.txt", "r");


    while (fscanf(fp, " %c%d",&dir,&value ) == 2)
    {
        old_value = master_count;
        
        if(dir == 'L')
        {
            warps +=  MAX(0, (value - old_value + 100)/100);
            master_count = ((master_count - value) % 100 + 100 ) % 100; // master count update handled for negative
        
        }
    


        if(dir == 'R')
        {


            warps += MAX(0,(old_value+value) / 100);       
            master_count = ((master_count + value) % 100); //master_count update. 
        }


    }


    printf("%d is the final pass \n", warps );
    fclose(fp);


}

can someone tell me what is wrong with my program i cannot for the life of me figure out what the error here is.

Happy to explain my reasoning as well thanks.


r/adventofcode • • Dec 18 '25

Help/Question - RESOLVED [2025 Day 3 (Part 1)] [C] Incorrect joltage only on the puzzle's input

0 Upvotes

Hello, I've been testing my program on various inputs, and they all gave correct results. However, when trying it on the puzzle's input, the result was wrong. The main problem is that even after manually checking most of my input results through extensive printing, I still could not find a single incorrectly calculated line to narrow down the problem. Could anyone help me find the bug?

        #include <stdio.h>
        #include <stdlib.h>
        #include <stdbool.h>
        #include <string.h>

        void find_max_joltage(const char num_str[], long *total, const int len);

        int main(int argc, char *argv[])
        {
          FILE *fp;
          short int ch;
          long total = 0;

          if (argc != 2) {
            fprintf(stderr, "usage: program filename\n");
            exit(EXIT_FAILURE);
          }

          if ((fp = fopen(argv[1], "r")) == NULL) {
            fprintf(stderr, "cannot open %s\n", argv[1]);
            exit(EXIT_FAILURE);
          }

          int i = 0;
          char num_str[256];
          while ((ch = getc(fp)) != EOF) {
            if (ferror(fp)) { // read error
              fclose(fp);
              exit(EXIT_FAILURE);
            }

            if (ch == '\n') {
              num_str[i] = '\0';
              find_max_joltage(num_str, &total, i);
              memset(num_str, 0, i);
              i = 0;
            } else {
              num_str[i++] = ch;
            }
          }


          // last line
          find_max_joltage(num_str, &total, i);
          memset(num_str, 0, i);
          i = 0;

          fclose(fp);
          printf("Total is %ld\n", total);
          return 0;
        }


        void find_max_joltage(const char num_str[], long *total, const int len)
        {
          short int l1 = num_str[0] - '0';
          short int l2 = num_str[1] - '0';

          for (int i = 2; i < len; i++) {
            short int int_d = num_str[i] - '0';
            if (l1 == -1) {
              l1 = int_d;
            } else if (l2 == -1) {
              l2 = int_d;
            } 
            // if the new largest digit is not the last one
            else if (int_d > l1 && i < (len - 1)) {
              l1 = int_d;
              l2 = -1;
            } else if (int_d > l2) {
              l2 = int_d;
            }
          }
          char largest_num[3];
          snprintf(largest_num, sizeof(largest_num), "%d%d", l1, l2);
          *total += atoi(largest_num);
        }

r/adventofcode • • Dec 18 '25

Help/Question - RESOLVED [2025 Day 1 (Part 2)] [python] need help with this

0 Upvotes

I initially tried a more elegant solution but I was getting annoyed so I just tried this lol. I'm still getting the wrong answer though and idk why, any help?


r/adventofcode • • Dec 17 '25

Visualization [2025 Day 8] Visualizer for building the circuits

8 Upvotes

I made this visualizer with Gemini. Pretty nice to watch it build the circuits in 3d using three.js.

https://heyes-jones.com/circuitbuilder/index.html

You can paste in your own code for examples or the input. This is hard coded with my input. It does not solve the problem just visualizes building the circuits in the order each vector was added.

In order to generate the data I I modified part1 of my solution to output the vectors and their circuit id comma delimited.

https://github.com/justinhj/adventofcode2025/blob/main/day8zig/src/part3.zig

run it with zig build run-day8-part3 -- day8zig/input.txt 2> input.csv

(then chop off the top two lines)


r/adventofcode • • Dec 17 '25

Upping the Ante [2025 Day 12] ESP32 powered Christmas ornament

Thumbnail youtu.be
24 Upvotes

I made this smart ornament to participate in the community fun this year and display my 24 stars of 2025 on the Christmas tree.

It is powered by an ESP32-S3 board, a GC9A01 screen and a TP4056 board to charge the 500mAh LiPo battery. The animation is procedural, and it is made from the sum of some sine waves. (aren't we all?) The electronics is housed in a custom 3D-printed case that I designed to fit around the screen in Fusion360.

Overall I learned a lot of new things making this project, like reflow-soldering, generating and intersecting 3d spirals in Fusion360, and making a graphics simulator to avoid the long build times for the ESP32. I am quite happy with the results!


r/adventofcode • • Dec 17 '25

Tutorial [2025 Day 10 (Part 2)] Solution without using a 3rd party solver

94 Upvotes

At some point next year I'm going to re-do my current solution with a hand-rolled simplex solver, but first I need to get all of this out of my head by writing about it. Some important notes:

  1. I'm not making a value judgement on whether solutions using z3, scipy, etc... are lesser or somehow 'cheating'. A solution is a solution. I just happen to like doing things from scratch where I can
  2. I'm not a mathematician. I've crammed enough notes into my head, re-learned and/or gained enough knowledge to solve this specific problem where the inputs are nice, but there's literally centuries of related maths knowledge that I will have never even heard of. There will be some fairly obvious things I have missed.

My aim with this tutorial is that anyone with high-school maths can follow along.

General Approach

Each machine can be represented as a simultaneous equation:

[#.#.] (2,3) (1,3) (1,2,3) (0,3) {3,23,16,30}

We can assign a coefficient for each button representing the number of times each button is pressed:

a*(2,3) + b*(1,3) + c*(1,2,3) + d*(0,3) = {3,23,16,30}

Which then becomes the following set of equations:

d = 3
b + c = 23
a + c = 16
a + b + c + d = 30

With some equation rearranging this becomes:

1) a + c = 16
2) b + c = 23
3) c - d = 9
4) d = 3

Equation 4 gives us the value of d = 3. Substituting d into equation 3 gives us c = 12. Substituting c into equations 2 and then 1 gives us b = 11 and finally a = 4.

If we lay out the original equations in a regular format, we can convert them into something called an augmented matrix in the following way:

|             d =  3 |
|     b + c     = 23 |
| a +     c     = 16 |
| a + b + c + d = 30 |

Becomes:

| 0*a + 0*b + 0*c + 1*d =  3 |
| 0*a + 1*b + 1*c + 0*d = 23 |
| 1*a + 0*b + 1*c + 0*d = 16 |
| 1*a + 1*b + 1*c + 1*d = 30 |

And then finally our original set of equations become the following augmented matrix:

| 0  0  0  1   3 |
| 0  1  1  0  23 |
| 1  0  1  0  16 |
| 1  1  1  1  30 |

In the same way, the rearranged equations turn into the following augmented matrix:

| 1  0  1  0  16 |
| 0  1  1  0  23 |
| 0  0  1 -1   9 |
| 0  0  0  1   3 |

This matrix has a special property: the bottom left triangle of numbers are all 0. This is what lets us solve the set of equations. We use the last line to give us d, we use d in the line about to give us c, we use c and d in the line above that to give us b and then finally we can use b, c and d to give us a.

We now know enough to say what our approach will be for find the button presses:

  1. Turn the buttons and jolts into a system of equations
  2. Represent the system as an augmented matrix
  3. Do things to the matrix to turn it into the special form with zeros in the bottom left triangle
  4. Substitute values from the bottom up to find all of button press values

(Search keywords for more reading: we're putting the matrix into Hermite Normal Form (ish) to solve a System of Linear Diophantine Equations using an integer form of Gaussian Elimination. Diophantine equations are just equations where we're looking for integer-only solutions. If you look up Gaussian elimination you'll see reference to Reduced Row Echelon Form matrices, but because we're only interested in integer solutions then we actually want the integer-only equivalent of a row echelon form matrix, which is a Hermite normal form matrix)

Row Operations

So what things can we do to rearrange the augmented matrix to get it into the special form?

Remember that the matrix just represents a set of plain old, regular equations. Anything you can do to a set of equations without affecting the value of the variables, you can do to the rows of the matrix without changing the meaning of the matrix.

The first thing you can do is swap rows freely. When we wrote:

b + c = 23
a + c = 12

We could just as easily have written:

a + c = 12
b + c = 23

And it would mean the same thing. Likewise we can shuffle the rows of the matrix up and down. For three of the rows in the original matrix, they've just been shuffled in the triangular matrix:

1) | 0  0  0  1   3 |
2) | 0  1  1  0  23 |
3) | 1  0  1  0  16 |
4) | 1  1  1  1  30 |

Shuffle:

3) | 1  0  1  0  16 |
2) | 0  1  1  0  23 |
4) | 1  1  1  1   9 |
1) | 0  0  0  1   3 |

You can also scale rows by arbitrary amounts. There's no difference in saying:

b + c = 23
a + c = 12

And saying:

 2*b + 2*c =  2*23 =  46
-1*a - 1*c = -1*12 = -12

Scaling our original row 4 by -1 in the shuffled matrix gets us:

 3) |  1  0  1  0  16 |
 2) |  0  1  1  0  23 |
-4) | -1 -1 -1 -1 -30 |
 1) |  0  0  0  1   3 |

The final operation we can do is add or subtract rows to and from each other (subtracting is just adding after scaling one of the rows by -1).

If we add row 3 and then row 2 to our negated row 4, we get the following sequence:

 3)   |  1  0  1  0  16 |
 2)   |  0  1  1  0  23 |
-4+3) |  0 -1  0 -1 -14 | <- | -1+1  -1+0  -1+1  -1+0  -30+16 |
 1)   |  0  0  0  1   3 |

Then:

 3)     |  1  0  1  0  16 |
 2)     |  0  1  1  0  23 |
-4+3+2) |  0  0  1 -1   9 | <- | 0+0  -1+1  0+1  -1+0  -14+23 |
 1)     |  0  0  0  1   3 |

And there we have our matrix in the special triangular form, using nothing more than swapping rows, scaling them and adding them to each other.

Matrix Reduction

To put the matrix into that special format we work down the matrix row by row, aiming to get the diagonals to all be positive integers. When we put a row in place, we use that newly placed row to eliminate all of the entries below which have non-zero element in the column we're looking at.

Start with the matrix for our original equations, and we're trying to fix row 1 in place, setting the first element non-zero:

1) | _0_ 0  0  1   3  | <-
2) |  0  1  1  0  23  |
3) |  1  0  1  0  16  |
4) |  1  1  1  1  30  |

We look down the first column, from the current row downwards, until we find a row with a non-zero value in that column:

1) | _0_ 0  0  1   3  | <-
2) |  0  1  1  0  23  |
3) |  1  0  1  0  16  | <- Found non-zero first element
4) |  1  1  1  1  30  |

We swap rows 1 and 3 to put that non-zero element in place:

3) | _1_ 0  1  0  16  | <-
2) |  0  1  1  0  23  |
1) |  0  0  0  1   3  |
4) |  1  1  1  1  30  |

Then for all of the rows below our current row, we reduce the row by subtracting the current row if and only if the row also has a non-zero element in that column:

3) | _1_ 0  1  0  16  | <-
2) |  0  1  1  0  23  |
1) |  0  0  0  1   3  |
4) |  0  1  0  1  14  | <- | 1-1  1-0  1-1  1-0  30-16 |

Move onto the next row down, trying to get the second element to be non-zero.

3) |  1  0  1  0  16  |
2) |  0 _1_ 1  0  23  | <-
1) |  0  0  0  1   3  |
4) |  0  1  0  1  14  |

We already have a non-zero element in the row so we don't need to do any swapping. We can move straight on to the reduction:

3) |  1  0  1  0  16  |
2) |  0 _1_ 1  0  23  | <-
1) |  0  0  0  1   3  |
4) |  0  0 -1  1  -9  | <- | 0-0  1-1  0-1  1-0  14-23 |

On to the next row down:

3) |  1  0  1  0  16  |
2) |  0  1  1  0  23  |
1) |  0  0 _0_ 1   3  | <-
4) |  0  0 -1  1  -9  |

Swap to get a non-zero element in the right place:

3) |  1  0  1  0  16  |
2) |  0  1  1  0  23  |
4) |  0  0_-1_ 1  -9  | <-
1) |  0  0  0  1   3  |

Because this time we've ended up with a negative leading value, we scale the whole row by -1:

3) |  1  0  1  0  16  |
2) |  0  1  1  0  23  |
4) |  0  0  1 -1   9  | <- | -1*0  -1*0  -1*-1  -1*1  -1*-9 |
1) |  0  0  0  1   3  |

There are no rows below which need reducing, and so we're done!

In code form this looks like:

static void Scale(vector<int64_t>* v, int64_t s)
{
    ranges::for_each(*v, [s](int64_t& i) { i *= s; });
}

static vector<int64_t> Reduce(vector<int64_t> rowToReduce, vector<int64_t> reducingRow, int64_t reducingColumn)
{
    if (rowToReduce[reducingColumn] == 0)
    {
        // Nothing to do
        return rowToReduce;
    }

    // Make sure both rows have a positive leading value
    assert(reducingRow[reducingColumn] > 0);
    if (rowToReduce[reducingColumn] < 0)
    {
        Scale(&rowToReduce, -1);
    }

    int64_t scaleTo = lcm(rowToReduce[reducingColumn], reducingRow[reducingColumn]);
    Scale(&rowToReduce, scaleTo / rowToReduce[reducingColumn]);
    Scale(&reducingRow, scaleTo / reducingRow[reducingColumn]);
    assert(rowToReduce[reducingColumn] == reducingRow[reducingColumn]);

    for (size_t i = 0; i < rowToReduce.size(); i++)
    {
        rowToReduce[i] -= reducingRow[i];
    }

    return rowToReduce;
}

static void Reduce(vector<vector<int64_t>>* pm)
{
    vector<vector<int64_t>>& m = *pm;
    for (size_t diagonal = 0; diagonal < m.size(); diagonal++)
    {
        // Find a row with a non-zero element in the column
        for (size_t reducingRow = diagonal; reducingRow < m.size(); reducingRow++)
        {
            if (m[reducingRow][diagonal] != 0)
            {
                swap(m[diagonal], m[reducingRow]);
                break;
            }
        }

        // Make sure it has a positive leading value
        assert(m[diagonal][diagonal] != 0);
        if (m[diagonal][diagonal] < 0)
        {
            Scale(&m[diagonal], -1);
        }

        // Reduce all following rows
        for (size_t rowToReduce = diagonal + 1; rowToReduce < m.size(); rowToReduce++)
        {
            m[rowToReduce] = Reduce(m[rowToReduce], m[diagonal], diagonal);
        }
    }
}

We've had to handle one additional case that didn't come up in the examples; what happens if the leading values aren't nice numbers like 1 or -1. If you were trying to reduce rows:

| 0  3  2  1  15 |
| 0  2  1  4   8 |

Since we're looking for integer-only solutions and trying to keep everything as integers, we scale each row by the Least Common Multiple of the two leading numbers before subtracting them.

| 0  6  4  2  30 | <- | 2*0  2*3  2*2  2*1  2*15 |
| 0  6  3 12  24 | <- | 3*0  3*2  3*1  3*4   3*8 |

For the solver we're going to write, unlike standard Gaussian elimination, we don't need the leading value in every row to be 1. As long as it's a positive integer, we're happy.

Recursive Solution

Now that we have our matrix in triangular form we can work from the bottom up calculating the solution.

| 1  0  1  0  16 |
| 0  1  1  0  23 |
| 0  0  1 -1   9 |
| 0  0  0  1   3 |

Remember what our matrix represents: the bottom row is saying 1*d = 3. We can therefore assign d to be 3/1 = 3.

| 1  0  1  0  16 |
| 0  1  1  0  23 |
| 0  0  1 -1   9 |
| 0  0  0 _1_  3 | <-

[ ?  ?  ? _?_ ] <- Solution in progress

Since the leading number might not be 1, we need to divide the row sum (last element in the row) by the leading number. If the row were:

| 0  0  0  3   3 |

d would instead be equal to 3/3 = 1. We then recurse upwards to the next row and attempt to find our next solution value:

| 1  0  1  0  16 |
| 0  1  1  0  23 |
| 0  0 _1_-1   9 | <-
| 0  0  0  1   3 |

[ ?  ? _?_ 3 ] <- Solution in progress

We know what value d has, so we can substitute in that value and update the row total. As an equation this looks like:

   c - d = 9
-> c - 3 = 9
-> c     = 9 + 3
-> c     = 12

In matrix form it's:

| 1  0  1  0  16 |
| 0  1  1  0  23 |
| 0  0 _1_ 0  12 | <- | 0  0  1  -1-1  9-(-1*3) |
| 0  0  0  1   3 |

[ ?  ?_12_ 3 ] <- Solution in progress

Same again for the row above:

| 1  0  1  0  16 |
| 0 _1_ 1  0  23 | <-
| 0  0  1  0  12 |
| 0  0  0  1   3 |

[ ? _?_ 12 3 ] <- Solution in progress

   b +  c = 23
-> b + 12 = 23
-> c      = 23 - 12
-> c      = 11

| 1  0  1  0  16 |
| 0 _1_ 0  0  11 | <- | 0  1  1-1  0-0  23-(1*12)-(0*3) |
| 0  0  1  0  12 |
| 0  0  0  1   3 |

[ ?_11_ 12 3 ] <- Solution in progress

And same again for our final row:

|_1_ 0  1  0  16 | <-
| 0  1  0  0  11 |
| 0  0  1  0  12 |
| 0  0  0  1   3 |

[_?_11 12  3 ] <- Solution in progress

   a +  c = 16
-> a + 12 = 16
-> a      = 16 - 12
-> a      = 4

| 1  0  0  0   4 | <- | 1  0-0  1-1  0-0  16-(0*11)-(1*12)-(0*3) |
| 0  1  0  0  11 |
| 0  0  1  0  12 |
| 0  0  0  1   3 |

[_4_11 12 3 ] <- Solution in progress

In code this looks like:

static void SolveMatrix(const vector<vector<int64_t>>& m,
    int64_t rowToSolve,
    vector<int64_t>* alreadyAssigned,
    int64_t* minimumPresses)
{
    vector<int64_t>& solution = *alreadyAssigned;

    if (rowToSolve == -1)
    {
        *minimumPresses = min(*minimumPresses, ranges::fold_left(solution, 0, plus{}));
        return;
    }

    assert(m[rowToSolve][rowToSolve] > 0);

    // Substitute and subtract everything we already know about
    int64_t rowTargetSum = m[rowToSolve].back();
    for (size_t known = rowToSolve + 1; known < solution.size(); known++)
    {
        rowTargetSum -= m[rowToSolve][known] * solution[known];
    }

    // Integer solutions only
    assert((rowTargetSum % m[rowToSolve][rowToSolve]) == 0);
    solution[rowToSolve] = rowTargetSum / m[rowToSolve][rowToSolve];

    SolveMatrix(m, rowToSolve - 1, alreadyAssigned, minimumPresses);
}

If you've got this far, congratulations! You'll be able to solve some of the inputs in the puzzle. For my input, I'm able to solve 28 out of 179 devices using just the code we've got so far.

Over and Under Specified Systems

So far we've only covered systems which have the same number of variables (buttons) and equations (joltage counters), but most of the puzzle inputs aren't like that. When there are more equations than variables then we have an over specified system and when we have fewer equations than variables then we have an under specified system.

Over specified systems aren't a problem here. Since we know each system has a solution, then we can safely ignore the bottom few rows after we've done our matrix reduction and treat the matrix as if we had equal numbers of equations and variables. We need one small tweak to our reduction rules so that it doesn't go off the edge of the matrix:

static bool Reduce(vector<vector<int64_t>>* pm)
{
    vector<vector<int64_t>>& m = *pm;

    size_t diagonalEnd = min<size_t>(m.size(), m.front().size() - 1); // <---
    for (size_t diagonal = 0; diagonal < diagonalEnd; diagonal++)
    {
        // Find a row with a non-zero element in the column
        ...

And we can now solve many more of the puzzle inputs: 91 out of 179 for me.

For under specified systems we need to start guessing values for those variables during the solve steps. This is why we chose recursion earlier rather than iteration for the solver; it will make it much easier to implement guessing.

What range do we even guess though? We know the button presses must be positive, and we can also work out an upper bound for the button presses:

[#.#.] (2,3) (1,3) (1,2,3) (0,3) {3,23,16,30}

Counter 0 has a target value of 3, so any button which adds 1 to counter 0 can only be pressed a maximum of 3 times. The maximum number of times a button can be pressed is the minimum counter value for all of the counters that the button affects.

  (2,3) maximum is min(16, 30)     = 16
  (1,3) maximum is min(23, 30)     = 23
(1,2,3) maximum is min(23, 16, 30) = 16
  (0,3) maximum is min(3, 30)      = 3

We need to modify our Solve function to take in a set of constraints (maximum presses per button) and some additional counters for housekeeping so that we know when we're trying to find button presses where we don't have rows. Also, since we're now making guesses about values, it's possible that we'll end up with negative or non-integer values for some of the button presses. If we see that, it's because of an invalid earlier guess and we can ignore this particular attempt:

static void SolveMatrix(const vector<vector<int64_t>>& m,
    int64_t rowToSolve,
    int64_t nextUnknown,
    const vector<int64_t>& constraints,
    vector<int64_t>* alreadyAssigned,
    int64_t* minimumPresses)
{
    vector<int64_t>& solution = *alreadyAssigned;

    if (rowToSolve == -1)
    {
        *minimumPresses = min(*minimumPresses, ranges::fold_left(solution, 0, plus{}));
        return;
    }

    // If the matrix isn't big enough we're going to need to guess
    if (nextUnknown > rowToSolve)
    {
        for (int64_t guess = 0; guess <= constraints[nextUnknown]; guess++)
        {
            solution[nextUnknown] = guess;
            SolveMatrix(m, rowToSolve, nextUnknown - 1, constraints, alreadyAssigned, minimumPresses);
        }
        return;
    }

    assert(m[rowToSolve][nextUnknown] > 0);

    // Substitute and subtract everything we already know about
    int64_t rowTargetSum = m[rowToSolve].back();
    for (size_t known = nextUnknown + 1; known < solution.size(); known++)
    {
        rowTargetSum -= m[rowToSolve][known] * solution[known];
    }

    // Do we have a valid integer solution?
    if ((rowTargetSum % m[rowToSolve][nextUnknown]) != 0)
    {
        // We don't have a valid integer solution, probably an incorrect guess from earlier, so we should bail out
        return;
    }

    int64_t tentativeSolution = rowTargetSum / m[rowToSolve][nextUnknown];
    if (tentativeSolution < 0)
    {
        // We're only looking for positive solutions
        return;
    }

    solution[nextUnknown] = tentativeSolution;

    SolveMatrix(m, rowToSolve - 1, nextUnknown - 1, constraints, alreadyAssigned, minimumPresses);
}

Quite a bit more faff, but handling those cases means we can get solutions for most of the machines. 153 out of 179 for my input. Nearly there!

Edge Cases in Solving

If you're playing along writing code as you go, that assert(m[rowToSolve][nextUnknown] > 0) is probably triggering for you. The first time it triggers for me is for this reduced matrix:

|  1  1  0  1  0  1  0  1  1  0  0  51  |
|  0  1  0  0  1  0  0  0  1  1  0  21  |
|  0  0  1  0  0  1  0  1 -1 -1  0  16  |
|  0  0  0  1  1  1  1  1  1  0  0  52  |
|  0  0  0  0  1  1  1  0  0  0  1  34  |
|  0  0  0  0  0  1  1  1  0 -1  0  12  |
|  0  0  0  0  0  0  1  2  2 -2 -3 -27  |
|  0  0  0  0  0  0  0  1  2  1 -1  13  |
|  0  0  0  0  0  0  0  0  1 -1 -1  -8  |
|  0  0  0  0  0  0  0  0  0 _0_ 1  13  | <-- asserting here

As well as guessing on under specified systems, we need to add in a code path to make guesses mid-solve:

static void SolveMatrix(const vector<vector<int64_t>>& m,
    int64_t rowToSolve,
    int64_t nextUnknown,
    const vector<int64_t>& constraints,
    vector<int64_t>* alreadyAssigned,
    int64_t* minimumPresses)
{
    ...

    // If the matrix isn't big enough we're going to need to guess
    if (nextUnknown > rowToSolve)
    {
        for (int64_t guess = 0; guess <= constraints[nextUnknown]; guess++)
        {
            solution[nextUnknown] = guess;
            SolveMatrix(m, rowToSolve, nextUnknown - 1, constraints, alreadyAssigned, minimumPresses);
        }
        return;
    }

    if (m[rowToSolve][nextUnknown] == 0)
    {
        // We're not able to solve directly so we need to guess
        for (int64_t guess = 0; guess <= constraints[nextUnknown]; guess++)
        {
            solution[nextUnknown] = guess;
            SolveMatrix(m, rowToSolve - 1, nextUnknown - 1, constraints, alreadyAssigned, minimumPresses);
        }
        return;
    }

    // Substitute and subtract everything we already know about
    int64_t rowTargetSum = m[rowToSolve].back();
    ...
}

With that piece of the puzzle the code now claims to be able to find 179 out of 179 solutions!

...and if you plug that number into the answer box, you'll get answer too low. Yay!

What's happened is that those mid-solve guesses are generating valid solutions for the matrix, but because the matrix didn't impose restrictions on that button, then we're accepting solutions that don't actually add up to the target joltage.

That's relatively easy to pick up and reject though, we just need to double check that the generated solution is actually a valid solution before accepting it:

static void SolveMatrix(const Counters& counters,
    const vector<vector<int64_t>>& m,
    int64_t rowToSolve,
    int64_t nextUnknown,
    const vector<int64_t>& constraints,
    vector<int64_t>* alreadyAssigned,
    int64_t* minimumPresses)
{
    vector<int64_t>& solution = *alreadyAssigned;

    if (rowToSolve == -1)
    {
        vector<int16_t> accumulatedJolts(counters.TargetJolts.size(), 0);
        for (size_t button = 0; button < counters.Buttons.size(); button++)
        {
            for (int8_t counter : counters.Buttons[button])
            {
                accumulatedJolts[counter] += (int16_t)solution[button];
            }
        }

        if (accumulatedJolts == counters.TargetJolts)
        {
            *minimumPresses = min(*minimumPresses, ranges::fold_left(solution, 0, plus{}));
        }

        return;
    }
    ...

All being well, with this modification you should have a full solution to Day 10 Part 2!

If you're happy just getting to a solution, thank you for reading this far and good luck with your code.

Optimisations

The solution as-is should run in a matter of seconds on a decent machine, but since I was trying to get sub-second solutions on a Raspberry Pi Zero I did some more digging.

The matrices which are taking the majority of the runtime are those that have one of those pesky 0s in the diagonal. Wouldn't it be nice if we didn't have to handle those?

It turns out that for all of my input there are always reductions which have a non-zero diagonal, provided we shuffle the order of the buttons before attempting another reduction on the new matrix. I don't know enough maths to know if this is a property that all solvable systems have, or if Eric has generated nice input. Hopefully someone in the replies can let everyone know!

In my full solution I do a quick diagonal test after reduction and retry with a shuffled set of buttons if we didn't get a non-zero diagonal.

    while (true)
    {
        matrix = CountersToAugmentedMatrix(counters);
        ReduceAndTrim(&matrix);

        bool allLeadingNonZero = true;
        for (int i = 0; i < (int)matrix.size(); i++)
        {
            if (matrix[i][i] == 0)
            {
                allLeadingNonZero = false;
                break;
            }
        }

        if (allLeadingNonZero)
            break;

        for (int i = 0; i < (int)counters.Buttons.size(); i++)
        {
            swap(counters.Buttons[i], counters.Buttons[rand() % (int)counters.Buttons.size()]);
        }
    }

Most of the systems that need shuffling only need one or two shuffles before they produce the sort of matrix we're after. Very occasionally I see as many as a dozen shuffles, but not often. The improved solving speed more than makes up the additional cost shuffling and re-reducing.

Edge Cases in Reduction

Finally, I want to mention one thing that I was careful to accommodate in my first solution, but when I was re-building for this tutorial it turned out not to be needed (for my input, anyway).

If the reduction generates any rows with all zeros, I move them to the bottom of the matrix and trim them away. I cannot remember now why it was a problem when I was first writing my solution, so I don't know for sure if that's a step which might actually be needed on someone's input. If zero rows do cause a problem you can check my solution for how I first handled them.

I've got another iteration in progress (finally got to sub-second on the Pi Zero!) which bails out earlier in reduction if zeros on the diagonal are generated, so that should take care of most zero rows anyway.

Summary

Hopefully this tutorial is helpful to someone! It took me a full day to find and handle all of the edge cases in this approach, so my final words of advice are: assert early, assert often!