|
| 1 | +package y25 |
| 2 | + |
| 3 | +import common.puzzle.solvePuzzle |
| 4 | +import common.puzzle.Input |
| 5 | +import common.puzzle.Puzzle |
| 6 | +import common.datastructures.* |
| 7 | +import common.ext.* |
| 8 | +import common.util.* |
| 9 | +import java.util.* |
| 10 | +import kotlin.math.* |
| 11 | +import kotlin.system.exitProcess |
| 12 | +import com.microsoft.z3.* |
| 13 | + |
| 14 | + |
| 15 | +fun main() = solvePuzzle(year = 2025, day = 10) { Day10(it) } |
| 16 | + |
| 17 | +class Day10(val input: Input) : Puzzle { |
| 18 | + |
| 19 | + class Machine( |
| 20 | + val diagram: BooleanArray, |
| 21 | + val buttons: List<List<Int>>, |
| 22 | + val joltage: IntArray, |
| 23 | + ) |
| 24 | + |
| 25 | + private fun parseInput(): List<Machine> { |
| 26 | + return input.lines.map { parseMachine(it) } |
| 27 | + } |
| 28 | + |
| 29 | + private fun parseMachine(line: String): Machine { |
| 30 | + val regex = "\\[(.+)] (.+) \\{(.*)}".toRegex() |
| 31 | + val result = regex.find(line)!! |
| 32 | + val diagram = result.groups[1]!!.value.map { it == '#' }.toBooleanArray() |
| 33 | + val buttons = result.groups[2]!!.value.split(" ").map { button -> |
| 34 | + button.trim('(', ')').split(",").map { it.toInt() } |
| 35 | + } |
| 36 | + val joltage = result.groups[3]!!.value.split(",").map { it.toInt() }.toIntArray() |
| 37 | + return Machine(diagram, buttons, joltage) |
| 38 | + } |
| 39 | + |
| 40 | + private fun fewestPresses(machine: Machine): Int { |
| 41 | + class Node(val lights: BooleanArray, val dist: Int) { |
| 42 | + override fun equals(other: Any?): Boolean { |
| 43 | + return other is Node && other.lights.contentEquals(lights) |
| 44 | + } |
| 45 | + |
| 46 | + override fun hashCode(): Int { |
| 47 | + return lights.contentHashCode() |
| 48 | + } |
| 49 | + } |
| 50 | + val visited = mutableSetOf<Node>() |
| 51 | + val queue = PriorityQueue<Node>(compareBy { it.dist }) |
| 52 | + queue += Node(BooleanArray(machine.diagram.size), 0) |
| 53 | + |
| 54 | + while (queue.isNotEmpty()) { |
| 55 | + val next = queue.poll() |
| 56 | + if (next in visited) { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + if (next.lights.contentEquals(machine.diagram)) { |
| 61 | + return next.dist |
| 62 | + } |
| 63 | + visited += next |
| 64 | + |
| 65 | + machine.buttons.forEach { button -> |
| 66 | + val newLights = next.lights.copyOf() |
| 67 | + button.forEach { i -> newLights[i] = !newLights[i] } |
| 68 | + queue.add(Node(newLights, next.dist + 1)) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return -1 |
| 73 | + } |
| 74 | + |
| 75 | + private fun fewestJoltagePressesLinearSolver(machine: Machine): Int { |
| 76 | + val ctx = Context() |
| 77 | + |
| 78 | + // Variables |
| 79 | + // n_i = amount of button presses of machine.buttons[i] |
| 80 | + // For each counter c_i, sum of buttons b_j with c_i * n_j = z_i |
| 81 | + val n = List(machine.buttons.size) { index -> |
| 82 | + ctx.mkIntConst("n_$index") |
| 83 | + } |
| 84 | + |
| 85 | + val constraints = mutableListOf<BoolExpr>() |
| 86 | + machine.joltage.forEachIndexed { joltIndex, jolt -> |
| 87 | + val buttonVars = machine.buttons |
| 88 | + .mapIndexed { btnIndex, btn -> |
| 89 | + btnIndex to btn |
| 90 | + } |
| 91 | + .filter { (_, btn) -> joltIndex in btn } |
| 92 | + .map { (btnIndex, _) -> n[btnIndex] } |
| 93 | + val sum = ctx.mkAdd(*buttonVars.toTypedArray()) |
| 94 | + val constraint = ctx.mkEq( |
| 95 | + ctx.mkReal(jolt), |
| 96 | + sum, |
| 97 | + ) |
| 98 | + constraints += constraint |
| 99 | + } |
| 100 | + |
| 101 | + val zero = ctx.mkReal(0) |
| 102 | + n.forEach { btn -> |
| 103 | + constraints += ctx.mkGe(btn, zero) |
| 104 | + } |
| 105 | + |
| 106 | + val sum = ctx.mkAdd(*n.toTypedArray()) |
| 107 | + |
| 108 | + val optimize = ctx.mkOptimize() |
| 109 | + constraints.forEach { optimize.Add(it) } |
| 110 | + optimize.MkMinimize(sum) |
| 111 | + |
| 112 | + val result = optimize.Check() |
| 113 | + if (result == Status.SATISFIABLE) { |
| 114 | + val model = optimize.model |
| 115 | + val result = model.evaluate(sum, false) as IntNum |
| 116 | + return result.int |
| 117 | + } |
| 118 | + |
| 119 | + error("not satisfiable") |
| 120 | + } |
| 121 | + |
| 122 | + override fun solveLevel1(): Any { |
| 123 | + val machines = parseInput() |
| 124 | + return machines.sumOf { fewestPresses(it) } |
| 125 | + } |
| 126 | + |
| 127 | + override fun solveLevel2(): Any { |
| 128 | + val machines = parseInput() |
| 129 | + return machines.sumOf { |
| 130 | + fewestJoltagePressesLinearSolver(it).also { println(it) } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments