From bd591263f6e11f24d022f0301d80f6e20d687c16 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Wed, 10 Dec 2025 17:02:28 +0100 Subject: [PATCH] Still not working, but stuff removed that was wrong. --- src/aoc2025/Day10.kt | 84 +++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/src/aoc2025/Day10.kt b/src/aoc2025/Day10.kt index 2f6ec59..fa2af32 100644 --- a/src/aoc2025/Day10.kt +++ b/src/aoc2025/Day10.kt @@ -128,62 +128,42 @@ fun main() { v.min = minT } - // look at which different possible sets of toggles need to be pressed to result in the target number - // pressing an even time will cancel out the effect, so only look what happens if you press once - val oddset = ArrayList>() - for (tm in 1 until (1 shl toggles.size)) { - val odds = toggles.withIndex().filter { (i, _) -> (1 shl i) and tm != 0 }.map { it.value }.toSet() - val result = odds.fold(0) { acc, iv -> acc xor iv.v } - if (result == m.target) { - oddset.add(odds) - } - } - // iterate over the possible odd sets var minPushes = Int.MAX_VALUE - newodd@ for (odds in oddset) { - val jolts = m.joltage.copyOf() - val pressCount = IntArray(m.toggles.size) + val jolts = m.joltage.copyOf() + val pressCount = IntArray(m.toggles.size) - // press all buttons regarding their minimal count (if any) - for (t in toggles) { - if (pressCount[t.idx] + t.min > t.max || !applyJoltage(jolts, t, times = t.min)) continue@newodd - pressCount[t.idx] += t.min - - if (odds.contains(t)) { - // make sure that the buttons that will lead to a solution have an odd count - if (pressCount[t.idx] and 1 == 0) { - if (++pressCount[t.idx] > t.max || !applyJoltage(jolts, t)) continue@newodd - } - } else { - // make sure that the buttons that will destroy the solution have an even count - if (pressCount[t.idx] and 1 == 1) { - if (++pressCount[t.idx] > t.max || !applyJoltage(jolts, t)) continue@newodd - } - } + // press all buttons regarding their minimal count (if any) + for (t in toggles) { + if (pressCount[t.idx] + t.min > t.max || !applyJoltage( + jolts, + t, + times = t.min + ) + ) throw IllegalStateException() + pressCount[t.idx] += t.min + } + // this is the starting point for the exhaustive search + val pq = PriorityQueue(compareBy> { it.second.sum() }) + pq.add(jolts to pressCount) + while (pq.isNotEmpty()) { + val (j, tc) = pq.poll() + val pushes = tc.sum() + if (pushes >= minPushes) break + // if the joltage has counted down to zero, we're done + if (j.sum() == 0) { + minPushes = pushes + break } - // this is the starting point for the exhaustive search - val pq = LinkedList>() - pq.add(jolts to pressCount) - while (pq.isNotEmpty()) { - val (j, tc) = pq.poll() - val pushes = tc.sum() - if (pushes >= minPushes) break - // if the joltage has counted down to zero, we're done - if (j.sum() == 0) { - minPushes = pushes - break - } - for (t in toggles) { - if (tc[t.idx] + 2 <= t.max) { - val maxTimes = findMaxButtonPresses(j, t) - if (maxTimes > 0) { - val nj = j.copyOf() - if (applyJoltage(nj, t, maxTimes)) { - val ntc = tc.copyOf() - ntc[t.idx] += maxTimes - pq.add(nj to ntc) - } + for (t in toggles) { + if (tc[t.idx] + 2 <= t.max) { + val maxTimes = findMaxButtonPresses(j, t) + if (maxTimes > 0) { + val nj = j.copyOf() + if (applyJoltage(nj, t, maxTimes)) { + val ntc = tc.copyOf() + ntc[t.idx] += maxTimes + pq.add(nj to ntc) } } }