Colony Step
Every cell of grid is 1 (alive) or 0 (empty). All cells change at the same time by these rules, which look only at the neighbours that share a side (up, down, left, right): an alive cell with no alive side neighbour becomes 0; an empty cell with at least two alive side neighbours becomes 1; every other cell keeps its value. Return the grid after one step, as a new grid, without changing grid.
Example 1
The centre is empty with four alive neighbours, so it becomes 1. Each corner is empty with two alive neighbours, so it becomes 1. The four alive cells have no alive side neighbour and die.
Example 2
The two alive cells keep each other alive; the empty cells below each have only one alive neighbour.
Constraints
1 <= rows, cols <= 10grid[r][c]is 0 or 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.