Lights Out is a grid of lights, and the goal is to make every light dark. A press toggles the pressed light and the lights directly above, below, left and right of it.
Click a light to press it. NEW BOARD deals a fresh board of the chosen SIZE, and NEW BOARDS ARE chooses whether every board is solvable or any board may be dealt.
HINT lights the next press of the minimum solution, and SOLVE shows all of it. STEP and AUTO play the shown solution one press at a time, and the panels below the board show the elimination that finds it.
This page uses no search and no learning. Pressing a light is addition mod 2, meaning only the remainder after division by 2 is kept, so 1 + 1 = 0. The rules therefore form a linear system with one equation per light, and Gaussian elimination solves that system exactly. The solver reports whether a board can be cleared and, if it can, the press set with the fewest presses. HINT shows the first press of that set and SOLVE shows all of it.
Cells are numbered row by row from the top left, 0 to N − 1, where N = n × n is the number of cells on an n × n board. The board is a vector b with b_i = 1 when cell i is lit and 0 when it is dark. A press pattern is a vector x with x_j = 1 when cell j is pressed. A press toggles the pressed cell and its orthogonal neighbours, the cells directly above, below, left and right of it. The set of cells that one press toggles is its neighbourhood1.
GF(2) is the set {0, 1} with addition mod 2, which is XOR (1 ⊕ 1 = 0), and with multiplication AND. Every press is one XOR of a neighbourhood into the board, so presses combine by XOR and their order does not matter. Pressing a cell twice cancels, which is why a minimum solution never presses a cell twice.
Here A is the N × N matrix whose row i is the neighbourhood N(i) of cell i, and x and b are the press pattern and the board. The equation for cell i says that the presses on the cells of N(i) XOR to the lit state b_i, so that cell ends dark. The matrix is symmetric, because j \in N(i) exactly when i \in N(j).
Here P is the set of pressed cells, N(p) is the neighbourhood of cell p, and b' is the board after the presses. The code stores every board, neighbourhood and press set as a bit mask, an integer whose bit i is 1 when cell i is in the set. A press is then one XOR of a mask into the board.
Gaussian elimination solves a linear system by row operations, each of which keeps the set of solutions. Gauss-Jordan elimination adds one step: it clears the pivot column in every other row, above as well as below. The result is the reduced row echelon form, in which every pivot column holds a single 1. The augmented matrix [A \mid b] is A with b added as one extra column on the right.
A pivot is a row chosen to hold the leading 1 of a column. A column with no pivot is a free column, and its variable may be set freely. The number of pivots is the rank of A. The solver works down the columns from 0 to N − 1. For each column it searches the rows from the current rank downward for a 1 in that column, and swaps the row it finds up to the rank4.
Each row is one Python integer, with bit j holding column j and bit N holding the right-hand side. A row operation is then one XOR. Clearing a column uses the rule below, which adds one equation to another and so keeps the solutions unchanged5.
Here \text{row}_{\text{pivot}} is the pivot row, which is not changed. Each such row s loses its 1 in the pivot column. The earlier pivot columns stay clear, because the pivot row has a 0 in each of them.
The solver writes each step as a Step: the column, the pivot row, the swap made, and the rows cleared. The server returns the steps only for boards up to 7 × 7, because the comment there says the trace grows roughly with n⁴7. The ELIMINATION panel replays the steps on the starting matrix, so it can show any step the player chooses8.
function ELIMINATE(A, b): # Gauss-Jordan mod 2 on [A | b]
rows ← each row of [A | b] as an integer
rank ← 0
for each column c from 0 to N − 1:
find a row r at or below rank with a 1 in column c
if there is no such row:
continue # column c is free
swap row r with row rank
for each row s ≠ rank with a 1 in column c:
row s ← row s ⊕ row rank # clears column c above and below
rank ← rank + 1
return rows, rank
function ELIMINATE(n, board): # board: N cells, bit i = cell i
N ← n · n
rhs ← bit(N) # the right-hand side bit
for each cell i from 0 to N − 1:
rows[i] ← neighbourhood(n, i) OR (rhs if cell i is lit else 0)
start ← each row of rows as N + 1 characters of 0 and 1
pivots ← empty list
steps ← empty list
rank ← 0
for each column c from 0 to N − 1:
mark ← bit(c)
found ← the first row r from rank to N − 1 with rows[r] AND mark ≠ 0, or none
if found is none:
store Step(c, none, none, empty) # free column: a null-space direction
continue
swap ← none
if found ≠ rank:
swap rows rank and found
swap ← (rank, found)
pivot_row ← rows[rank]
cleared ← every row r ≠ rank with rows[r] AND mark ≠ 0 # above and below
for each r in cleared:
rows[r] ← rows[r] ⊕ pivot_row
append c to pivots
store Step(c, rank, swap, cleared)
rank ← rank + 1
return rows, pivots, rank, steps, start
The simple version is the whole method: find a pivot in each column, swap it up, and clear the column everywhere else. The full version adds the bookkeeping the code keeps. Each row starts with its neighbourhood, so the right-hand side is the cell's own lit state.
A free column gets a Step with no pivot, because its variable is one of the free choices that generate the null space. The row swap and the cleared rows are recorded so the visualizer can replay each step. The clearing loop runs over every other row, not only the rows below, which is what makes each pivot column a single 1.
Example. On a blank 5 × 5 board, the matrix has rank 23 of 25, so the elimination ends with two free columns. Those two columns are the null-space directions described below.
The boards that can be reached from the dark board are the column space of A, the set of all vectors A\,x. Each press adds one column of A, so pressing moves a board only within its class, the set of boards that differ from it by a column combination. A board in another class can never be cleared.
The solver checks solvability after the elimination. Rows from the rank on have no pivot, so their left side is all zeros. If one of those rows has a 1 on the right, it reads 0 = 1 and no press pattern satisfies it10.
Here \text{rhs}_r is the right-hand side bit of reduced row r. The test is exact, so it has no tolerance. Solvability is an invariant, a property that no move changes: no sequence of presses can carry a board out of its class, so a board that cannot be cleared stays that way however it is pressed.
The reachable boards are a fixed fraction of all boards. There are 2rank reachable boards out of 2N in total. On 5 × 5 the rank is 23, so one board in four is reachable (223 ÷ 225 = 1/4)11.
A null-space vector is a press pattern v with A\,v = 0. Pressing exactly the cells of v changes no light, so the presses cancel. The nullity is the dimension of the null space, which is the number of vectors in a basis, a smallest set of vectors from which every null vector is a combination. Gauss-Jordan elimination gives the nullity directly, because the free columns are the directions in which a solution can move without changing the board12.
Here \mathcal{S} is the set of solutions of a solvable board, and |\mathcal{S}| is its size. The code reports the size as Solution.count. On 5 × 5 the nullity is 2, so every solvable board has 4 solutions. The brute force alternative would try all 225 (about 33 million) press sets on 5 × 5. The solver never does this, since it builds only the 2nullity solutions.
Here x_0 is one particular solution, and \text{piv}(r) is the pivot column of row r. Setting every free press to 0 makes each pivot press equal to its row's right-hand side, since the reduced row has no other pivot columns.
Here e_c is the vector with a 1 only in cell c, written bit(c) in the code, and v_f is the null-space vector for free column f. Setting f to 1 with the other free presses at 0 forces each pivot press to the bit that its row holds in column f. A reduced pivot row reads x_{\text{piv}(r)} plus the free presses it contains equal to its right-hand side, which is 0 for a null vector.
Every solution is x_0 XOR a combination of the null-space vectors v_f. The code builds the list by doubling: for each vector v, it appends every existing solution XORed with v, which gives all 2nullity combinations.
function NULL-BASIS(rows, pivots):
basis ← empty list
for each free column f: # a column with no pivot
v ← bit(f)
for each pivot row r:
if bit f of rows[r] is 1:
v ← v ⊕ bit(pivots[r])
append v to basis
return basis
function NULL-BASIS(n, rows, pivots):
pivot_cols ← the set of values in pivots
basis ← empty list
for each column f from 0 to N − 1:
if f is in pivot_cols:
continue # only free columns give a basis vector
v ← bit(f) # set the free variable to 1
for each pivot row r, with pivot column pivots[r]:
if bit f of rows[r] is 1:
v ← v OR bit(pivots[r]) # this pivot must be 1 as well
append v to basis
return basis
The full version does not use a separate null-space routine: the solver calls it with the reduced rows, and null_space(n) calls the same function on the empty board. The code uses OR here. Each pivot column differs from f and from the other pivot columns, so no bit is set twice, and OR and XOR give the same vector.
The solver returns the lightest solution, the one with the fewest presses. Solutions are compared by a pair: first the number of presses, then the integer value of the press mask. The second key makes ties deterministic, so the same board always gives the same answer18.
Here |x| is the number of presses in x, which the code gets with bit_count(), and the sum is the integer whose bit c is x_c. Pairs are compared in order, first by |x| and then by the sum. HINT shows the first cell of x^{*} in increasing order, and SOLVE shows the whole set20.
function SOLVE(board):
rows, rank ← ELIMINATE(A, board) # reduce [A | b] mod 2
for each row r from rank to N − 1:
if the right-hand side of row r is 1:
return unsolvable # the row reads 0 = 1
x ← the particular solution, free presses 0
solutions ← x ⊕ each combination of the NULL-BASIS vectors
return the solution with the fewest presses
function SOLVE(n, board, trace): # board: N values, each 0 or 1
if board does not have N values, each 0 or 1:
raise ValueError("board must be N cells of 0 or 1")
rows, pivots, rank, steps, start ← ELIMINATE(n, board)
nullity ← N − rank
keep steps and start if trace is set # the visualizer replays them
for each row r from rank to N − 1: # no pivot: the left side is all 0
if the right-hand side of row r is 1:
return unsolvable, with rank and nullity
x0 ← 0
for each pivot row r:
if the right-hand side of row r is 1:
x0 ← x0 OR bit(pivots[r]) # free presses stay 0
solutions ← [x0]
for each v in NULL-BASIS(n, rows, pivots):
solutions ← solutions + [s ⊕ v for each s in solutions] # doubles the list
best ← the s in solutions with the least (POPCOUNT(s), s)
presses ← the cells c in increasing order with bit c of best set
sizes ← the sorted values of POPCOUNT(s) over solutions
return solvable, rank, nullity, presses, sizes, trace
The simple version shows the decision: it either reports the 0 = 1 contradiction or returns the lightest of the solutions. The full version adds the checks and outputs the page uses: the input is validated, the elimination steps are kept only when a trace is requested, and the list of solution sizes is returned along with the presses.
Setting the free presses to 0 gives x_0, and doubling the list adds every null-space combination. The solution with the fewest presses is found by scanning the list once with the key above. This is why the solver never tries all 225 press sets on 5 × 5.
Example. Pressing a1, b2 and d4 on a dark 5 × 5 board lights nine cells. Its four solutions use 3, 13, 13 and 19 presses, and the lightest is a1, b2, d4, which is the press set that made the board.
NEW BOARD deals a board with random_board. In solvable mode, each cell is pressed with probability one half, and the chosen presses are applied to a dark board. The result is solvable by construction, because pressing the same set again undoes the presses, since every press is its own inverse22.
In any mode, each light is lit with probability one half, independently of the others. Most of those boards cannot be cleared. A board that is already dark is rejected and drawn again23. The board sizes, 3 to 9 with 5 as the default, are limits set in the board module24.
Pressing a light never calls the server. The page keeps the shown plan valid instead. A press on cell p toggles N(p) on the board, and the plan changes by the single cell p, so the plan is toggled at p rather than solved again25.
Here e_p is the vector with a 1 only at p, so A\,e_p is the neighbourhood N(p). Since A\,x' = A\,x \oplus A\,e_p = b \oplus A\,e_p = b', the new plan is still a solution. In the code, toggling p means removing it from the plan if it is there and adding it otherwise.
STEP and AUTO press the first cell of the plan, so the plan shrinks by one each time. Their presses count as assists, not as the player's moves27.
Each value below comes from the solver's elimination on a board with no lit cells, which gives the rank and nullity of A, and from the solvable share 2rank/2N9. Nothing here is tuned or learned.
| Board | What it measures | Value |
|---|---|---|
| 3x3 | Rank of A, nullity, solvable share | rank 9 of 9, nullity 0, all boards solvable, 1 solution |
| 4x4 | Rank of A, nullity, solvable share | rank 12 of 16, nullity 4, 1 in 16 boards solvable, 16 solutions |
| 5x5 (default) | Rank of A, nullity, solvable share | rank 23 of 25, nullity 2, 1 in 4 boards solvable, 4 solutions |
| 6x6, 7x7, 8x8 | Rank of A, nullity, solvable share | full rank, nullity 0, all boards solvable, 1 solution |
| 9x9 | Rank of A, nullity, solvable share | rank 73 of 81, nullity 8, 1 in 256 boards solvable, 256 solutions |
The 5 × 5 solution sizes were measured by re-running the code: 3,000 solvable boards from random_board(5, random.Random(1), solvable=True). The lightest solution averages 9.89 presses, and the gap between the heaviest and the lightest solution averages 5.19 presses. With seed 0 on the same number of boards, the averages are 9.83 and 5.24, so the averages depend on the sample. No committed result file covers Lights Out28.
The tests compare the solver with brute force. Brute force tries every press set and records the lightest one per board. The check covers all 512 boards of 3 × 3 and 1,500 random boards of 4 × 42930. Another test checks that the 5 × 5 null space has two vectors, that each changes no light, and that a solvable board has exactly 4 solutions31.