Game of Life
Real-world version: One Tick of a Lamp GridThe statement here is our own: the same inputs, outputs and technique.
A display panel holds lamps, each either lit as 1 or dark as 0. A lamp's neighbours are the cells touching it along an edge or at a corner, so up to eight of them.
On one tick every lamp changes at the same instant, and each one looks only at how many of its neighbours were lit just before the tick:
- a lit lamp with fewer than two lit neighbours goes dark;
- a lit lamp with two or three lit neighbours stays lit;
- a lit lamp with more than three lit neighbours goes dark;
- a dark lamp with exactly three lit neighbours comes on.
Return the panel as it stands after one tick.
Examples
Example 1
- Input
- panel = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
- Output
- [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
The middle lamp of the lit row keeps two lit neighbours and holds. The two ends have only one each and go dark. The cells straight above and below the middle each touch all three lit lamps, so they come on, leaving the row standing upright.
Example 2
- Input
- panel = [[1, 1], [1, 1]]
- Output
- [[1, 1], [1, 1]]
Each lamp touches the other three, so every one of them has three lit neighbours and stays lit. Nothing changes.
Example 3
- Input
- panel = [[0, 1, 0], [1, 1, 0], [0, 0, 1]]
- Output
- [[1, 1, 0], [1, 1, 1], [0, 1, 0]]
The dark top-left cell touches three lit lamps and comes on. The lit lamp in the bottom-right corner touches only one and goes out. Two more dark cells, the one right of centre and the one below centre, each touch exactly three lit lamps and come on.
Constraints
- 1 <= panel.length <= 25
- 1 <= panel[i].length <= 25
- 0 <= panel[i][j] <= 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def game_of_life(panel: list[list[int]]) -> list[list[int]]:public int[][] gameOfLife(int[][] panel)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Transpose MatrixReal-world version: Flip a Reading Grid Onto Its SideEasy
- Shift 2D GridReal-world version: Rolling the Tile Board ForwardEasy
- Find Winner on a Tic Tac Toe GameReal-world version: Who Comes Out Ahead on the Three-by-ThreeEasy