Restored deleted code from previous commit and used it to solve Part 1 much faster.

This commit is contained in:
Chris Hodges 2025-12-10 18:44:40 +01:00
parent bd591263f6
commit ccc3fdad4c

View File

@ -41,23 +41,17 @@ fun main() {
val machines = parse(input) val machines = parse(input)
var sumButts = 0 var sumButts = 0
for (m in machines) { for (m in machines) {
val pq = LinkedList<Pair<Int, Int>>() // look at which different possible sets of toggles need to be pressed to result in the target number
val killArray = BooleanArray(1 shl m.size) // pressing an even time will cancel out the effect, so only look what happens if you press once
pq.add(0 to 0) var minPresses = Int.MAX_VALUE
out@ while (pq.isNotEmpty()) { for (tm in 1 until (1 shl m.toggles.size)) {
val (v, bi) = pq.poll() val odds = m.toggles.filterIndexed { i, v -> (1 shl i) and tm != 0 }
for (t in m.toggles) { val result = odds.fold(0) { acc, iv -> acc xor iv }
val nv = v xor t if (result == m.target) {
if (nv == m.target) { minPresses = minPresses.coerceAtMost(odds.size)
sumButts += bi + 1
break@out
}
if (!killArray[nv]) {
killArray[v] = true
pq.add(nv to bi + 1)
}
} }
} }
sumButts += minPresses
} }
return sumButts return sumButts
} }
@ -103,7 +97,8 @@ fun main() {
for (m in machines) { for (m in machines) {
// generate toggles and calculate the global maximum of toggle presses for this toggle // generate toggles and calculate the global maximum of toggle presses for this toggle
val toggles = m.toggles.mapIndexed { i, t -> val toggles = m.toggles.mapIndexed { i, t ->
Toggle(i, Toggle(
i,
t, t,
max = IntRange(0, m.size).filter { b -> t and (1 shl b) != 0 }.minOf { m.joltage[it] }) max = IntRange(0, m.size).filter { b -> t and (1 shl b) != 0 }.minOf { m.joltage[it] })
} }
@ -128,7 +123,6 @@ fun main() {
v.min = minT v.min = minT
} }
// iterate over the possible odd sets
var minPushes = Int.MAX_VALUE var minPushes = Int.MAX_VALUE
val jolts = m.joltage.copyOf() val jolts = m.joltage.copyOf()
val pressCount = IntArray(m.toggles.size) val pressCount = IntArray(m.toggles.size)