←back to thread

82 points raymondtana | 10 comments | | HN request time: 0.578s | source | bottom

"Lights Out" is a mathematical puzzle that lives on an grid where each cell of the grid is one of two colors: either red or white. The goal is to eventually get all the cells in the grid to be red.

What's the catch? Clicking a cell will not only flip its color, but also that of all cells sharing the same row or column as it.

To me, this game feels like playing with a Rubik's cube --- every time you think you are fixing one cell, you mess up its neighbors!

There are many ways to arrive at a solution... some mathematical (linear algebra, or combinatorics), others more logical,... and yet others which are brute force.

The title “Lights Out” comes from a classic handheld game from 1997. The “rule” they follow for which-cells-get-flipped-on-click is what I call “Adjacent.” Additionally, my mathematics teacher showed me this game following another variant which I call “Same Row & Column,” but on a bigger board. He had worked out an algorithm for his version. I found the same strategy before he revealed the answer, and I feel that the process of discovering a solution is quite rewarding—it’s fundamentally related to computing on restrictive computer architectures.

I implemented this app with pretty basic TypeScript. It’s been used for some experiments to discover more general of strategies for different click variants, board sizes, and even board dimensions! It has also been the basis for the corresponding video produced using the Python library manim.

Try it out and let me know how you do!

1. adamschwartz ◴[] No.45541433[source]
Memorize which cells are white when the board first loads. Tap all of those in order without regard to the way the board changes as you go.

Edit: above tested for 5x5 rows&cols. For even boards it seems there’s a small end-game to repeat the process — something about parity I assume.

replies(3): >>45541495 #>>45541691 #>>45541985 #
2. rprenger ◴[] No.45541495[source]
And you can do that at any board state, so if it starts with like 16 white squares you can make one or two greedy moves to minimize white squares, then do your memorize trick.
replies(1): >>45541608 #
3. adamschwartz ◴[] No.45541608[source]
Yea that can shave a few moves off.

For fun you can also, for example, invert any board in N moves by tapping every cell straight across any row or column.

4. o-o- ◴[] No.45541691[source]
Interesting, and black magic as far as I'm concerned. How does that algorithm translate onto the Rubik's cube (which I evidently never learned to solve)?
5. patrickdavey ◴[] No.45541985[source]
Why does that work?
replies(1): >>45542201 #
6. primitivesuave ◴[] No.45542201[source]
If you think of each button press as a matrix being added to the board state where only the row and column are set to 1, along with the commutative nature of the moves (order doesn't matter), then as long as the total number of "flips" from the cumulative matrices of moves is odd, then it will reset the board.

Mathematically I might say that the system's precomputed solution vector is readily apparent.

replies(2): >>45542342 #>>45549129 #
7. 0x1ceb00da ◴[] No.45542342{3}[source]
What if there was only one white block on the grid?
replies(2): >>45542455 #>>45543438 #
8. adamschwartz ◴[] No.45542455{4}[source]
The game is initialized with a guaranteed solvable board:

https://github.com/RaymondTana/Lights_Out/blob/31fe5e866c45c...

9. bradrn ◴[] No.45543438{4}[source]
I’m pretty sure this case is solvable too. Click the white block, then click all the blocks which turned white after that. This flips each block twice (bringing them back to their original state), except for the original white block which was only flipped once.
10. furyofantares ◴[] No.45549129{3}[source]
I think this only works with an odd grid size. With an even grid size you might have to do it twice.