FunctionRacerProblemsRace

Life-Like Generations

MediumGridsPython, JavaScript, TypeScript, Java, C++

grid is a board of cells given as a list of rows: 1 is a live cell and 0 a dead one. The neighbours of a cell are the up to eight cells that share a side or a corner with it; places beyond the edge of the board are always dead. In one generation every cell changes at the same time: a dead cell comes alive when its number of live neighbours appears in birth, a live cell stays alive when its number of live neighbours appears in survive, and every other cell is dead in the next generation. Return the board after steps generations as a new board, without changing grid. For example, Conway's Game of Life uses birth = [3] and survive = [2, 3].

Example 1

grid [[0,0,0],[1,1,1],[0,0,0]]
birth [3]
survive [2,3]
steps 1
returns [[0,1,0],[0,1,0],[0,1,0]]

With the Game of Life rules the middle cell keeps its two live neighbours, the two ends have only one and die, and the cells above and below the middle have three live neighbours and come alive.

Example 2

grid [[0,0,0,0],[0,1,1,0],[0,0,0,0]]
birth [2]
survive []
steps 2
returns [[0,0,0,0],[1,0,0,1],[0,0,0,0]]

With an empty survive list every live cell dies each generation. First the four cells directly above and below the pair come alive, since each touches both live cells. In the second generation only the two cells at the ends of the middle row touch exactly two live cells.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems