How to resolve the algorithm One-dimensional cellular automata step by step in the ALGOL 68 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values. Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm One-dimensional cellular automata step by step in the ALGOL 68 programming language
Source code in the algol programming language
INT stop generation = 9;
INT universe width = 20;
FORMAT alive or dead = $b("#","_")$;
BITS universe := 2r01110110101010100100;
# universe := BIN ( ENTIER ( random * max int ) ); #
INT upb universe = bits width;
INT lwb universe = bits width - universe width + 1;
PROC couple = (BITS parent, INT lwb, upb)BOOL: (
SHORT INT sum := 0;
FOR bit FROM lwb TO upb DO
sum +:= ABS (bit ELEM parent)
OD;
sum = 2
);
FOR generation FROM 0 WHILE
printf(($"Generation "d": "$, generation,
$f(alive or dead)$, []BOOL(universe)[lwb universe:upb universe],
$l$));
# WHILE # generation < stop generation DO
BITS next universe := 2r0;
# process the first event horizon manually #
IF couple(universe,lwb universe,lwb universe + 1) THEN
next universe := 2r10
FI;
# process the middle kingdom in a loop #
FOR bit FROM lwb universe + 1 TO upb universe - 1 DO
IF couple(universe,bit-1,bit+1) THEN
next universe := next universe OR 2r1
FI;
next universe := next universe SHL 1
OD;
# process the last event horizon manually #
IF couple(universe, upb universe - 1, upb universe) THEN
next universe := next universe OR 2r1
FI;
universe := next universe
OD
INT stop generation = 9;
INT upb universe = 20;
FORMAT alive or dead = $b("#","_")$;
BITS bits universe := 2r01110110101010100100;
# bits universe := BIN ( ENTIER ( random * max int ) ); #
[upb universe] BOOL universe := []BOOL(bits universe)[bits width - upb universe + 1:];
PROC couple = (REF[]BOOL parent)BOOL: (
SHORT INT sum := 0;
FOR bit FROM LWB parent TO UPB parent DO
sum +:= ABS (parent[bit])
OD;
sum = 2
);
FOR generation FROM 0 WHILE
printf(($"Generation "d": "$, generation,
$f(alive or dead)$, universe,
$l$));
# WHILE # generation < stop generation DO
[UPB universe]BOOL next universe;
# process the first event horizon manually #
next universe[1] := couple(universe[:2]);
# process the middle kingdom in a loop #
FOR bit FROM LWB universe + 1 TO UPB universe - 1 DO
next universe[bit] := couple(universe[bit-1:bit+1])
OD;
# process the last event horizon manually #
next universe[UPB universe] := couple(universe[UPB universe - 1: ]);
universe := next universe
OD
You may also check:How to resolve the algorithm Array concatenation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the PepsiScript programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Phix programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the C# programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Erlang programming language