Log In
NP Code Competition

Calculate the meaning of life

Back to Problem Listing
Conway's "game of life" is a simulation of cell reproduction, more or less. If you are not familiar with it, here's what you need to know:
- There is a grid.
- Each cell in the grid is either alive or dead.
- After each "turn", the following operations are applied:
-- Any cell that is alive and has 1 or 0 adjacent living cells (diagonal neighbors included) will die on the next turn...from loneliness
-- Any cell that is alive and has 4 or more adjacent living cells (again, diagonal neighbors included) will die on the next turn...from overcrowding.
-- Any cell that isn't alive but has 3 living neighbors (again, diagonal neighbors included) will become a live cell on the next turn.
-- All other cells stay the same during the next turn.

Write a function called thats_life that takes in a string and an integer. The string is the visual representation of the game grid. Live cells will be represented with lowercase x's, and dead cells will be represented by spaces. Each row is delimited by a newline character (\n). The integer is the number of turns to iterate through.

Sample

Here are some well known CGoL patterns:

A 2x2 square doesn't change...
>>> thats_life("    \n xx \n xx \n    ", 100)
"    \n xx \n xx \n    "

This one is called a "glider", and repeats itself every 4 iterations, offset by 1
>>> thats_life("        \n   x    \n x x    \n  xx    \n        \n        \n        \n        ", 4)
"        \n    x   \n  x x   \n   xx   \n        \n        \n        \n        "

This one is called a blinker and rotates in place every 2 iterations:
>>> thats_life("     \n     \n xxx \n     \n     ", 11)
"     \n  x  \n  x  \n  x  \n     "

This contest has ended. No more answers will be accepted.