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 closest individual cell among the previously defined finite number of cells.

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.

C++[edit]

 
#include <iostream>
#include <iomanip>
#include <string>
 
class oo {
public:
void evolve( int l, int rule ) {
std::string cells = "O";
std::cout << " Rule #" << rule << ":\n";
for( int x = 0; x < l; x++ ) {
addNoCells( cells );
std::cout << std::setw( 40 + ( static_cast<int>( cells.length() ) >> 1 ) ) << cells << "\n";
step( cells, rule );
}
}
private:
void step( std::string& cells, int rule ) {
int bin;
std::string newCells;
for( size_t i = 0; i < cells.length() - 2; i++ ) {
bin = 0;
for( size_t n = i, b = 2; n < i + 3; n++, b >>= 1 ) {
bin += ( ( cells[n] == 'O' ? 1 : 0 ) << b );
}
newCells.append( 1, rule & ( 1 << bin ) ? 'O' : '.' );
}
cells = newCells;
}
void addNoCells( std::string& s ) {
char l = s.at( 0 ) == 'O' ? '.' : 'O',
r = s.at( s.length() - 1 ) == 'O' ? '.' : 'O';
s = l + s + r;
s = l + s + r;
}
};
int main( int argc, char* argv[] ) {
oo o;
o.evolve( 35, 90 );
std::cout << "\n";
return 0;
}
 
Output:
Rule #90:                                                    Rule #30:                   
                        ..O..                                                        ..O..
                       ..O.O..                                                      ..OOO..
                      ..O...O..                                                    ..OO..O..
                     ..O.O.O.O..                                                  ..OO.OOOO..
                    ..O.......O..                                                ..OO..O...O..
                   ..O.O.....O.O..                                              ..OO.OOOO.OOO..
                  ..O...O...O...O..                                            ..OO..O....O..O..
                 ..O.O.O.O.O.O.O.O..                                          ..OO.OOOO..OOOOOO..
                ..O...............O..                                        ..OO..O...OOO.....O..
               ..O.O.............O.O..                                      ..OO.OOOO.OO..O...OOO..
              ..O...O...........O...O..                                    ..OO..O....O.OOOO.OO..O..
             ..O.O.O.O.........O.O.O.O..                                  ..OO.OOOO..OO.O....O.OOOO..
            ..O.......O.......O.......O..                                ..OO..O...OOO..OO..OO.O...O..
           ..O.O.....O.O.....O.O.....O.O..                              ..OO.OOOO.OO..OOO.OOO..OO.OOO..
          ..O...O...O...O...O...O...O...O..                            ..OO..O....O.OOO...O..OOO..O..O..
         ..O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O..                          ..OO.OOOO..OO.O..O.OOOOO..OOOOOOO..
        ..O...............................O..                        ..OO..O...OOO..OOOO.O....OOO......O..
       ..O.O.............................O.O..                      ..OO.OOOO.OO..OOO....OO..OO..O....OOO..
      ..O...O...........................O...O..                    ..OO..O....O.OOO..O..OO.OOO.OOOO..OO..O..
     ..O.O.O.O.........................O.O.O.O..                  ..OO.OOOO..OO.O..OOOOOO..O...O...OOO.OOOO..
    ..O.......O.......................O.......O..                ..OO..O...OOO..OOOO.....OOOO.OOO.OO...O...O..
   ..O.O.....O.O.....................O.O.....O.O..              ..OO.OOOO.OO..OOO...O...OO....O...O.O.OOO.OOO..
  ..O...O...O...O...................O...O...O...O..            ..OO..O....O.OOO..O.OOO.OO.O..OOO.OO.O.O...O..O..
 ..O.O.O.O.O.O.O.O.................O.O.O.O.O.O.O.O..          ..OO.OOOO..OO.O..OOO.O...O..OOOO...O..O.OO.OOOOOO..
..O...............O...............O...............O..        ..OO..O...OOO..OOOO...OO.OOOOO...O.OOOOO.O..O.....O..

D[edit]

Translation of: Python
import std.stdio, std.array, std.range, std.typecons, std.string, std.conv,
std.algorithm;
alias R = replicate;
 
void main() {
enum nLines = 25;
enum notCell = (in char c) pure => (c == '1') ? "0" : "1";
 
foreach (immutable rule; [90, 30]) {
writeln("\nRule: ", rule);
immutable ruleBits = "%08b".format(rule).retro.text;
const neighs2next = 8.iota
.map!(n => tuple("%03b".format(n), [ruleBits[n]]))
.assocArray;
 
string C = "1";
foreach (immutable i; 0 .. nLines) {
writefln("%2d: %s%s", i, " ".R(nLines - i), C.tr("01", ".#"));
C = notCell(C[0]).R(2) ~ C ~ notCell(C[$ - 1]).R(2);
C = iota(1, C.length - 1)
.map!(i => neighs2next[C[i - 1 .. i + 2]])
.join;
}
}
}

The output is the same as the Python entry.

Elixir[edit]

Works with: Elixir version 1.3
Translation of: Ruby
 
defmodule Elementary_cellular_automaton do
def infinite(cell, rule, times) do
each(cell, rule_pattern(rule), times)
end
 
defp each(_, _, 0), do: :ok
defp each(cells, rules, times) do
IO.write String.duplicate(" ", times)
IO.puts String.replace(cells, "0", ".") |> String.replace("1", "#")
c = not_cell(String.first(cells)) <> cells <> not_cell(String.last(cells))
next_cells = Enum.map_join(0..String.length(cells)+1, fn i ->
Map.get(rules, String.slice(c, i, 3))
end)
each(next_cells, rules, times-1)
end
 
defp not_cell("0"), do: "11"
defp not_cell("1"), do: "00"
 
defp rule_pattern(rule) do
list = Integer.to_string(rule, 2) |> String.pad_leading(8, "0")
|> String.codepoints |> Enum.reverse
Enum.map(0..7, fn i -> Integer.to_string(i, 2) |> String.pad_leading(3, "0") end)
|> Enum.zip(list) |> Map.new
end
end
 
Enum.each([18, 30], fn rule ->
IO.puts "\nRule : #{rule}"
Elementary_cellular_automaton.infinite("1", rule, 25)
end)
Output:
Rule : 18
                         #
                        #.#
                       #...#
                      #.#.#.#
                     #.......#
                    #.#.....#.#
                   #...#...#...#
                  #.#.#.#.#.#.#.#
                 #...............#
                #.#.............#.#
               #...#...........#...#
              #.#.#.#.........#.#.#.#
             #.......#.......#.......#
            #.#.....#.#.....#.#.....#.#
           #...#...#...#...#...#...#...#
          #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
         #...............................#
        #.#.............................#.#
       #...#...........................#...#
      #.#.#.#.........................#.#.#.#
     #.......#.......................#.......#
    #.#.....#.#.....................#.#.....#.#
   #...#...#...#...................#...#...#...#
  #.#.#.#.#.#.#.#.................#.#.#.#.#.#.#.#
 #...............#...............#...............#

Rule : 30
                         #
                        ###
                       ##..#
                      ##.####
                     ##..#...#
                    ##.####.###
                   ##..#....#..#
                  ##.####..######
                 ##..#...###.....#
                ##.####.##..#...###
               ##..#....#.####.##..#
              ##.####..##.#....#.####
             ##..#...###..##..##.#...#
            ##.####.##..###.###..##.###
           ##..#....#.###...#..###..#..#
          ##.####..##.#..#.#####..#######
         ##..#...###..####.#....###......#
        ##.####.##..###....##..##..#....###
       ##..#....#.###..#..##.###.####..##..#
      ##.####..##.#..######..#...#...###.####
     ##..#...###..####.....####.###.##...#...#
    ##.####.##..###...#...##....#...#.#.###.###
   ##..#....#.###..#.###.##.#..###.##.#.#...#..#
  ##.####..##.#..###.#...#..####...#..#.##.######
 ##..#...###..####...##.#####...#.#####.#..#.....#

Haskell[edit]

Infinite lists are natural in Haskell, however the task forces us to deal with lists that are infinite in both directions. These structures could be efficiently implemented as a zipper lists. Moreover, zipper lists are instances of magic Comonad class, which gives beautifull implementation of cellular automata.

This solution is kinda involved, but it is guaranteed to be total and correct by type checker.

First we provide the datatype, the viewer and constructor:

{-# LANGUAGE DeriveFunctor #-}
 
import Control.Comonad
import Data.InfList (InfList (..), (+++))
import qualified Data.InfList as Inf
 
data Cells a = Cells (InfList a) a (InfList a) deriving Functor
 
view n (Cells l x r) = reverse (Inf.take n l) ++ [x] ++ (Inf.take n r)
 
fromList [] = fromList [0]
fromList (x:xs) = let zeros = Inf.repeat 0
in Cells zeros x (xs +++ zeros)

In order to run the CA on the domain we make it an instance of Comonad class. Running the CA turns to be just an iterative comonadic extension of the rule:

instance Comonad Cells where
extract (Cells _ x _) = x
duplicate x = Cells (rewind left x) x (rewind right x)
where
rewind dir = Inf.iterate dir . dir
right (Cells l x (r ::: rs)) = Cells (x ::: l) r rs
left (Cells (l ::: ls) x r) = Cells ls l (x ::: r)
 
runCA rule = iterate (=>> step)
where step (Cells (l ::: _) x (r ::: _)) = rule l x r

Following is the rule definition and I/O routine:

rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2
 
displayCA n w rule init = mapM_ putStrLn $ take n result
where result = fmap display . view w <$> runCA rule init
display 0 = ' '
display _ = '*'
Output:
λ> displayCA 30 20 (rule 90) (fromList [1])
                    *                    
                   * *                   
                  *   *                  
                 * * * *                 
                *       *                
               * *     * *               
              *   *   *   *              
             * * * * * * * *             
            *               *            
           * *             * *           
          *   *           *   *          
         * * * *         * * * *         
        *       *       *       *        
       * *     * *     * *     * *       
      *   *   *   *   *   *   *   *      
     * * * * * * * * * * * * * * * *     
    *                               *    
   * *                             * *   
  *   *                           *   *  
 * * * *                         * * * * 
*       *                       *       *
 *     * *                     * *     * 
  *   *   *                   *   *   *  
 * * * * * *                 * * * * * * 
            *               *            
           * *             * *           
          *   *           *   *          
         * * * *         * * * *         
*       *       *       *       *       *
 *     * *     * *     * *     * *     * 

See also Elementary cellular automaton#Haskell

J[edit]

Implementation note: edges are complement of the first and last represented cell, which we define as 1 for the case of an empty numeric list. (So we can represent an infinite space of 0s but not an infinite space of 1s.)

We actually only extend our edges by 9 positions (which is more than sufficient), and then trim everything up to the first change from each edge (so the result from a rule which results in all 1s will be silently converted to an empty all 0s result).

Note however that this means that positions in the result are not anchored to positions in the argument. They might correspond or they might be "off by one" position.


Implementation:

ext9=: (9#1-{.!.1),],9#1-{:!.1
trim=: |.@(}.~ ] i. 1-{.)^:2
next=: trim@(((8$2) #: [) {~ 2 #. 1 - [: |: |.~"1 0&_1 0 1@]) ext9

In other words, a wrapped version of the original implementation.

example use:

   ' *'{~90 next^:(i.9) 1
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *

Looks like a Sierpinski triangle

Perl[edit]

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.

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");
Output:
   -1|                                        ..#..
   -2|                                       ..#.#..
   -3|                                      ..#...#..
   -4|                                     ..#.#.#.#..
   -5|                                    ..#.......#..
   -6|                                   ..#.#.....#.#..
   -7|                                  ..#...#...#...#..
   -8|                                 ..#.#.#.#.#.#.#.#..
   -9|                                ..#...............#..
  -10|                               ..#.#.............#.#..
  -11|                              ..#...#...........#...#..
  -12|                             ..#.#.#.#.........#.#.#.#..
  -13|                            ..#.......#.......#.......#..
---(infinite more lines snipped)---

Python[edit]

Infinite generator but only print 25 lines of each rule.

def _notcell(c):
return '0' if c == '1' else '1'
 
def eca_infinite(cells, rule):
lencells = len(cells)
rulebits = '{0:08b}'.format(rule)
neighbours2next = {'{0:03b}'.format(n):rulebits[::-1][n] for n in range(8)}
c = cells
while True:
yield c
c = _notcell(c[0])*2 + c + _notcell(c[-1])*2 # Extend and pad the ends
 
c = ''.join(neighbours2next[c[i-1:i+2]] for i in range(1,len(c) - 1))
#yield c[1:-1]
 
if __name__ == '__main__':
lines = 25
for rule in (90, 30):
print('\nRule: %i' % rule)
for i, c in zip(range(lines), eca_infinite('1', rule)):
print('%2i: %s%s' % (i, ' '*(lines - i), c.replace('0', '.').replace('1', '#')))
Output:
Rule: 90
 0:                          #
 1:                         #.#
 2:                        #...#
 3:                       #.#.#.#
 4:                      #.......#
 5:                     #.#.....#.#
 6:                    #...#...#...#
 7:                   #.#.#.#.#.#.#.#
 8:                  #...............#
 9:                 #.#.............#.#
10:                #...#...........#...#
11:               #.#.#.#.........#.#.#.#
12:              #.......#.......#.......#
13:             #.#.....#.#.....#.#.....#.#
14:            #...#...#...#...#...#...#...#
15:           #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
16:          #...............................#
17:         #.#.............................#.#
18:        #...#...........................#...#
19:       #.#.#.#.........................#.#.#.#
20:      #.......#.......................#.......#
21:     #.#.....#.#.....................#.#.....#.#
22:    #...#...#...#...................#...#...#...#
23:   #.#.#.#.#.#.#.#.................#.#.#.#.#.#.#.#
24:  #...............#...............#...............#

Rule: 30
 0:                          #
 1:                         ###
 2:                        ##..#
 3:                       ##.####
 4:                      ##..#...#
 5:                     ##.####.###
 6:                    ##..#....#..#
 7:                   ##.####..######
 8:                  ##..#...###.....#
 9:                 ##.####.##..#...###
10:                ##..#....#.####.##..#
11:               ##.####..##.#....#.####
12:              ##..#...###..##..##.#...#
13:             ##.####.##..###.###..##.###
14:            ##..#....#.###...#..###..#..#
15:           ##.####..##.#..#.#####..#######
16:          ##..#...###..####.#....###......#
17:         ##.####.##..###....##..##..#....###
18:        ##..#....#.###..#..##.###.####..##..#
19:       ##.####..##.#..######..#...#...###.####
20:      ##..#...###..####.....####.###.##...#...#
21:     ##.####.##..###...#...##....#...#.#.###.###
22:    ##..#....#.###..#.###.##.#..###.##.#.#...#..#
23:   ##.####..##.#..###.#...#..####...#..#.##.######
24:  ##..#...###..####...##.#####...#.#####.#..#.....#

Racket[edit]

Uses solution to Elementary cellular automaton saved in file "Elementary_cellular_automata.rkt"

#lang racket
; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
(require racket/fixnum)
 
(define (wrap-rule-infinite v-in vl-1 offset)
(define l-bit-set? (bitwise-bit-set? (fxvector-ref v-in 0) usable-bits/fixnum-1))
(define r-bit-set? (bitwise-bit-set? (fxvector-ref v-in vl-1) 0))
 ;; if we need to extend left offset is reduced by 1
(define l-pad-words (if l-bit-set? 1 0))
(define r-pad-words (if r-bit-set? 1 0))
(define new-words (fx+ l-pad-words r-pad-words))
(cond
[(fx= 0 new-words) (values v-in vl-1 offset)] ; nothing changes
[else
(define offset- (if l-bit-set? (fx- offset 1) offset))
(define l-sequence (if l-bit-set? (in-value 0) (in-sequences)))
(define vl-1+ (fx+ vl-1 (fx+ l-pad-words r-pad-words)))
(define v-out
(for/fxvector
#:length (fx+ vl-1+ 1) #:fill 0 ; right padding
([f (in-sequences l-sequence (in-fxvector v-in))])
f))
(values v-out vl-1+ offset-)]))
 
(module+ main
(define ng/90/infinite (CA-next-generation 90 #:wrap-rule wrap-rule-infinite))
(for/fold ([v (fxvector #b10000000000000000000)]
[o 1]) ; start by pushing output right by one
([step (in-range 40)])
(show-automaton v #:step step #:push-right o)
(newline)
(ng/90/infinite v o)))
Output:
[         0] ..............................000000000010000000000000000000
[         1] ..............................000000000101000000000000000000
[         2] ..............................000000001000100000000000000000
[         3] ..............................000000010101010000000000000000
[         4] ..............................000000100000001000000000000000
[         5] ..............................000001010000010100000000000000
[         6] ..............................000010001000100010000000000000
[         7] ..............................000101010101010101000000000000
[         8] ..............................001000000000000000100000000000
[         9] ..............................010100000000000001010000000000
[        10] ..............................100010000000000010001000000000
[        11] 000000000000000000000000000001010101000000000101010100000000
[        12] 000000000000000000000000000010000000100000001000000010000000
[        13] 000000000000000000000000000101000001010000010100000101000000
[        14] 000000000000000000000000001000100010001000100010001000100000
[        15] 000000000000000000000000010101010101010101010101010101010000
[        16] 000000000000000000000000100000000000000000000000000000001000
[        17] 000000000000000000000001010000000000000000000000000000010100
[        18] 000000000000000000000010001000000000000000000000000000100010
[        19] 000000000000000000000101010100000000000000000000000001010101
[        20] 000000000000000000001000000010000000000000000000000010000000100000000000000000000000000000
[        21] 000000000000000000010100000101000000000000000000000101000001010000000000000000000000000000
[        22] 000000000000000000100010001000100000000000000000001000100010001000000000000000000000000000
[        23] 000000000000000001010101010101010000000000000000010101010101010100000000000000000000000000
[        24] 000000000000000010000000000000001000000000000000100000000000000010000000000000000000000000
[        25] 000000000000000101000000000000010100000000000001010000000000000101000000000000000000000000
[        26] 000000000000001000100000000000100010000000000010001000000000001000100000000000000000000000
[        27] 000000000000010101010000000001010101000000000101010100000000010101010000000000000000000000
[        28] 000000000000100000001000000010000000100000001000000010000000100000001000000000000000000000
[        29] 000000000001010000010100000101000001010000010100000101000001010000010100000000000000000000
[        30] 000000000010001000100010001000100010001000100010001000100010001000100010000000000000000000
[        31] 000000000101010101010101010101010101010101010101010101010101010101010101000000000000000000
[        32] 000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000
[        33] 000000010100000000000000000000000000000000000000000000000000000000000001010000000000000000
[        34] 000000100010000000000000000000000000000000000000000000000000000000000010001000000000000000
[        35] 000001010101000000000000000000000000000000000000000000000000000000000101010100000000000000
[        36] 000010000000100000000000000000000000000000000000000000000000000000001000000010000000000000
[        37] 000101000001010000000000000000000000000000000000000000000000000000010100000101000000000000
[        38] 001000100010001000000000000000000000000000000000000000000000000000100010001000100000000000
[        39] 010101010101010100000000000000000000000000000000000000000000000001010101010101010000000000
#fx(536879104 0 33554944)
0

Ruby[edit]

Translation of: Python
def notcell(c)
c.tr('01','10')
end
 
def eca_infinite(cells, rule)
neighbours2next = Hash[8.times.map{|i|["%03b"%i, "01"[rule[i]]]}]
c = cells
Enumerator.new do |y|
loop do
y << c
c = notcell(c[0])*2 + c + notcell(c[-1])*2 # Extend and pad the ends
c = (1..c.size-2).map{|i| neighbours2next[c[i-1..i+1]]}.join
end
end
end
 
if __FILE__ == $0
lines = 25
for rule in [90, 30]
puts "\nRule: %i" % rule
for i, c in (0...lines).zip(eca_infinite('1', rule))
puts '%2i: %s%s' % [i, ' '*(lines - i), c.tr('01', '.#')]
end
end
end

The output is the same as the Python entry.

Sidef[edit]

Translation of: Perl
func evolve(rule, bin) {
var offset = 0
var (l='', r='')
Inf.times {
bin.sub!(/^((.)\g2*)/, {|_s1, s2| l = s2; offset -= s2.len; s2*2 })
bin.sub!(/(.)\g1*$/, {|s1| r = s1; s1*2 })
printf("%5d| %s%s\n", offset, ' ' * (40 + offset), bin.tr('01','.#'))
bin = [l*3, 0.to(bin.len-3).map{|i| bin.substr(i, 3) }..., r*3 ].map { |t|
1 & (rule >> t.bin)
}.join
}
}
 
evolve(90, "010")
Output:
   -1|                                        ..#..
   -2|                                       ..#.#..
   -3|                                      ..#...#..
   -4|                                     ..#.#.#.#..
   -5|                                    ..#.......#..
   -6|                                   ..#.#.....#.#..
   -7|                                  ..#...#...#...#..
   -8|                                 ..#.#.#.#.#.#.#.#..
   -9|                                ..#...............#..
  -10|                               ..#.#.............#.#..
  -11|                              ..#...#...........#...#..
  -12|                             ..#.#.#.#.........#.#.#.#..
  -13|                            ..#.......#.......#.......#..
  -14|                           ..#.#.....#.#.....#.#.....#.#..
  -15|                          ..#...#...#...#...#...#...#...#..
  -16|                         ..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..
  -17|                        ..#...............................#..
  -18|                       ..#.#.............................#.#..
  -19|                      ..#...#...........................#...#..
  -20|                     ..#.#.#.#.........................#.#.#.#..
   ⋮

Tcl[edit]

Works with: Tcl version 8.6
package require Tcl 8.6
 
oo::class create InfiniteElementaryAutomaton {
variable rules
# Decode the rule number to get a collection of state mapping rules.
# In effect, "compiles" the rule number
constructor {ruleNumber} {
set ins {111 110 101 100 011 010 001 000}
set bits [split [string range [format %08b $ruleNumber] end-7 end] ""]
foreach input {111 110 101 100 011 010 001 000} state $bits {
dict set rules $input $state
}
}
 
# Apply the rule to an automaton state to get a new automaton state.
# We wrap the edges; the state space is circular.
method evolve {left state right} {
set state [list $left {*}$state $right]
set len [llength $state]
for {set i -1;set j 0;set k 1} {$j < $len} {incr i;incr j;incr k} {
set a [expr {$i<0 ? $left : [lindex $state $i]}]
set b [lindex $state $j]
set c [expr {$k==$len ? $right : [lindex $state $k]}]
lappend result [dict get $rules $a$b$c]
}
return $result
}
 
method evolveEnd {endState} {
return [dict get $rules $endState$endState$endState]
}
 
# Simple driver method; omit the initial state to get a centred dot
method run {steps {initialState "010"}} {
set cap [string repeat "\u2026" $steps]
set s [split [string map ". 0 # 1" $initialState] ""]
set left [lindex $s 0]
set right [lindex $s end]
set s [lrange $s 1 end-1]
for {set i 0} {$i < $steps} {incr i} {
puts $cap[string map "0 . 1 #" $left[join $s ""]$right]$cap
set s [my evolve $left $s $right]
set left [my evolveEnd $left]
set right [my evolveEnd $right]
set cap [string range $cap 1 end]
}
puts $cap[string map "0 . 1 #" $left[join $s ""]$right]$cap
}
}
 
foreach num {90 30} {
puts "Rule ${num}:"
set rule [InfiniteElementaryAutomaton new $num]
$rule run 25
$rule destroy
}
Output:
Rule 90:
………………………………………………………………….#.…………………………………………………………………
……………………………………………………………….#.#.………………………………………………………………
…………………………………………………………….#...#.……………………………………………………………
………………………………………………………….#.#.#.#.…………………………………………………………
……………………………………………………….#.......#.………………………………………………………
…………………………………………………….#.#.....#.#.……………………………………………………
………………………………………………….#...#...#...#.…………………………………………………
……………………………………………….#.#.#.#.#.#.#.#.………………………………………………
…………………………………………….#...............#.……………………………………………
………………………………………….#.#.............#.#.…………………………………………
……………………………………….#...#...........#...#.………………………………………
…………………………………….#.#.#.#.........#.#.#.#.……………………………………
………………………………….#.......#.......#.......#.…………………………………
……………………………….#.#.....#.#.....#.#.....#.#.………………………………
…………………………….#...#...#...#...#...#...#...#.……………………………
………………………….#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.…………………………
……………………….#...............................#.………………………
…………………….#.#.............................#.#.……………………
………………….#...#...........................#...#.…………………
……………….#.#.#.#.........................#.#.#.#.………………
…………….#.......#.......................#.......#.……………
………….#.#.....#.#.....................#.#.....#.#.…………
……….#...#...#...#...................#...#...#...#.………
…….#.#.#.#.#.#.#.#.................#.#.#.#.#.#.#.#.……
….#...............#...............#...............#.…
.#.#.............#.#.............#.#.............#.#.
Rule 30:
………………………………………………………………….#.…………………………………………………………………
……………………………………………………………….###.………………………………………………………………
…………………………………………………………….##..#.……………………………………………………………
………………………………………………………….##.####.…………………………………………………………
……………………………………………………….##..#...#.………………………………………………………
…………………………………………………….##.####.###.……………………………………………………
………………………………………………….##..#....#..#.…………………………………………………
……………………………………………….##.####..######.………………………………………………
…………………………………………….##..#...###.....#.……………………………………………
………………………………………….##.####.##..#...###.…………………………………………
……………………………………….##..#....#.####.##..#.………………………………………
…………………………………….##.####..##.#....#.####.……………………………………
………………………………….##..#...###..##..##.#...#.…………………………………
……………………………….##.####.##..###.###..##.###.………………………………
…………………………….##..#....#.###...#..###..#..#.……………………………
………………………….##.####..##.#..#.#####..#######.…………………………
……………………….##..#...###..####.#....###......#.………………………
…………………….##.####.##..###....##..##..#....###.……………………
………………….##..#....#.###..#..##.###.####..##..#.…………………
……………….##.####..##.#..######..#...#...###.####.………………
…………….##..#...###..####.....####.###.##...#...#.……………
………….##.####.##..###...#...##....#...#.#.###.###.…………
……….##..#....#.###..#.###.##.#..###.##.#.#...#..#.………
…….##.####..##.#..###.#...#..####...#..#.##.######.……
….##..#...###..####...##.#####...#.#####.#..#.....#.…
.##.####.##..###...#.##..#....#.##.#.....#####...###.

zkl[edit]

Translation of: D
nLines,flipCell := 25, fcn(c){ (c=="1") and "0" or "1" };
foreach rule in (T(90,30)){
println("\nRule: ", rule);
ruleBits:="%08.2B".fmt(rule); // eg 90-->"01011010"
neighs2next:=(8).pump(Dictionary(),
'wrap(n){ T("%03.2B".fmt(n), ruleBits.reverse()[n]) });
C:="1"; // C is "1"s and "0"s, I'll auto cast to Int as needed
foreach i in (nLines){
println("%2d: %s%s".fmt(i," "*(nLines - i), C.translate("01",".#")));
C=String(flipCell(C[0])*2, C, flipCell(C[-1])*2);
C=[1..C.len()-2].pump(String,'wrap(n){ neighs2next[C[n-1,3]] });
}
}
Output:
Rule: 90
 0:                          #
 1:                         #.#
 2:                        #...#
 3:                       #.#.#.#
 4:                      #.......#
 5:                     #.#.....#.#
 6:                    #...#...#...#
 7:                   #.#.#.#.#.#.#.#
 8:                  #...............#
 9:                 #.#.............#.#
10:                #...#...........#...#
11:               #.#.#.#.........#.#.#.#
12:              #.......#.......#.......#
13:             #.#.....#.#.....#.#.....#.#
14:            #...#...#...#...#...#...#...#
15:           #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
16:          #...............................#
17:         #.#.............................#.#
18:        #...#...........................#...#
19:       #.#.#.#.........................#.#.#.#
20:      #.......#.......................#.......#
21:     #.#.....#.#.....................#.#.....#.#
22:    #...#...#...#...................#...#...#...#
23:   #.#.#.#.#.#.#.#.................#.#.#.#.#.#.#.#
24:  #...............#...............#...............#

Rule: 30
 0:                          #
 1:                         ###
 2:                        ##..#
 3:                       ##.####
 4:                      ##..#...#
 5:                     ##.####.###
 6:                    ##..#....#..#
 7:                   ##.####..######
 8:                  ##..#...###.....#
 9:                 ##.####.##..#...###
10:                ##..#....#.####.##..#
11:               ##.####..##.#....#.####
12:              ##..#...###..##..##.#...#
13:             ##.####.##..###.###..##.###
14:            ##..#....#.###...#..###..#..#
15:           ##.####..##.#..#.#####..#######
16:          ##..#...###..####.#....###......#
17:         ##.####.##..###....##..##..#....###
18:        ##..#....#.###..#..##.###.####..##..#
19:       ##.####..##.#..######..#...#...###.####
20:      ##..#...###..####.....####.###.##...#...#
21:     ##.####.##..###...#...##....#...#.#.###.###
22:    ##..#....#.###..#.###.##.#..###.##.#.#...#..#
23:   ##.####..##.#..###.#...#..####...#..#.##.######
24:  ##..#...###..####...##.#####...#.#####.#..#.....#