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)
var sumButts = 0
for (m in machines) {
val pq = LinkedList<Pair<Int, Int>>()
val killArray = BooleanArray(1 shl m.size)
pq.add(0 to 0)
out@ while (pq.isNotEmpty()) {
val (v, bi) = pq.poll()
for (t in m.toggles) {
val nv = v xor t
if (nv == m.target) {
sumButts += bi + 1
break@out
}
if (!killArray[nv]) {
killArray[v] = true
pq.add(nv to bi + 1)
}
// 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
var minPresses = Int.MAX_VALUE
for (tm in 1 until (1 shl m.toggles.size)) {
val odds = m.toggles.filterIndexed { i, v -> (1 shl i) and tm != 0 }
val result = odds.fold(0) { acc, iv -> acc xor iv }
if (result == m.target) {
minPresses = minPresses.coerceAtMost(odds.size)
}
}
sumButts += minPresses
}
return sumButts
}
@ -103,7 +97,8 @@ fun main() {
for (m in machines) {
// generate toggles and calculate the global maximum of toggle presses for this toggle
val toggles = m.toggles.mapIndexed { i, t ->
Toggle(i,
Toggle(
i,
t,
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
}
// iterate over the possible odd sets
var minPushes = Int.MAX_VALUE
val jolts = m.joltage.copyOf()
val pressCount = IntArray(m.toggles.size)