Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Light chasing solution in Clear category for Wall Keeper by Leonix
from collections import defaultdict
def wall_keeper(on_panels):
""" Essentially implements light chasing strategy described in
https://en.wikipedia.org/wiki/Lights_Out_(game)#Light_chasing """
# Stores a bool for each panel, True if toggled odd number of times
output = defaultdict(bool)
on_panels = set(on_panels)
while on_panels:
p = min(on_panels)
if p <= 20:
# For everything except last row we disable light
# by switching a panel on a line below it
output[p+5] = not output[p+5]
on_panels.symmetric_difference_update(panels_around(p+5))
else:
# To solve the last row, we apply mystical knowledge of wise ancestors.
# For each combination of lights still on, there's a combination of panels
# to toggle so that if algorithm is followed again, puzzle will solve.
for p in ANCESTRAL_KNOWLEDGE[frozenset(on_panels)]:
output[p] = not output[p]
on_panels.symmetric_difference_update(panels_around(p))
return [p for p, is_on in output.items() if is_on]
def panels_around(p):
""" Which panels will switch state if given panel is triggered """
result = [p]
if p > 5: result.append(p-5)
if p < 21: result.append(p+5)
if p % 5 != 1: result.append(p-1)
if p % 5 != 0: result.append(p+1)
return result
# What is left on the bottom row => what to toggle to solve the puzzle
ANCESTRAL_KNOWLEDGE = dict([
(frozenset([21,25]), [1, 2]),
(frozenset([22,24]), [1, 4]),
(frozenset([21,22,23]), [2]),
(frozenset([23,24,25]), [4]),
(frozenset([21,23,24]), [5]),
(frozenset([22,23,25]), [1]),
(frozenset([21,22,24,25]), [3]),
])
April 7, 2019
Comments: