#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:
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.
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.
Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.