You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<divid="notes"><p><em>Just across the hall, you find a large factory. Fortunately, the Elves here have plenty of time to decorate. Unfortunately, it’s because the factory machines are all offline, and none of the Elves can figure out the initialization procedure.</em></p>
276
276
<p><em>The Elves do have the manual for the machines, but the section detailing the initialization procedure was eaten by a requirements for each machine.</em></p>
277
277
<p><em>Visit the website for the full story and the <ahref="https://adventofcode.com/2025/day/10">full puzzle</a> description.</em></p>
278
-
<p>The first half of the problem was totally fine: we just had to find the combination with the minimal number of button presses.</p>
279
-
<p>However, Part 2 was, in my view, against the spirit of Advent of Code.</p>
280
-
<p>It’s clearly a linear algebra problem. The buttons form the matrix <code>A</code>. Each button gets a column in <code>A</code> with zeros and ones corresponding to the bits of the button.</p>
281
-
<p>The desired joltage becomes the vector <code>b</code>.</p>
282
-
<p>Then one needs to solve <code>Ax = b</code> such that the components of <code>x</code> are non-negative integers and <code>Σ xᵢ</code> is minimal.</p>
283
-
<p>This is a textbook integer linear programming problem, trivially solved by an ILP solver, or in <ahref="solve.py">my case</a> z3. But this is not how Advent of Code has worked in the last 10+ years, so I was hoping for some shortcut, maybe a special structure of the input, but no luck. This really seems to be the intended way.</p>
284
-
<p>I did find another path, though, which is not that good-looking. One can notice that <code>A</code> is almost always full rank, and the kernel space has at most 2–3 dimensions.</p>
285
-
<p>This means we can solve the equation using Gaussian elimination, which will give us a solution with the columns in the kernel set to 0. There is no guarantee that the solution is integer, or that all <code>xᵢ</code> are non-negative, though.</p>
286
-
<p>But once the columns in the base and the kernel are identified, we form a square matrix <code>B</code> and move the remaining columns to <code>K</code>, then deal with <code>Bx = b − Ky</code>.</p>
278
+
<p>The first half of the problem was totally fine: we just had to find the combination with the minimal number of button presses. However, Part 2 was, in my view, against the spirit of Advent of Code.</p>
279
+
<p>It’s clearly a linear algebra problem as follows: </p>
280
+
<ul>
281
+
<li>The buttons form the matrix <code>A</code>. </li>
282
+
<li>Each button gets a column in <code>A</code> with zeros and ones corresponding to the bits of the button.</li>
283
+
<li>The desired joltage becomes the vector <code>b</code>.</li>
284
+
<li>Solve <code>Ax = b</code> such that the components of <code>x</code> are non-negative integers and <code>Σ xᵢ</code> is minimal.</li>
285
+
</ul>
286
+
<p>This is a textbook integer linear programming problem, trivially solved by an ILP solver, or in <ahref="solve.py">my case z3</a>. But this is not how Advent of Code has worked in the last 10+ years, so I was hoping for some shortcut, maybe a special structure of the input... But no luck, ILP really seems to be the intended way.</p>
287
+
<p>I'm a purist in the sense that I don't use external dependencies in my C# solutions, so something like <em>OR-tools</em> is out of scope. I did find another path, though, which is not that good-looking, but workable.... </p>
288
+
<p>One can notice that <code>A</code> is almost always full (column) rank, and the kernel space has at most 2–3 dimensions. This means we can solve the equation using Gaussian elimination, which will give us a solution with the columns in the kernel set to 0. There is no guarantee that the solution is integer, or that all <code>xᵢ</code> are non-negative, though.</p>
289
+
<p>But once the columns in the base and the kernel are identified, we can form a square matrix <code>B</code> and move the remaining columns to <code>K</code>, then deal with <code>Bx = b − Ky</code>.</p>
287
290
<p>Say that <strong>K</strong> has 3 columns. Now we can start brute-forcing by setting the values in <strong>y</strong> to some low integers: <code>[1,0,0]</code>, <code>[0,1,0]</code>, <code>[0,0,1]</code>, then <code>[1,1,0]</code>, <code>[1,0,1]</code>, <code>[0,1,1]</code>, <code>[2,0,0]</code>, <code>[0,2,0]</code>, <code>[0,0,2]</code>, slowly increasing the sum of the values and solving for each combination.</p>
288
291
<p>This way we eventually get a feasible solution for the original problem, say with the total button-press count equal to 100 or so. Then it’s enough to continue the above iteration while the sum of <code>yᵢ</code> is ≤ 100. That gives us a termination condition. During the search we might run into better solutions, which further lowers the upper bound.</p>
289
-
<p>I <ahref="gauss.py">prototyped this idea</a> in Python with ChatGPT. I think, if anything, this could be ported to C#, but I don’t feel the urge. Although it uses first principles that one could potentially implement, it’s a much slower solution than the one using z3.</p>
292
+
<p>I prototyped <ahref="gauss.py">this idea</a> in Python with ChatGPT. I think, if anything, this could be ported to C#, but I don’t feel the urge. Although it uses first principles that one could potentially implement, it’s a much slower solution than the one using z3.</p>
0 commit comments