Elementary cellular automaton/Infinite length

From Rosetta Code
Elementary cellular automaton/Infinite length is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The purpose of this task is to create a version of an Elementary cellular automaton whose number of cells is only limited by the memory size of the computer.

To be precise, consider the state of the automaton to be made of an infinite number of cells, but with a bounded support. In other words, to describe the state of the automaton, you need a finite number of adjacent cells, along with their individual state, and you then consider that the individual state of each of all other cells is the negation of the individual cell which the closest.

Examples:

1        ->   ..., 0, 0,      1,      0, 0, ...
0, 1     ->   ..., 1, 1,   0, 1,      0, 0, ...
1, 0, 1  ->   ..., 0, 0,   1, 0, 1,   0, 0, ...

More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.

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>

Output:
   -1|                                        ..#..
   -2|                                       ..#.#..
   -3|                                      ..#...#..
   -4|                                     ..#.#.#.#..
   -5|                                    ..#.......#..
   -6|                                   ..#.#.....#.#..
   -7|                                  ..#...#...#...#..
   -8|                                 ..#.#.#.#.#.#.#.#..
   -9|                                ..#...............#..
  -10|                               ..#.#.............#.#..
  -11|                              ..#...#...........#...#..
  -12|                             ..#.#.#.#.........#.#.#.#..
  -13|                            ..#.......#.......#.......#..
---(infinite more lines snipped)---