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.


defmodule Conway do
  def game_of_life(name, size, generations, initial_life\\nil) do
    board = seed(size, initial_life)
    print_board(board, name, size, 0)
    reason = generate(name, size, generations, board, 1)
    case reason do
      :all_dead -> "no more life."
      :static   -> "no movement"
      _         -> "specified lifetime ended"
    end
    |> IO.puts
    IO.puts ""
  end
 
  defp new_board(n) do
    for x <- 1..n, y <- 1..n, into: %{}, do: {{x,y}, 0}
  end
 
  defp seed(n, points) do
    if points do
      points
    else # randomly seed board
      (for x <- 1..n, y <- 1..n, do: {x,y}) |> Enum.take_random(10)
    end
    |> Enum.reduce(new_board(n), fn pos,acc -> %{acc | pos => 1} end)
  end
 
  defp generate(_, _, generations, _, gen) when generations < gen, do: :ok
  defp generate(name, size, generations, board, gen) do
    new = evolve(board, size)
    print_board(new, name, size, gen)
    cond do
      barren?(new) -> :all_dead
      board == new -> :static
      true         -> generate(name, size, generations, new, gen+1)
    end
  end
 
  defp evolve(board, n) do
    for x <- 1..n, y <- 1..n, into: %{}, do: {{x,y}, fate(board, x, y, n)}
  end
 
  defp fate(board, x, y, n) do
    irange = max(1, x-1) .. min(x+1, n)
    jrange = max(1, y-1) .. min(y+1, n)
    sum = ((for i <- irange, j <- jrange, do: board[{i,j}]) |> Enum.sum) - board[{x,y}]
    cond do
      sum == 3                       -> 1
      sum == 2 and board[{x,y}] == 1 -> 1
      true                           -> 0
    end
  end
 
  defp barren?(board) do
    Enum.all?(board, fn {_,v} -> v == 0 end)
  end
 
  defp print_board(board, name, n, generation) do
    IO.puts "#{name}: generation #{generation}"
    Enum.each(1..n, fn y ->
      Enum.map(1..n, fn x -> if board[{x,y}]==1, do: "#", else: "." end)
      |> IO.puts
    end)
  end
end
 
Conway.game_of_life("blinker", 3, 2, [{2,1},{2,2},{2,3}])
Conway.game_of_life("glider", 4, 4, [{2,1},{3,2},{1,3},{2,3},{3,3}])
Conway.game_of_life("random", 5, 10)