How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the jq programming language

Table of Contents

Problem Statement

The purpose of this task is to create a version of an Elementary cellular automaton whose number of cells is only limited by the memory size of the computer. To be precise, consider the state of the automaton to be made of an infinite number of cells, but with a bounded support. In other words, to describe the state of the automaton, you need a finite number of adjacent cells, along with their individual state, and you then consider that the individual state of each of all other cells is the negation of the closest individual cell among the previously defined finite number of cells. Examples: More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the jq programming language

Source code in the jq programming language

def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;

def lpad($len): lpad($len; " ");

# Like *ix `tr` but it is as though $to is padded with blanks
def tr($from;$to):
  explode as $s
  | ($from | explode) as $f
  | ($to   | explode) as $t
  | reduce range(0;length) as $i ([];
      ($f | index($s[$i])) as $ix
      | if $ix then . + [$t[$ix] // " "]
        else . + [$s[$i]]
	end )
  | implode;	

# Input: a non-negative integer
# Output: the corresponding stream of bits (0s and 1s),
# least significant digit first, with a final 0
def stream:
  recurse(if . > 0 then ./2|floor else empty end) | . % 2 ;

# input: an array, e.g. as produced by [7|stream]
# output: the corresponding binary string
def to_b: reverse | map(tostring) | join("") | sub("^0";"");

# Output: an unbounded stream of the form [count, row]
# giving the rows produced by the eca defined by
# $cells (a string) and $rule (an integer)
def eca_infinite($cells; $rule):

  def notcell: tr("01";"10") ;

  def rule2hash($rule):
    [$rule | stream] as $r
    | reduce range(0;8) as $i ({};
        . + { ($i|[stream]|to_b|lpad(3;"0")): ($r[$i] // 0)});

  rule2hash($rule) as $neighbours2next
  | [0, $cells],
    foreach range(1; infinite) as $i ({c: $cells};
      .c = (.c[0:1]|notcell)*2 + .c + (.c[-1:]|notcell)*2        # Extend and pad the ends
      | .c = ([range(1; .c|length - 1) as $i | $neighbours2next[.c[$i-1:$i+2] ]] | join(""));
      [$i, .c] ) ;

# $lines specifies the number of lines to display for each eca
def main($lines):
  (90, 30) as $rule
  | "\nRule: \($rule)",
    (limit($lines; eca_infinite("1"; $rule)
     | .[0] as $line
     | ($line|lpad(3)) + " " * ($lines - $line) + (.[1] | tr("01"; ".#") )));

main(25)

  

You may also check:How to resolve the algorithm Pernicious numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm File size distribution step by step in the Julia programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Java programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Arturo programming language
You may also check:How to resolve the algorithm Main step of GOST 28147-89 step by step in the Phix programming language