Still not working, but stuff removed that was wrong.

This commit is contained in:
Chris Hodges 2025-12-10 17:02:28 +01:00
parent 4982f2cb3c
commit bd591263f6

View File

@ -128,42 +128,23 @@ fun main() {
v.min = minT 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<Set<Toggle>>()
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 // iterate over the possible odd sets
var minPushes = Int.MAX_VALUE var minPushes = Int.MAX_VALUE
newodd@ for (odds in oddset) {
val jolts = m.joltage.copyOf() val jolts = m.joltage.copyOf()
val pressCount = IntArray(m.toggles.size) val pressCount = IntArray(m.toggles.size)
// press all buttons regarding their minimal count (if any) // press all buttons regarding their minimal count (if any)
for (t in toggles) { for (t in toggles) {
if (pressCount[t.idx] + t.min > t.max || !applyJoltage(jolts, t, times = t.min)) continue@newodd if (pressCount[t.idx] + t.min > t.max || !applyJoltage(
jolts,
t,
times = t.min
)
) throw IllegalStateException()
pressCount[t.idx] += t.min 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
}
}
} }
// this is the starting point for the exhaustive search // this is the starting point for the exhaustive search
val pq = LinkedList<Pair<IntArray, IntArray>>() val pq = PriorityQueue(compareBy<Pair<IntArray, IntArray>> { it.second.sum() })
pq.add(jolts to pressCount) pq.add(jolts to pressCount)
while (pq.isNotEmpty()) { while (pq.isNotEmpty()) {
val (j, tc) = pq.poll() val (j, tc) = pq.poll()
@ -188,7 +169,6 @@ fun main() {
} }
} }
} }
}
println("$minPushes") println("$minPushes")
sumButts += minPushes sumButts += minPushes
} }