Skip to content

Commit a8f0e84

Browse files
Update docs on Sun Dec 14 08:37:28 UTC 2025
1 parent 7e92f16 commit a8f0e84

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

2025/10/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,14 @@ <h2 id="problem-name">Factory</h2>
284284
<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>
285285
</ul>
286286
<p>This is a textbook integer linear programming problem, trivially solved by an ILP solver, or in <a href="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+
<h2>The Gaussian way</h2>
287288
<p>I&#39;m a purist in the sense that I don&#39;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>
288289
<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>
289290
<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>
290291
<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>
291292
<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>
292293
<p>I prototyped <a href="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>
293-
<h2>Update</h2>
294+
<h2>A recursive solution</h2>
294295
<p>Based on the ingenious idea of <a href="https://www.reddit.com/r/adventofcode/comments/1pk87hl/2025_day_10_part_2_bifurcate_your_way_to_victory/">tenthmascot</a>, I could create a plain C# implementation for the problem.</p>
295296
<p>As he says: find all possible sets of buttons you can push so that the remaining voltages are even,
296297
and divide by 2 and recurse.</p>

0 commit comments

Comments
 (0)