Editor

Game of Life: One Step

#99 · Simulation · Intermediate · 80 XP · about 15 min

In Conway's Game of Life, every cell of a grid is alive (#) or dead (.). Each cell has up to 8 neighbours: the cells touching it along a side or at a corner. Cells beyond the edge of the grid count as dead.

All cells change at the same moment, based on the grid as it was before the step:

  • A live cell with 2 or 3 live neighbours stays alive; any other live cell dies.
  • A dead cell with exactly 3 live neighbours comes alive; any other dead cell stays dead.

Write life_step(grid) that takes the grid as a list of strings of equal length and returns the next generation in the same form. Do not change the list you were given.

Examples

life_step(['.....',
           '..#..',
           '..#..',
           '..#..',
           '.....'])
# returns ['.....', '.....', '.###.', '.....', '.....']

life_step(['##',
           '##'])
# returns ['##', '##']

The first pattern, called a blinker, flips between a vertical and a horizontal line. The 2 × 2 block never changes.

Constraints

  • 1 <= number of rows, number of columns <= 60

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.