Welcome to Project Lovelace! We're still super new so there are still tons of bugs to find and improvements to make. If you have any suggestions, complaints, or any comments at all please let us know on Discourse!

A cellular automaton is a model used to simulate a wide variety of systems in science. We'll look at one that's supposed to simulate life itself, Conway's Game of Life. You have a two-dimensional rectangular grid of square cells, each of which can be either alive or dead. Each cell interacts with its eight nearest neighbors and evolves according to the following rules:

A live cell with less than two live neighbours dies (due to underpopulation).

A live cell with two or three live neighbours continues to live.

A live cell with more than three live neighbours dies (due to overpopulation).

A dead cell with exactly three live neighbours becomes a live cell (due to reproduction).

To deal with the finite size of the grid we'll impose periodic (or toroidal) boundary conditions, that is, to treat the left and right edges of the grid to be stitched together so that moving across the right boundary returns you to the left boundary (and we will do the same with the top and bottom edges).

Given an initial configuration for the grid, run the Game of Life simulation for $N$ time steps (also an input) and return the state of the grid.

Input:
A 2D integer array representing the grid where the dead cells are represnted by 0's and the alive cells are 1's, and a number of time steps $N$ to run the Game of Life for.

Output:
A 2D array representing the state of the grid after $N$ time steps.

Write a function that accepts the input as function parameters and returns the correct output. Make sure to read the description above to produce the correct output in the correct format and use the correct function signature so we can run your code. A good first step is to try reproducing the example(s). Your code must not take longer than the maximum runtime to run and must not use more memory than the allowed limit.

You must be logged in to view your submissions.

Notes

There is a Numberphile video where John Conway himself talks about how he invented the Game of Life!

LifeWiki has tons more information on the Game of Life, cool patterns, and the latest news (people are still discovering completely new patterns in 2018).

Let us know what you think about this problem! Was it too hard? Difficult to understand? Also feel free to discuss the problem, ask questions, and post cool stuff on Discourse. You should be able view the discussion thread below. Feel free to post your solutions but please do your best to organize and document your code well so others can learn from it.