Advent of Code 2025 - Day 10Factory
| Problem statement | Source code | Tags: BFS/DFSMathematics
Part 1
See this problem as a graph problem. Each light state is a node, and each button press is a directed edge to another node. We want to find the shortest path from the initial state to the target state. This can be done with a BFS. As a micro-optimization, I used a bitset to represent the state of the lights.
Part 2
I think this is the hardest problem of the year (of which there are few). Essentially, we have a list of button presses . For each joltage , button either works or doesn't work for that joltage (denoted by ). This means solving the system of equations:
And we'd like to minimize . This is a linear programming problem. (Note that an exact solution only exists if .) Of course I can implement Simplex myself, which I actually did once but in Haskell, but I don't want to do that again. I just used good_lp with its default microlp solver.
To define an LP problem, we need to define the variables, the constraints, and the objective function. The variables are . The first set of constraints is that for all . The second set, perhaps less obvious, is that for all such that —because more presses would always overshoot.
The objective function is simply .
The final set of constraints is the system of equations above.
Then we just call solve() and extract the value of the objective function.
Of course this is a bit of cheat, but I just don't want to spend an hour implementing an LP solver myself.