One-dimensional cellular automata: Difference between revisions
One-dimensional cellular automata (view source)
Revision as of 11:36, 27 February 2014
, 10 years ago→{{header|Perl 6}}: generalization to any cellular automata of this kind
(→{{header|Perl 6}}: slightly shorter + joining output) |
(→{{header|Perl 6}}: generalization to any cellular automata of this kind) |
||
Line 2,360:
=={{header|Perl 6}}==
We'll make a general algorithm capable of computing any cellular automata as defined by Stephen Wolfram's famous book [[wp:A new kind of Science|A new kind of Science]]. As an example, we'll show the automata defined by rule n°90, which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]]-like figure.
<lang perl6>class Automata {
constant patterns = reverse ^8;
has Int $.code;
has @.cells;
method gist { <| |>.join: @!cells.map({$_ ?? '#' !! ' '}).join }
method mapping { hash patterns Z=> $.code.fmt("%08b").comb }
method succ {
self.new: :$.code, :cells(
self.mapping{
4 «*« @.cells.rotate(-1)
Z+ 2 «*« @.cells
Z+ @.cells.rotate(1)
}
)
}
}
my $size = 50;
my $a = Automata.new: :code(90), :cells( 0 xx $size div 2, 1, 0 xx $size div 2 );
say $a++ for ^20;</lang>
<pre>| # |
| # # |
| # # |
| # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # # # |
| # # # # # # # # |
| # # # # # # # # |
| # # # # # # # # # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |</pre>
=={{header|PicoLisp}}==
|