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.


#!/usr/bin/env emacs -script
;; -*- lexical-binding: t -*-
;; run: ./conways-life conways-life.config
(require 'cl-lib)
 
(defconst blinker '("***"))
(defconst toad '(".***" "***."))
(defconst pentomino-p '(".**" ".**" ".*."))
(defconst pi-heptomino '("***" "*.*" "*.*"))
(defconst glider '(".*." "..*" "***"))
(defconst pre-pulsar '("***...***" "*.*...*.*" "***...***"))
(defconst ship '("**." "*.*" ".**"))
(defconst pentadecathalon '("**********"))
(defconst clock '("..*." "*.*." ".*.*" ".*.."))
 
(defmacro swap (a b)
  `(setq ,b (prog1 ,a (setq ,a ,b))))
 
(cl-defstruct world rows cols data)
 
(defun new-world (rows cols)
  (make-world :rows rows :cols cols :data (make-vector (* rows cols) nil)))
 
(defmacro world-pt (w r c)
  `(+ (* (mod ,r (world-rows ,w)) (world-cols ,w))
      (mod ,c (world-cols ,w))))
 
(defmacro world-ref (w r c)
  `(aref (world-data ,w) (world-pt ,w ,r ,c)))
 
(defun print-world (world)
  (dotimes (r (world-rows world))
    (dotimes (c (world-cols world))
      (princ (format "%c" (if (world-ref world r c) ?* ?.))))
    (terpri)))
 
(defun insert-pattern (world row col shape)
  (let ((r row)
        (c col))
    (unless (listp shape)
      (setq shape (symbol-value shape)))
    (dolist (row-data shape)
      (dolist (col-data (mapcar 'identity row-data))
        (setf (world-ref world r c) (not (or (eq col-data ?.))))
        (setq c (1+ c)))
      (setq r (1+ r))
      (setq c col))))
 
(defun neighbors (world row col)
  (let ((n 0))
    (dolist (offset '((1 . 1) (1 . 0) (1 . -1) (0 . 1) (0 . -1) (-1 . 1) (-1 . 0) (-1 . -1)))
      (when (world-ref world (+ row (car offset)) (+ col (cdr offset)))
        (setq n (1+ n))))
    n))
 
(defun advance-generation (old new)
  (dotimes (r (world-rows old))
    (dotimes (c (world-cols old))
      (let ((n (neighbors old r c)))
        (setf (world-ref new r c)
              (if (world-ref old r c)
                  (or (= n 2) (= n 3))
                (= n 3)))))))
 
(defun read-config (file-name)
  (with-temp-buffer
    (insert-file-contents-literally file-name)
    (read (current-buffer))))
 
(defun get-config (key config)
  (let ((val (assoc key config)))
    (if (null val)
        (error (format "missing value for %s" key))
      (cdr val))))
 
(defun insert-patterns (world patterns)
  (dolist (p patterns)
    (apply 'insert-pattern (cons world p))))
 
(defun simulate-life (file-name)
  (let* ((config (read-config file-name))
         (rows (get-config 'rows config))
         (cols (get-config 'cols config))
         (generations (get-config 'generations config))
         (a (new-world rows cols))
         (b (new-world rows cols)))
    (insert-patterns a (get-config 'patterns config))
    (dotimes (g generations)
      (princ (format "generation %d\n" g))
      (print-world a)
      (advance-generation a b)
      (swap a b))))
 
(simulate-life (elt command-line-args-left 0))