How to resolve the algorithm Conway's Game of Life step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Conway's Game of Life step by step in the jq programming language

Table of Contents

Problem Statement

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: 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.

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Conway's Game of Life step by step in the jq programming language

Source code in the jq programming language

# Notes on the implementation:

# 1. For efficiency, the implementation requires that the world
#    has boundaries, as illustrated in the examples.
# 2. For speed, the simulation uses the exploded string.
# 3. The ASCII values of the "alive" and "empty" symbols are 
#    hardcoded: "." => 46; " " => 32
# 4. To adjust the refresh rate, adjust the input to "spin".

def lines: split("\n")|length;
 
def cols: split("\n")[0]|length + 1;  # allow for the newline

# Is there a "." (46) at [x,y] relative to position i,
# assuming the width is w?
# Input is an array; result is 0 or 1 so we can easily count the total.
def isAlive(x; y; i; w): if .[i+ w*y + x] == 46 then 1 else 0 end;

def neighborhood(i;w):
  isAlive(-1; -1; i; w) + isAlive(0; -1; i; w) + isAlive(1; -1; i; w) +
  isAlive(-1;  0; i; w)                        + isAlive(1;  0; i; w) +
  isAlive(-1;  1; i; w) + isAlive(0;  1; i; w) + isAlive(1;  1; i; w) ;

# The basic rules:
def evolve(cell; sum) : 
  if   cell == 46 then if sum == 2 or sum == 3 then 46 else 32 end
  elif cell == 32 then if sum == 3 then 46 else 32 end
  else cell
  end ;

# [world, lines, cols] | next(w) => [world, lines, cols]
def next:
  .[0] as $world | .[1] as $lines | .[2] as $w
  | reduce range(0; $world|length) as $i
    ($world;
      .[$i] as $c
      | if $c == 32 or $c == 46 then
           # updates are "simultaneous" i.e. relative to $world, not "."
           ($world | neighborhood($i; $w)) as $sum
           | evolve($c; $sum) as $next
           | if $c == $next then . else .[$i] = $next end
        else .
        end )
  | [., $lines, $w] ;

# "clear screen":
def cls: "\u001b[2J";

# Input: an integer; 1000 ~ 1 sec
def spin:
  reduce range(1; 500 * .) as $i
    (0; . + ($i|cos)*($i|cos) + ($i|sin)*($i|sin) )
  |  "" ;
 
# Animate n steps;
# if "sleep" is non-negative then cls and 
# sleep about "sleep" ms between frames.
def animate(n; sleep):
  if n == 0 then empty
  else (if sleep >= 0 then cls else "" end),
       (.[0]|implode), n, "\n",
       (sleep|spin),
       ( next|animate(n-1; sleep) )
  end ;
 
# Input: a string representing the initial state
def animation(n; sleep):
  [ explode, lines, cols] | animate(n; sleep) ;
 
# Input: a string representing the initial state
def frames(n):  animation(n; -1);

def world3:
"+---+\n" +
"|   |\n" +
"|...|\n" +
"|   |\n" +
"+---+\n" ;

def world11:
"+-----------+\n" +
"|           |\n" +
"|   ..      |\n" +
"|    ...    |\n" +
"|      ..   |\n" +
"|           |\n" +
"+-----------+\n" ;

world3 | frames(3)

# Animation of 100 frames with approximately 1 second between each update:
world11 | animation(100; 1000)

  

You may also check:How to resolve the algorithm FizzBuzz step by step in the Boo programming language
You may also check:How to resolve the algorithm String prepend step by step in the Maple programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the Crystal programming language
You may also check:How to resolve the algorithm String concatenation step by step in the OCaml programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Groovy programming language