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
class Day10 : Puzzle {
lateinit var machines: List<Machine>
override fun readFile() {
val input = readInputFromFile(2025, 10, false)
machines = input.lines().filter { it.isNotBlank() }.map { Machine(it) }
}
private fun buildMatrix(machine: Machine) = machine.buttons.map { List(machine.lights.size) { i -> i in it } }
override fun solvePartOne(): String {
var sum = 0L
for (machine in machines) {
val matrix = buildMatrix(machine)
val iterationLimit = machine.buttons.size
val cache = mutableMapOf<Triple<List<Boolean>, List<Boolean>, Int>, Long>()
fun traverseRow(row: List<Boolean>, lights: List<Boolean>, presses: Int = 0): Long {
val key = Triple(row, lights, presses)
if (key in cache) {
return cache[key]!!
} else if (row == lights) {
return 1L
} else if (presses == iterationLimit) {
return Long.MAX_VALUE
} else if (lights.indices.filter { lights[it] }.none { row[it] }) {
return Long.MAX_VALUE
} else {
val newLights = lights.zip(row) { a, b -> a xor b }
val min = matrix.filter { it != row }.minOf { traverseRow(it, newLights, presses + 1) }
val out = 1L + if (min == Long.MAX_VALUE) Long.MAX_VALUE - 1L else min // careful with MAX + 1
cache[key] = out
return out
}
}
sum += matrix.minOf { traverseRow(it, machine.lights) }
}
return sum.toString()
}
override fun solvePartTwo(): String {
return machines.sumOf(::getMachineSum).toString()
}
private fun getMachineSum(machine: Machine): Int {
val buttonMatrix = buildMatrix(machine) // one row corresponds to one button
val joltageMatrix = mirrorMatrix(buttonMatrix) // one row corresponds to one joltage counter
.map { r -> r.map { if (it) 1 else 0 } }
val model = ExpressionsBasedModel()
val buttons = buttonMatrix.map { row ->
row.mapIndexedNotNull { i, b -> if (b) i else null }.minOf { machine.joltages[it] }
}.mapIndexed { i, button ->
model.newIntegerVariable("button$i").lower(0).upper(button)
}
// minimize sum over all buttons
buttons.forEach { model.objective.set(it, 1) }
// joltageMatrix @ buttons = machine.joltages
for (r in joltageMatrix.indices) {
val row = model.newConstraint("row$r").level(machine.joltages[r])
joltageMatrix[r].forEachIndexed { i, j -> row.set(buttons[i], j) }
}
model.minimise()
return buttons.sumOf { it.intValue() }
}
data class Machine(val inputString: String) {
val lights: List<Boolean>
val buttons: List<List<Int>>
val joltages: List<Int>
init {
val input = inputString.split("] ").flatMap { it.split(" {") }
lights = input[0].filter { it != '[' }.map { it == '#' }
buttons = input[1].split(" ").map { listStr ->
listStr.filter { it != '(' && it != ')' }.split(",").map { it.toInt() }
}
joltages = input[2].filter { it != '}' }.split(",").map { it.toInt() }
}
}
}
full code on Codeberg