Solving “Large” Lights-Out Puzzles With Python

Karla Hernández
Random Noise
Published in
13 min readSep 17, 2023

--

Image Generated By Author Using Midjourney

The Lights-Out Puzzle

The lights-out puzzle is pervasive in video games. The premise is simple: you start with a grid of lights each with a switch to turn them on/off. Some lights are on and others are off. The goal is to turn all lights off. The catch? Whenever you flip the switch for any given light, the state (on/off) of the 4 adjacent lights (think above, below, left, and right) is also toggled.

In the grid on the left, if we flip the switch in the center (marked with a dot) then the center light will turn off but 4 other lights (above, below, left, and right) will turn on (see the grid on the right).

Solving 3 x 3 puzzles is not difficult. Here’s one way in which you can turn lights off starting from a configuration where only the center light is on:

The small dot represents the switch that will be flipped next.

Things become a little more complex when dealing with larger puzzles. How would you turn all lights off starting from the next configuration:

How would you turn all lights off starting from this configuration?

A more generalized variant of the lights-out puzzle asks the question: given a starting…

--

--