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 tom-tom
from itertools import product
def wall_keeper(on_panels):
def index(i, j):
return i + j * 5 + 1
def press(i, j):
result.append(index(i, j))
for x, y in (i, j), (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1):
if x in range(5) and y in range(5):
lights[y][x] = not lights[y][x]
def press_row(row, j):
for i, value in enumerate(row):
if value:
press(i, j)
for first_row in product((False, True), repeat=5):
result = []
lights = [[index(i, j) in on_panels for i in range(5)] for j in range(5)]
press_row(first_row, 0)
for j in range(1, 5):
press_row(lights[j - 1], j)
if all(not any(row) for row in lights):
return result
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
from itertools import chain
def checker(solution, on_panels):
answer = solution(on_panels)
wk_p = list((0, 1)[n in on_panels] for n in range(1, 26))
p = list(wk_p[n: n+5] for n in range(0, 25, 5))
for a in answer:
r, c = (a-1) // 5, (a-1) % 5
p[r][c] = 1 - p[r][c]
if r+1 < 5:
p[r+1][c] = 1 - p[r+1][c]
if r-1 > -1:
p[r-1][c] = 1 - p[r-1][c]
if c+1 < len(p[0]):
p[r][c+1] = 1 - p[r][c+1]
if c-1 > -1:
p[r][c-1] = 1 - p[r][c-1]
return sum(chain(*p)) == 0
assert checker(wall_keeper, [5, 7, 13, 14, 18]), 'basic'
assert checker(wall_keeper, list(range(1, 26))), 'all_lights'
Aug. 3, 2017
Comments: