How to resolve the algorithm One-dimensional cellular automata step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the Raku 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 Raku programming language
Source code in the raku programming language
class Automaton {
has $.rule;
has @.cells;
has @.code = $!rule.fmt('%08b').flip.comb».Int;
method gist { "|{ @!cells.map({+$_ ?? '#' !! ' '}).join }|" }
method succ {
self.new: :$!rule, :@!code, :cells(
@!code[
4 «*« @!cells.rotate(-1)
»+« 2 «*« @!cells
»+« @!cells.rotate(1)
]
)
}
}
# The rule proposed for this task is rule 0b01101000 = 104
my @padding = 0 xx 5;
my Automaton $a .= new:
rule => 104,
cells => flat @padding, '111011010101'.comb, @padding
;
say $a++ for ^10;
# Rule 104 is not particularly interesting so here is [[wp:Rule 90|Rule 90]],
# which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]].
say '';
@padding = 0 xx 25;
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding);
say $a++ for ^20;
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Pascal programming language
You may also check:How to resolve the algorithm JortSort step by step in the Rust programming language
You may also check:How to resolve the algorithm Create a file step by step in the Scheme programming language
You may also check:How to resolve the algorithm Currying step by step in the Vala programming language
You may also check:How to resolve the algorithm Bifid cipher step by step in the AppleScript programming language