Still not working, but stuff removed that was wrong.
This commit is contained in:
parent
4982f2cb3c
commit
bd591263f6
@ -128,62 +128,42 @@ 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(
|
||||||
pressCount[t.idx] += t.min
|
jolts,
|
||||||
|
t,
|
||||||
if (odds.contains(t)) {
|
times = t.min
|
||||||
// make sure that the buttons that will lead to a solution have an odd count
|
)
|
||||||
if (pressCount[t.idx] and 1 == 0) {
|
) throw IllegalStateException()
|
||||||
if (++pressCount[t.idx] > t.max || !applyJoltage(jolts, t)) continue@newodd
|
pressCount[t.idx] += t.min
|
||||||
}
|
}
|
||||||
} else {
|
// this is the starting point for the exhaustive search
|
||||||
// make sure that the buttons that will destroy the solution have an even count
|
val pq = PriorityQueue(compareBy<Pair<IntArray, IntArray>> { it.second.sum() })
|
||||||
if (pressCount[t.idx] and 1 == 1) {
|
pq.add(jolts to pressCount)
|
||||||
if (++pressCount[t.idx] > t.max || !applyJoltage(jolts, t)) continue@newodd
|
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
|
for (t in toggles) {
|
||||||
val pq = LinkedList<Pair<IntArray, IntArray>>()
|
if (tc[t.idx] + 2 <= t.max) {
|
||||||
pq.add(jolts to pressCount)
|
val maxTimes = findMaxButtonPresses(j, t)
|
||||||
while (pq.isNotEmpty()) {
|
if (maxTimes > 0) {
|
||||||
val (j, tc) = pq.poll()
|
val nj = j.copyOf()
|
||||||
val pushes = tc.sum()
|
if (applyJoltage(nj, t, maxTimes)) {
|
||||||
if (pushes >= minPushes) break
|
val ntc = tc.copyOf()
|
||||||
// if the joltage has counted down to zero, we're done
|
ntc[t.idx] += maxTimes
|
||||||
if (j.sum() == 0) {
|
pq.add(nj to ntc)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user