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,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<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
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<Pair<IntArray, IntArray>> { 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<Pair<IntArray, IntArray>>()
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)
}
}
}