Elementary cellular automaton/Infinite length: Difference between revisions
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>
|
Revision as of 19:08, 21 March 2014
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 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.
- 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. --Ledrug (talk) 17:54, 21 March 2014 (UTC)
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)---