Visualizations
429 / 500

429 · Math

Lights Out Algebra

The toggle-tile puzzle solved instantly at any size by linear algebra over GF(2).

Pressing a tile toggles it and its neighbors, and since order never matters and two presses cancel, a solution is a set of tiles: a bit vector x with A x = b, where column j of A is what tile j toggles and addition is XOR. Gauss-Jordan elimination over the two-element field, with rows packed into 32-bit words, solves any board in well under a millisecond, and the panel replays the elimination column by column on the matrix [A | b] while the answer appears as rings on the board. When A is singular its kernel holds quiet patterns, press sets that change nothing; because A is symmetric a board is solvable exactly when it overlaps every quiet pattern in an even number of lights, so only 1 in 2^k boards can be solved, and the fewest-press answer is found by trying all 2^k combinations. Square, toroidal, hexagonal and king-move boards change A and its kernel completely.

Try it. Click tiles to press them and watch the ringed answer update live. Edit lights (or Shift-click or right-click) toggles a single light, which can make the board unsolvable and shows which quiet patterns it overlaps oddly. Change the size with minus and plus (or the up and down arrows), switch between Square, Torus, Hex and King boards (G), Scramble (R), Solve (S or Space) and Hide answer (H). Left alone, it scrambles and solves boards of every kind.

  • Gaussian elimination over GF(2)
  • Bit-packed row operations
  • Kernel and parity analysis
  • Pre-rendered glow sprites

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build the Lights Out puzzle with a built-in solver, using JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Use a near-black background.
- Draw a 5 by 5 grid of rounded tiles. Lit tiles are warm amber, unlit tiles dark slate.
- Clicking a tile toggles it and its four orthogonal neighbors.
- Add a Scramble button that presses 10 random tiles, so the board is always solvable.
- Solve it with linear algebra over GF(2): build the 25 by 25 matrix A where column j marks the tiles that pressing j toggles, append the lit pattern b as an extra column, and run Gauss-Jordan elimination where adding rows means XOR. Read off which tiles to press and mark them with a ring.

Once that works, make it beautiful:
- Give lit tiles a soft glow (prerender a lit and an unlit tile to offscreen canvases and drawImage them), and animate toggles as a quick flip with a ripple spreading from the pressed tile.
- Draw the matrix [A | b] beside the board as a grid of tiny squares and replay the elimination one column at a time.
- On 5 by 5 the matrix has rank 23, so some boards cannot be solved. Find the two "quiet patterns" (the kernel: press sets that change nothing), draw them as mini boards, and explain that a board is solvable only if it overlaps each quiet pattern in an even number of lights.

Explain the GF(2) trick in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as other board sizes, wrap-around or hexagonal boards, or choosing the solution with the fewest presses.
PreviousSokoban ForgeCrate puzzles forged by playing backward from the goals, then solved by A*. NextWind WalkerA six-legged beach machine walks on Theo Jansen's eleven-bar linkage.

Related visualizations

  • Gridlock SolverAlgorithms Map every position of a sliding-car puzzle, solve it, and generate harder ones.
  • Dancing LinksAlgorithms Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
  • Sliding Puzzle SolverGames IDA* with pattern databases solves the 15-puzzle in the fewest possible moves.
  • Nonogram ForgeAlgorithms Paints a pixel picture, proves its clues have one answer, then solves it line by line.
  • Rebound RobotsAlgorithms Robots slide until they hit something. A* finds the fewest moves to the target.
  • Wave Function CollapseAlgorithms A tile-based world that assembles itself one consistent cell at a time.
  • Constraint SolverAlgorithms A sudoku solver reasons in pencil, guesses in ink, and erases when it is wrong.
  • Tessellation MorphMath Drag one edge of an Escher-style creature and every copy reshapes while still tiling.
  • Prism PuzzleGames Split white light with rutile prisms to light colored targets. A search AI plays.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs