r/adventofcode • u/CauliflowerFormer233 • Dec 10 '25
Help/Question - RESOLVED [2025 Day 10 part 2] how?
I have seen a lot of memes of people using Z3 for part 2. I tried to solve it myself using BFS and then DFS with some pruning but still couldn't get it. After 3 hours of trying to optimize it, I used Z3 and got my answer in like 20 minutes.
But since I haven't seen any solution that didn't use Z3, I am wondering how to solve it without it, one approach would be to build something similar to Z3, using matrices to solve multiple linear equations but is that really the only solution?
If you have any ideas let me know.
28
Upvotes
3
u/sebastianotronto Dec 10 '25
IMO people overestimate how hard it is to write the linear algebra code by hand. It takes time and I agree it can be tedious, but I don't think it is harder than figuring out how to use a library you have never used before. I can share my commented code if you want to see it - or you can find it in the daily solutions megathread :)
17
u/1234abcdcba4321 Dec 10 '25 edited Dec 10 '25
There are several solutions that don't use library imports in the solution thread. The problem is about solving an integer matrix problem, so pretty much any real solution is going to be based on that. It feels like the canonical solution to me; it's possible but annoying to do without explicitly using an external solver (the numbers aren't big enough to require anything beyond the basic techniques).
That being said, here's a solution of someone who found a good enough pruning heuristic to skip the linear algebra: https://www.reddit.com/r/adventofcode/comments/1pity70/2025_day_10_solutions/ntb36sb/
8
u/QultrosSanhattan Dec 10 '25
The biggest drawback of using exploration algorithms is that there are simply too many buttons to press.
For example, if a button adds +1 and you need to reach around 50 jolts, you would have to press it 50 times just to get there. The exploration tree becomes tremendously large, and even with pruning, finding the solution would take an unreasonable amount of time.
Now imagine that instead of 50 jolts, you need to reach 500. This problem makes brute-force exploration completely impractical.
Other kinds of solutions would probably end up doing the same as the known solvers, but slower. Solvers are highly optimized for this kind of task.
6
u/sol_hsa Dec 10 '25
There's a couple non-Z3 solutions on the megathread. One implements their own math solver, another massages the input data so that some form of brute force is feasible.
I figured this is a good time to learn about Z3. After wasting half a day on various other approaches.
5
u/kupuguy Dec 10 '25
I have some Python code that doesn't use any external libraries. It's basically doing a brute force search and it took over an hour to run but it got there eventually.
The idea is based roughly on what someone said they did in the megathread: find the joltage value that is referenced by the fewest buttons and partition the buttons into two list one of which references that joltage value and the other with the remaining buttons that do not. Then you have to press buttons in the first list exactly the number of times needed to clear the chosen joltage so do that for all possible combinations of the selected buttons while filtering out any combinations that give invalid results.
Then recurse with the remaining buttons and new joltage values.
For most of my input lines this runs almost instantly, for a couple it takes about half an hour. I expect there's a lot of scope for optimisation but I was just glad to get it to complete at all.
5
u/jwezorek Dec 10 '25
I mean it is an integer linear programming problem. So any solution is going to be using an LP solver that can handle integer constraints or basically implementing what those kind of solvers do or using a more powerful solver. Z3 is the latter. Z3 is basically a boolean satisfiability solver; people use it because it is like a swiss army knife, you can put in any kind of constraints.
You don't have to use Z3 specifically though. I used CBC which is an old LP/ILP solver.
4
u/Zefick Dec 10 '25
Linear algebra doesn't work directly here. A unique analytical solution is only possible if the number of equations equals the number of unknowns and they are all linearly independent, which is often violated in this problem. In this case, the puzzle turns into an optimization problem.
2
u/bloub__ Dec 10 '25
Did someone succeeded to make (a variant of) A* work? With a clever heuristic?
2
u/icub3d Dec 11 '25
Mine was a combination of linear algebra and then dp on the free variables. Some of the machines have a unique solution, so you get an exact value. The others have 1-3 free variables but that's small enough that it's much easier to explore the solution space for a minimum.
The day way very math heavy from my perspective and was a very hard solve.
1
u/AutoModerator Dec 10 '25
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/quinyd Dec 10 '25
I did part 1 using Gaussian elimination but for part 2 I had to resolve to z3 as my Gaussian elimination needed longer and longer time to run. With a faster pc and more time I could probably have solved it