Game of Life
A grid of cells is a tiny world. Each cell is alive (1) or dead (0). The next generation is computed from the current one by looking at each cell's eight neighbours (horizontal, vertical and diagonal; cells outside the grid count as dead):
- A live cell with fewer than 2 live neighbours dies (too lonely).
- A live cell with 2 or 3 live neighbours stays alive.
- A live cell with more than 3 live neighbours dies (too crowded).
- A dead cell with exactly 3 live neighbours becomes alive.
All cells change at the same time: every decision uses only the current generation. Update the grid in place to the next generation and return nothing. The tests inspect the grid after your function returns.
Constraints
1 <= rows, columns <= 25- Every cell is
0or1.
Examples
Example 1
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Each cell counts its eight neighbours in the old board. A live cell survives with 2 or 3 live neighbours; a dead cell with exactly 3 comes alive.
Example 2
Input: board = [[1,1],[1,0]] Output: [[1,1],[1,1]]
Three live cells in a corner form an L. The dead cell has three live neighbours and is born, so the block becomes stable.
Example 3
Input: board = [[0,1,0],[0,1,0],[0,1,0]] Output: [[0,0,0],[1,1,1],[0,0,0]]
A vertical bar of three flips into a horizontal bar. This pattern is called a blinker.
JavaScript you'll use: Arrays: create, read, update
/**
* @param {number[][]} board
* @return {void}
*/
function gameOfLife(board) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.