Elementary cellular automaton/Infinite length: Difference between revisions

→‎{{header|Perl 6}}: Add a Perl 6 example
m (→‎{{header|Kotlin}}: Minor formatting changes)
(→‎{{header|Perl 6}}: Add a Perl 6 example)
Line 445:
-13| ..#.......#.......#.......#..
---(infinite more lines snipped)---
</pre>
 
=={{header|Perl 6}}==
This version, while it is ''capable'' of working with infinite length cellular automata, makes the assumption that any cells which have not been explicitly examined are in a 'null' state, neither '0' or '1'. Further it makes the assumption that a null cell, on being examined, initially contains nothing (░). Otherwise it would take infinite time to calculate every row and would be exceptionally boring to watch.
 
Based heavily on the code from the [[One-dimensional_cellular_automata#Perl_6|One-dimensional cellular automata]] task. Example uses rule 90 (Sierpinski triangle).
 
<lang perl6>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)
],
' '
)
}
}
 
my Automaton $a .= new: :rule(90), :cells(flat '010'.comb);
 
# display the first 20 rows
say $a++ for ^20;
 
# then calculate the other infinite number of rows, (may take a while)
$a++ for ^Inf;</lang>
{{out}}
<pre>░▲░
░▲░▲░
░▲░░░▲░
░▲░▲░▲░▲░
░▲░░░░░░░▲░
░▲░▲░░░░░▲░▲░
░▲░░░▲░░░▲░░░▲░
░▲░▲░▲░▲░▲░▲░▲░▲░
░▲░░░░░░░░░░░░░░░▲░
░▲░▲░░░░░░░░░░░░░▲░▲░
░▲░░░▲░░░░░░░░░░░▲░░░▲░
░▲░▲░▲░▲░░░░░░░░░▲░▲░▲░▲░
░▲░░░░░░░▲░░░░░░░▲░░░░░░░▲░
░▲░▲░░░░░▲░▲░░░░░▲░▲░░░░░▲░▲░
░▲░░░▲░░░▲░░░▲░░░▲░░░▲░░░▲░░░▲░
░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░▲░
░▲░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▲░
░▲░▲░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▲░▲░
░▲░░░▲░░░░░░░░░░░░░░░░░░░░░░░░░░░▲░░░▲░
░▲░▲░▲░▲░░░░░░░░░░░░░░░░░░░░░░░░░▲░▲░▲░▲░
^C
</pre>
 
10,333

edits