[Math] Method for solving a 5×5 light puzzle

matricesnumber theory

There is a $5 \times 5$ square of lights, of which some number are on and off. "Selecting" a light changes the state of the selected light and all four lights adjacent to it, in effect, changing the state of the lights in a plus centered on the selected light. The ultimate goal is to have every light in the puzzle in its "off" state. Is there a surefire algorithm to solve this using something like a binary matrix? If no, what is the most efficient method to solve this puzzle with only one light on, in the center?

Best Answer

Represent a configuration as a $25$-dimensional vector over $\mathbb{F}_2$, the field with two elements. Each component of the vector corresponds to one of the lights. $1$ represents on, and $0$ represents off.

You can also represent a sequence of moves as a $25$-dimensional vector over $\mathbb{F}_2$ because of two properties:

  1. Toggling a light twice has the same effect as not toggling it at all.
  2. Toggling a set of lights in any order has the same effect.

So the component corresponding to a certain light represents the number times you select it $\pmod{2}$.

Now, let $A$ be the adjacency matrix of the $5\times 5$ rectangular grid graph with $1$'s in the diagonal. Let $\vec{x}$ be the unknown solution to a given initial configuration $\vec{b}$. Then you need to solve the system of $25$ equations $\vec{b}+A\vec{x}=\vec{0}$ which is equivalent to $A\vec{x}=\vec{b}$ over $\mathbb{F}_2$. In your specific problem, you need to set $\vec{b}$ to the vector where the component corresponding to the light in the center is a $1$ and the rest are $0$'s. Solve the system of equations. You may get no solution, a unique solution, or many solutions. If you get many solutions, compare each of the solutions directly to figure out which solution involves pressing the fewest lights.

It so happens that there are $4$ solutions to your problem, and they are all equivalent to each other by rotation. Here's one of these $4$ equally optimal solutions, which toggles $11$ lights: $$\begin{matrix}1&1&0&0&0\\0&0&1&0&0\\1&0&1&1&0\\1&0&0&0&1\\0&1&1&0&1\end{matrix}$$

P.S. I've actually been studying the mathematics of Lights Out for a few years. The topic fascinates me and I'm trying to publish a paper about fractal patterns in the solutions to larger boards of size $2^n\times 2^n$. There are many different classes of board sizes whose solutions give fractal patterns, and my profile picture is the pattern that emerges from boards of size $7(2^n)-1\times 7(2^n)-1$.

Related Question