Jump to content

One-dimensional cellular automata: Difference between revisions

→‎{{header|Perl 6}}: Update (GLR) and improve clarity
m (→‎{{header|Sidef}}: modified the code to work with Sidef 2.30)
(→‎{{header|Perl 6}}: Update (GLR) and improve clarity)
Line 3,225:
and it makes the implementation a lot easier.
 
<lang perl6>class AutomataAutomaton {
has ($.rule, @.cells);
has @.cells;
method gist { <| |>.join: @!cells.map({$_ ?? '#' !! ' '}).join }
methodhas @.code {= $.!rule.fmt("'%08b"').flip.comb».reverse }Int;
)
method gist { <"| |>.join:{ @!cells.map({+$_ ?? '#' !! ' '}).join }|" }
method succ {
self.new: :$.!rule, :@!code, :cells(
self. @!code[
[Z+] 4 «*« @.!cells.rotate(-1),
»+« 2 «*« @.!cells,
»+« @.!cells.rotate(1)
]
)
)
}
}</lang>
Line 3,242 ⟶ 3,245:
The rule proposed for this task is rule 0b01101000 = 104
 
<lang perl6>my $size@padding = 100 xx 5;
my AutomataAutomaton $a .= new:
:rule( => 104),
:cells( 0=> xxflat $size div 2@padding, '111011010101'.comb, 0 xx $size div 2 );@padding
;
say $a++ for ^10;</lang>
 
{{out}}
<pre>
<pre>| ### ## # # # |
| # ##### # # |
| ## ## # |
Line 3,257 ⟶ 3,263:
| ## |
| ## |
| ## |</pre>
</pre>
 
Rule 104 is not particularly interesting so here is [[wp:Rule 90|Rule 90]],
which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]].
 
<lang perl6>my $size@padding = 500 xx 25;
my AutomataAutomaton $a .= new: :rule(90), :cells(flat 0 xx $size div 2@padding, 1, 0 xx $size div 2 @padding);
 
say $a++ for ^20;</lang>
 
{{out}}
<pre>
<pre>| # |
| # # |
| # # |
Line 3,287 ⟶ 3,295:
| # # # # |
| # # # # |
| # # # # # # # # |</pre>
</pre>
 
=={{header|Phix}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.