14
π - 2025 DAY 10 SOLUTIONS - π
(programming.dev)
An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Everybody Codes is another collection of programming puzzles with seasonal events.
Solution Threads
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
Kotlin
Day 10, the end of daily solutions for me⦠This puzzle really broke me, part 1 as well as part 2.
For part 1, I eventually just tried all options with memoization. Not very pretty, not very fast (between 5 and 10 seconds runtime), but works reliably enough. My new part 2 is likely applicable to part 1 too, but I am just so done with this one.
Part 2. Wow. After two nights of unsuccessful brute-force, I saw the linear algebra pretty quickly: The matrix of the buttons (A) times a solution vector (x) is equal to the joltage counts (b). At first glance, this is a very simple $Ax = b$ system of linear equations. This got me 30 / 200 machines, with the Kotlin equivalent of numpy (multik). For the rest, I tried optimizing my brute-force approach in combination with a timeout. Any machine which ran into a timeout, I would manually construct the matrix and put it into a matrix calculator. As you may be able to guess, I couldn't for the life of me figure out how to solve an underdetermined system in code. This matrix calculator approach worked well for simpler cases, but was horribly slow. For more complex cases, I figured out later that I often did not pick the most optimal solution. So I decided to try my hand at it with Python: After some research, I figured out that sympy can indeed solve underdetermined systems the same way my matrix calculator website did! So now I no longer had to manually type the matrix into the website, my Python code would parse the input and show me the vectors. This was still very error-prone though.
Eventually, I figured out that scipy has a thing called "optimization algorithms". That led me to a first, very horribly looking Python solution using scipy's milp. After I grasped the concept a bit better, I made a much cleaner Python solution with linprog.
Now that I understood the problem, I wanted to make it work in Kotlin. If you looked at my repo in the past, you will notice that none of my solutions use any external libraries. I really wanted to stick to this, but at the end this problem had been killing me for a week and I just wanted it to be done. ojAlgo to the rescue! With this wonderful java library, my part 2 is very concise and readable, while also being pretty fast.
Sorry for the part 2 rambling, here's the solution:
Solution
full code on Codeberg