Elementary cellular automaton/Infinite length: Difference between revisions

+perl
(must include the infinite part)
(+perl)
Line 16:
 
: But you can't stick to a simple version, and infinite padding beyond the edges must be included somehow. Suppose the visible part of the cells ends with "...110" and is followed by repeating 0s to infinity, and rule is just 1 (000->1). Now after one iteration those three cells become "000", but followed by repeating 1s instead. You can add another constraint that padding/edge cells don't change, but that would make the "infinite" part kind of pointless because changes won't propagate beyond edge cells. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 17:54, 21 March 2014 (UTC)
 
=={{header|Perl}}==
The edges of a pattern is implicitly repeating. The code will try to lineup output by padding up to 40 spaces to the left, but since the cells keep expanding, that has to end somewhere.
<lang perl>sub evolve {
my ($rule, $_) = @_;
my $offset = 0;
 
while (1) {
my ($l, $r, $st);
s/^((.)\g2*)/$2$2/ and $l = $2, $offset -= length($2);
s/(.)\g1*$/$1$1/ and $r = $1;
 
$st = $_;
 
tr/01/.#/;
printf "%5d| %s%s\n", $offset, ' ' x (40 + $offset), $_;
 
$_ = join '', map(1 & ($rule>>oct "0b$_"),
$l x 3,
map(substr($st, $_, 3), 0 .. length($st)-3),
$r x 3);
}
}
 
evolve(90, "010");</lang>
{{out}}
<pre>
-1| ..#..
-2| ..#.#..
-3| ..#...#..
-4| ..#.#.#.#..
-5| ..#.......#..
-6| ..#.#.....#.#..
-7| ..#...#...#...#..
-8| ..#.#.#.#.#.#.#.#..
-9| ..#...............#..
-10| ..#.#.............#.#..
-11| ..#...#...........#...#..
-12| ..#.#.#.#.........#.#.#.#..
-13| ..#.......#.......#.......#..
---(infinite more lines snipped)---
</pre>
Anonymous user