Elementary cellular automaton/Infinite length: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
→‎{{header|Perl 6}}: Add a Perl 6 example
SqrtNegInf (talk | contribs)
m →‎{{header|Perl}}: revised use of $_
Line 408: Line 408:
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.
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 {
<lang perl>sub evolve {
my ($rule, $_) = @_;
my ($rule, $pattern) = @_;
my $offset = 0;
my $offset = 0;


while (1) {
while (1) {
my ($l, $r, $st);
my ($l, $r, $st);
s/^((.)\g2*)/$2$2/ and $l = $2, $offset -= length($2);
$pattern =~ s/^((.)\g2*)/$2$2/ and $l = $2, $offset -= length($2);
s/(.)\g1*$/$1$1/ and $r = $1;
$pattern =~ s/(.)\g1*$/$1$1/ and $r = $1;


$st = $_;
$st = $pattern;


tr/01/.#/;
$pattern =~ tr/01/.#/;
printf "%5d| %s%s\n", $offset, ' ' x (40 + $offset), $_;
printf "%5d| %s%s\n", $offset, ' ' x (40 + $offset), $pattern;


$_ = join '', map(1 & ($rule>>oct "0b$_"),
$pattern = join '', map(1 & ($rule>>oct "0b$_"),
$l x 3,
$l x 3,
map(substr($st, $_, 3), 0 .. length($st)-3),
map(substr($st, $_, 3), 0 .. length($st)-3),