The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton.

Conway's game of life is described   here:

A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells.

We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table:

   C   N                 new C
   1   0,1             ->  0  # Lonely
   1   4,5,6,7,8       ->  0  # Overcrowded
   1   2,3             ->  1  # Lives
   0   3               ->  1  # It takes three to give birth!
   0   0,1,2,4,5,6,7,8 ->  0  # Barren

Assume cells beyond the boundary are always dead.

The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.



Task

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.



References

   Its creator John Conway, explains   the game of life.   Video from numberphile on youtube.
   John Conway   Inventing Game of Life   - Numberphile video.


Related task

   Langton's ant   - another well known cellular automaton.


-----------------------------------------------------------
-- Print 100 simulated states of conway's game of life
-- for a glider starting pattern on a wrapping grid
-- Print generation number along with cells
 
output =
  initCells
  |> iterate(updateCells)
  |> take(130)
  |> enumerate
  |> map(showPair)
  |> printFrames
 
initCells =
 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
 
width  = length(initCells[0])
height = length(initCells)
 
positions = 
  for y in range(0, height - 1)
  for x in range(0, width - 1)
  yield (x, y)
 
-----------------------------------------------------------
-- Update each cell in the grid according to its position,
-- and convert the resulting list back to a 2D array
 
updateCells(cells) =
  positions
  |> map(tick(cells))
  |> toNDArray((height, width))
 
-----------------------------------------------------------
-- Get the new value for a cell at a give position
-- based on the current cell values in the grid
 
tick(cells, pos) = toInt(survive or birth) where {
  survive = cells[y][x] == 1 and count in {2, 3}
  birth   = cells[y][x] == 0 and count == 3
  count   = getCount(x, y, cells)
  (x, y)  = pos
}
 
-----------------------------------------------------------
-- Get the number of live neighbors of a given position
 
getCount(x, y, cells) = sum(
  for dx in [-1, 0, 1] 
  for dy in [-1, 0, 1] 
  when (dx != 0 or dy != 0)
  yield getNeighbor(x + dx, y + dy, cells) 
)
 
getNeighbor(x, y, cells) = cells[y % height][x % width]   
 
-----------------------------------------------------------
-- Print the board and generation number given pairs
-- of (gen, cells) from the enumerate function
 
showPair(pair) =
  format("{}\ngeneration: {}", [showCells(cells), gen])
  where (gen, cells) = pair
 
showCells(cells) =
  toList(cells)
  |> map(showRow)
  |> join("\n")
 
showRow(row) =
  format("|{}|", [map(showCell, toList(row)) |> join("")])
 
showCell(cell) =
  if cell == 1 then "*" else " "