Elementary cellular automaton: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 141: Line 141:




=={{header|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>
<lang csharp>
using System;
using System;
Line 293: Line 293:
50: .##.#...#.#...#.##.
50: .##.#...#.#...#.##.
</pre>
</pre>

=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<lang go>package main

Revision as of 09:29, 15 May 2014

Elementary cellular automaton 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.

EDIT: The requirements of this task have recently been slightly modified. Please update your solution accordingly if necessary.

An elementary cellular automaton is a one-dimensional cellular automaton where there are two possible states (labeled 0 and 1) and the rule to determine the state of a cell in the next generation depends only on the current state of the cell and its two immediate neighbors. Those three values can be encoded with three bits.

The rules of evolution are then encoded with eight bits indicating the outcome of each of the eight possibilities 111, 110, 101, 100, 011, 010, 001 and 000 in this order. Thus for instance the rule 13 means that a state is updated to 1 only in the cases 011, 010 and 000, since 13 in binary is 0b00001101.

The purpose of this task is to create a subroutine, program or function that allows to create and visualize the evolution of any of the 256 possible elementary cellular automaton of arbitrary space length and for any given initial state. You can demonstrate your solution with any automaton of your choice.

The space state should wrap: this means that the left-most cell should be considered as the right neighbor of the right-most cell, and reciprocally.

This task is basically a generalization of one-dimensional cellular automaton.

C

64 cells, edges are cyclic. <lang c>#include <stdio.h>

  1. include <limits.h>

typedef unsigned long long ull;

  1. define N (sizeof(ull) * CHAR_BIT)
  2. define B(x) (1ULL << (x))

void evolve(ull state, int rule) { int i; ull st;

printf("Rule %d:\n", rule); do { st = state; for (i = N; i--; ) putchar(st & B(i) ? '#' : '.'); putchar('\n');

for (state = i = 0; i < N; i++) if (rule & B(7 & (st>>(i-1) | st<<(N+1-i)))) state |= B(i); } while (st != state); }

int main(int argc, char **argv) { evolve(B(N/2), 90); evolve(B(N/4)|B(N - N/4), 30); // well, enjoy the fireworks

return 0; }</lang>

Output:
Rule 90:
................................#...............................
...............................#.#..............................
..............................#...#.............................
.............................#.#.#.#............................
............................#.......#...........................
...........................#.#.....#.#..........................
..........................#...#...#...#.........................
.........................#.#.#.#.#.#.#.#........................
........................#...............#.......................
                     ---(output snipped)---

Common Lisp

<lang lisp>(defun automaton (init rule &optional (stop 10))

 (labels ((next-gen (cells)
            (mapcar #'new-cell 
                    (cons (car (last cells)) cells)
                    cells
                    (append (cdr cells) (list (car cells)))))
          (new-cell (left current right)
            (let ((shift (+ (* left 4) (* current 2) right)))
              (if (logtest rule (ash 1 shift)) 1 0)))
          (pretty-print (cells)
            (format T "~{~a~}~%" 
                    (mapcar (lambda (x) (if (zerop x) #\. #\#))
                            cells))))
   (loop for cells = init then (next-gen cells)
         for i below stop
         do (pretty-print cells))))

(automaton '(0 0 0 0 0 0 1 0 0 0 0 0 0) 90)</lang>

Output:
......#......
.....#.#.....
....#...#....
...#.#.#.#...
..#.......#..
.#.#.....#.#.
#...#...#...#
##.#.#.#.#.##
.#.........#.
#.#.......#.#

C++

<lang cpp>#include <bitset>

  1. include <stdio.h>
  1. define SIZE 80
  2. define RULE 30
  3. define RULE_TEST(x) (RULE & 1 << (7 & (x)))

void evolve(std::bitset<SIZE> &s) {

   int i;
   std::bitset<SIZE> t(0);
   t[SIZE-1] = RULE_TEST( s[0] << 2 | s[SIZE-1] << 1 | s[SIZE-2] );
   t[     0] = RULE_TEST( s[1] << 2 | s[     0] << 1 | s[SIZE-1] );
   for (i = 1; i < SIZE-1; i++)

t[i] = RULE_TEST( s[i+1] << 2 | s[i] << 1 | s[i-1] );

   for (i = 0; i < SIZE; i++) s[i] = t[i];

} void show(std::bitset<SIZE> s) {

   int i;
   for (i = SIZE; --i; ) printf("%c", s[i] ? '#' : ' ');
   printf("\n");

} int main() {

   int i;
   std::bitset<SIZE> state(1);
   state <<= SIZE / 2;
   for (i=0; i<10; i++) {

show(state); evolve(state);

   }
   return 0;

}</lang>

Output:
                                       #                                       |
                                      ###                                      |
                                     ##  #                                     |
                                    ## ####                                    |
                                   ##  #   #                                   |
                                  ## #### ###                                  |
                                 ##  #    #  #                                 |
                                ## ####  ######                                |
                               ##  #   ###     #                               |
                              ## #### ##  #   ###                              |


C#

<lang csharp> using System; using System.Collections; namespace ElementaryCellularAutomaton {

   class Automata
   {
       BitArray cells, ncells;
       const int MAX_CELLS = 19;
       public void run()
       {
           cells = new BitArray(MAX_CELLS);
           ncells = new BitArray(MAX_CELLS);
           while (true)
           {
               Console.Clear();
               Console.WriteLine("What Rule do you want to visualize");
               doRule(int.Parse(Console.ReadLine()));
               Console.WriteLine("Press any key to continue...");
               Console.ReadKey();
           }
       }
       private byte getCells(int index)
       {
           byte b;
           int i1 = index - 1,
               i2 = index,
               i3 = index + 1;
           if (i1 < 0) i1 = MAX_CELLS - 1;
           if (i3 >= MAX_CELLS) i3 -= MAX_CELLS;
           b = Convert.ToByte(
               4 * Convert.ToByte(cells.Get(i1)) +
               2 * Convert.ToByte(cells.Get(i2)) +
               Convert.ToByte(cells.Get(i3)));
           return b;
       }
       private string getBase2(int i)
       {
           string s = Convert.ToString(i, 2);
           while (s.Length < 8)
           { s = "0" + s; }
           return s;
       }
       private void doRule(int rule)
       {
           Console.Clear();
           string rl = getBase2(rule);
           cells.SetAll(false);
           ncells.SetAll(false);
           cells.Set(MAX_CELLS / 2, true);
           Console.WriteLine("Rule: " + rule + "\n----------\n");
           for (int gen = 0; gen < 51; gen++)
           {
               Console.Write("{0, 4}", gen + ": ");
               foreach (bool b in cells)
                   Console.Write(b ? "#" : ".");
               Console.WriteLine("");
               int i = 0;
               while (true)
               {
                   byte b = getCells(i);
                   ncells[i] = '1' == rl[7 - b] ? true : false;
                   if (++i == MAX_CELLS) break;
               }
               i = 0;
               foreach (bool b in ncells)
                   cells[i++] = b;
           }
           Console.WriteLine("");
       }
   };
   class Program
   {
       static void Main(string[] args)
       {
           Automata t = new Automata();
           t.run();
       }
   }

} </lang>

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: .#..#.........#..#.
25: #.##.#.......#.##.#
26: #.##..#.....#..##.#
27: #.####.#...#.####.#
28: #.#..#..#.#..#..#.#
29: #..##.##...##.##..#
30: #####.###.###.#####
31: ....#.#.#.#.#.#....
32: ...#...........#...
33: ..#.#.........#.#..
34: .#...#.......#...#.
35: #.#.#.#.....#.#.#.#
36: #......#...#......#
37: ##....#.#.#.#....##
38: .##..#.......#..##.
39: #####.#.....#.#####
40: ....#..#...#..#....
41: ...#.##.#.#.##.#...
42: ..#..##.....##..#..
43: .#.#####...#####.#.
44: #..#...##.##...#..#
45: ###.#.###.###.#.###
46: ..#...#.#.#.#...#..
47: .#.#.#.......#.#.#.
48: #.....#.....#.....#
49: ##...#.#...#.#...##
50: .##.#...#.#...#.##.

Go

<lang go>package main

import (

   "fmt"
   "math/big"
   "math/rand"
   "strings"

)

func main() {

   const cells = 20
   const generations = 9
   fmt.Println("Single 1, rule 90:")
   a := big.NewInt(1)
   a.Lsh(a, cells/2)
   elem(90, cells, generations, a)
   fmt.Println("Random intial state, rule 30:")
   a = big.NewInt(1)
   a.Rand(rand.New(rand.NewSource(3)), a.Lsh(a, cells))
   elem(30, cells, generations, a)

}

func elem(rule uint, cells, generations int, a *big.Int) {

   output := func() {
       fmt.Println(strings.Replace(strings.Replace(
           fmt.Sprintf("%0*b", cells, a), "0", " ", -1), "1", "#", -1))
   }
   output()
   a1 := new(big.Int)
   set := func(cell int, k uint) {
       a1.SetBit(a1, cell, rule>>k&1)
   }
   last := cells - 1
   for r := 0; r < generations; r++ {
       k := a.Bit(last) | a.Bit(0)<<1 | a.Bit(1)<<2
       set(0, k)
       for c := 1; c < last; c++ {
           k = k>>1 | a.Bit(c+1)<<2
           set(c, k)
       }
       set(last, k>>1|a.Bit(0)<<2)
       a, a1 = a1, a
       output()
   }

}</lang>

Output:
Single 1, rule 90:
         #          
        # #         
       #   #        
      # # # #       
     #       #      
    # #     # #     
   #   #   #   #    
  # # # # # # # #   
 #               #  
# #             # # 
Random intial state, rule 30:
 #   # #  ####     #
 ## ## ####   #   ##
 #  #  #   # ### ## 
######### ## #   # #
          #  ## ## #
#        #####  #  #
 #      ##    ######
 ##    ## #  ##     
## #  ##  #### #    
#  #### ###    ##  #

Perl

Translation of: Perl 6

<lang perl>use strict; use warnings;

package Automaton {

   sub new {

my $class = shift; my $rule = [ reverse split //, sprintf "%08b", shift ]; return bless { rule => $rule, cells => [ @_ ] }, $class;

   }
   sub next {

my $this = shift; my @previous = @{$this->{cells}}; $this->{cells} = [ @{$this->{rule}}[ map { 4*$previous[($_ - 1) % @previous] + 2*$previous[$_] + $previous[($_ + 1) % @previous] } 0 .. @previous - 1 ] ]; return $this;

   }
   use overload
   q{""} => sub {

my $this = shift; join , map { $_ ? '#' : ' ' } @{$this->{cells}}

   };

}

my @a = map 0, 1 .. 91; $a[45] = 1; my $a = Automaton->new(90, @a);

for (1..40) {

   print "|$a|\n"; $a->next;

}</lang>

Output:
|                                             #                                             |
|                                            # #                                            |
|                                           #   #                                           |
|                                          # # # #                                          |
|                                         #       #                                         |
|                                        # #     # #                                        |
|                                       #   #   #   #                                       |
|                                      # # # # # # # #                                      |
|                                     #               #                                     |
|                                    # #             # #                                    |
|                                   #   #           #   #                                   |
|                                  # # # #         # # # #                                  |
|                                 #       #       #       #                                 |
|                                # #     # #     # #     # #                                |
|                               #   #   #   #   #   #   #   #                               |
|                              # # # # # # # # # # # # # # # #                              |
|                             #                               #                             |
|                            # #                             # #                            |
|                           #   #                           #   #                           |
|                          # # # #                         # # # #                          |
|                         #       #                       #       #                         |
|                        # #     # #                     # #     # #                        |
|                       #   #   #   #                   #   #   #   #                       |
|                      # # # # # # # #                 # # # # # # # #                      |
|                     #               #               #               #                     |
|                    # #             # #             # #             # #                    |
|                   #   #           #   #           #   #           #   #                   |
|                  # # # #         # # # #         # # # #         # # # #                  |
|                 #       #       #       #       #       #       #       #                 |
|                # #     # #     # #     # #     # #     # #     # #     # #                |
|               #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #               |
|              # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #              |
|             #                                                               #             |
|            # #                                                             # #            |
|           #   #                                                           #   #           |
|          # # # #                                                         # # # #          |
|         #       #                                                       #       #         |
|        # #     # #                                                     # #     # #        |
|       #   #   #   #                                                   #   #   #   #       |
|      # # # # # # # #                                                 # # # # # # # #      |

Perl 6

<lang perl6>class Automaton {

   has ($.rule, @.cells);
   method gist { <| |>.join: @!cells.map({$_ ?? '#' !! ' '}).join }
   method code { $!rule.fmt("%08b").flip.comb }
   method succ {
       self.new: :$!rule, :cells(
           self.code[
                  (4 X* @!cells.rotate(-1))
               Z+ (2 X* @!cells)
               Z+       @!cells.rotate(1)
           ]
       )
   }

}

my $size = 10; my Automaton $a .= new:

   :rule(30),
   :cells( 0 xx $size, 1, 0 xx $size );

say $a++ for ^$size;</lang>

Output:
|          #          |
|         ###         |
|        ##  #        |
|       ## ####       |
|      ##  #   #      |
|     ## #### ###     |
|    ##  #    #  #    |
|   ## ####  ######   |
|  ##  #   ###     #  |
| ## #### ##  #   ### |

Unfortunately that version is somewhat slow due to the (as yet) unoptimized vector operations, as well as rebuilding the code every iteration. The following version runs about ten times faster and produces the same output: <lang perl6>class Automaton {

   has $.rule;
   has @.cells;
   has @.code;
   method new (|args) { self.bless(|args).init }
   method init { @!code ||= $!rule.fmt("%08b").flip.comb».Int; self; }
   method gist { <| |>.join: @!cells.map({$_ ?? '#' !! ' '}).join }
   method succ {
       my \size = +@!cells;
       self.new: :$!rule, :@!code, :cells(
           @!code[
               for ^size -> \i {
                   4 * @!cells[(i - 1) % size] +
                   2 * @!cells[i] +
                       @!cells[(i+1) % size];
               }
           ]
       );
   }

}

my $size = 10; my Automaton $a .= new:

   :rule(30),
   :cells( 0 xx $size, 1, 0 xx $size );

say $a++ for ^$size;</lang>

Python

Python: Zero padded

Note: This only fitted the original task description that read:

You can deal with the limit conditions (what happens on the borders of the space) in any way you please.

<lang python>def eca(cells, rule):

   lencells = len(cells)
   c = "0" + cells + "0"    # Zero pad the ends
   rulebits = '{0:08b}'.format(rule)
   neighbours2next = {'{0:03b}'.format(n):rulebits[::-1][n] for n in range(8)}
   yield c[1:-1]
   while True:
       c = .join(['0',
                    .join(neighbours2next[c[i-1:i+2]]
                            for i in range(1,lencells+1)),
                    '0'])
       yield c[1:-1]

if __name__ == '__main__':

   lines, start, rules = 50, '0000000001000000000', (90, 30, 122)
   zipped = [range(lines)] + [eca(start, rule) for rule in rules]
   print('\n   Rules: %r' % (rules,))
   for data in zip(*zipped):
       i = data[0]
       cells = data[1:]
       print('%2i: %s' % (i, '    '.join(cells).replace('0', '.').replace('1', '#')))</lang>
Output:

(Note how Rule 30 does not look random).

   Rules: (90, 30, 122)
 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: .#...#.......#...#.    #.#.#...#.##.#.#.#.    .#.#.#.#.#.#.#.#.#.
25: #.#.#.#.....#.#.#.#    #.#.##.##.#..#.#.##    #.#.#.#.#.#.#.#.#.#
26: .......#...#.......    #.#.#..#..####.#.#.    .#.#.#.#.#.#.#.#.#.
27: ......#.#.#.#......    #.#.#######....#.##    #.#.#.#.#.#.#.#.#.#
28: .....#.......#.....    #.#.#......#..##.#.    .#.#.#.#.#.#.#.#.#.
29: ....#.#.....#.#....    #.#.##....#####..##    #.#.#.#.#.#.#.#.#.#
30: ...#...#...#...#...    #.#.#.#..##....###.    .#.#.#.#.#.#.#.#.#.
31: ..#.#.#.#.#.#.#.#..    #.#.#.####.#..##..#    #.#.#.#.#.#.#.#.#.#
32: .#...............#.    #.#.#.#....####.###    .#.#.#.#.#.#.#.#.#.
33: #.#.............#.#    #.#.#.##..##....#..    #.#.#.#.#.#.#.#.#.#
34: ...#...........#...    #.#.#.#.###.#..###.    .#.#.#.#.#.#.#.#.#.
35: ..#.#.........#.#..    #.#.#.#.#...####..#    #.#.#.#.#.#.#.#.#.#
36: .#...#.......#...#.    #.#.#.#.##.##...###    .#.#.#.#.#.#.#.#.#.
37: #.#.#.#.....#.#.#.#    #.#.#.#.#..#.#.##..    #.#.#.#.#.#.#.#.#.#
38: .......#...#.......    #.#.#.#.####.#.#.#.    .#.#.#.#.#.#.#.#.#.
39: ......#.#.#.#......    #.#.#.#.#....#.#.##    #.#.#.#.#.#.#.#.#.#
40: .....#.......#.....    #.#.#.#.##..##.#.#.    .#.#.#.#.#.#.#.#.#.
41: ....#.#.....#.#....    #.#.#.#.#.###..#.##    #.#.#.#.#.#.#.#.#.#
42: ...#...#...#...#...    #.#.#.#.#.#..###.#.    .#.#.#.#.#.#.#.#.#.
43: ..#.#.#.#.#.#.#.#..    #.#.#.#.#.####...##    #.#.#.#.#.#.#.#.#.#
44: .#...............#.    #.#.#.#.#.#...#.##.    .#.#.#.#.#.#.#.#.#.
45: #.#.............#.#    #.#.#.#.#.##.##.#.#    #.#.#.#.#.#.#.#.#.#
46: ...#...........#...    #.#.#.#.#.#..#..#.#    .#.#.#.#.#.#.#.#.#.
47: ..#.#.........#.#..    #.#.#.#.#.#######.#    #.#.#.#.#.#.#.#.#.#
48: .#...#.......#...#.    #.#.#.#.#.#.......#    .#.#.#.#.#.#.#.#.#.
49: #.#.#.#.....#.#.#.#    #.#.#.#.#.##.....##    #.#.#.#.#.#.#.#.#.#

Python: wrap

The ends of the cells wrap-around. <lang python>def eca_wrap(cells, rule):

   lencells = len(cells)
   rulebits = '{0:08b}'.format(rule)
   neighbours2next = {tuple('{0:03b}'.format(n)):rulebits[::-1][n] for n in range(8)}
   c = cells
   while True:
       yield c
       c = .join(neighbours2next[(c[i-1], c[i], c[(i+1) % lencells])] for i in range(lencells))

if __name__ == '__main__':

   lines, start, rules = 50, '0000000001000000000', (90, 30, 122)
   zipped = [range(lines)] + [eca_wrap(start, rule) for rule in rules]
   print('\n   Rules: %r' % (rules,))
   for data in zip(*zipped):
       i = data[0]
       cells = data[1:]
       print('%2i: %s' % (i, '    '.join(cells).replace('0', '.').replace('1', '#')))

</lang>

Output:
   Rules: (90, 30, 122)
 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: .#..#.........#..#.    ..#.##....#.####..#    ####...........####
25: #.##.#.......#.##.#    ###.#.#..##.#...###    ...##.........##...
26: #.##..#.....#..##.#    ....#.####..##.##..    ..####.......####..
27: #.####.#...#.####.#    ...##.#...###..#.#.    .##..##.....##..##.
28: #.#..#..#.#..#..#.#    ..##..##.##..###.##    ########...########
29: #..##.##...##.##..#    ###.###..#.###...#.    .......##.##.......
30: #####.###.###.#####    #...#..###.#..#.##.    ......#######......
31: ....#.#.#.#.#.#....    ##.#####...####.#..    .....##.....##.....
32: ...#...........#...    #..#....#.##....###    ....####...####....
33: ..#.#.........#.#..    .####..##.#.#..##..    ...##..##.##..##...
34: .#...#.......#...#.    ##...###..#.####.#.    ..###############..
35: #.#.#.#.....#.#.#.#    #.#.##..###.#....#.    .##.............##.
36: #......#...#......#    #.#.#.###...##..##.    ####...........####
37: ##....#.#.#.#....##    #.#.#.#..#.##.###..    ...##.........##...
38: .##..#.......#..##.    #.#.#.####.#..#..##    ..####.......####..
39: #####.#.....#.#####    ..#.#.#....#######.    .##..##.....##..##.
40: ....#..#...#..#....    .##.#.##..##......#    ########...########
41: ...#.##.#.#.##.#...    .#..#.#.###.#....##    .......##.##.......
42: ..#..##.....##..#..    .####.#.#...##..##.    ......#######......
43: .#.#####...#####.#.    ##....#.##.##.###.#    .....##.....##.....
44: #..#...##.##...#..#    ..#..##.#..#..#...#    ....####...####....
45: ###.#.###.###.#.###    ######..########.##    ...##..##.##..##...
46: ..#...#.#.#.#...#..    ......###........#.    ..###############..
47: .#.#.#.......#.#.#.    .....##..#......###    .##.............##.
48: #.....#.....#.....#    #...##.####....##..    ####...........####
49: ##...#.#...#.#...##    ##.##..#...#..##.##    ...##.........##...

Python: Infinite

Note: This only fitted the original task description that read:

You can deal with the limit conditions (what happens on the borders of the space) in any way you please.

Pad and extend with inverse of end cells on each iteration. <lang python>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, start, rules = 20, '1', (90, 30, 122)
   zipped = [range(lines)] + [eca_infinite(start, rule) for rule in rules]
   print('\n   Rules: %r' % (rules,))
   for data in zip(*zipped):
       i = data[0]
       cells = ['%s%s%s' % (' '*(lines - i), c, ' '*(lines - i)) for c in data[1:]]
       print('%2i: %s' % (i, '    '.join(cells).replace('0', '.').replace('1', '#')))</lang>
Output:
   Rules: (90, 30, 122)
 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:  #...............#...............#...............#      ##..#...###..####...##.#####...#.#####.#..#.....#      #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# 

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

oo::class create ElementaryAutomaton {

   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 {state} {

set len [llength $state] for {set i 0} {$i < $len} {incr i} { lappend result [dict get $rules [ lindex $state [expr {($i-1)%$len}]][ lindex $state $i][ lindex $state [expr {($i+1)%$len}]]] } return $result

   }
   # Simple driver method; omit the initial state to get a centred dot
   method run {steps {initialState ""}} {

if {[llength [info level 0]] < 4} { set initialState "[string repeat . $steps]1[string repeat . $steps]" } set s [split [string map ". 0 # 1" $initialState] ""] for {set i 0} {$i < $steps} {incr i} { puts [string map "0 . 1 #" [join $s ""]] set s [my evolve $s] } puts [string map "0 . 1 #" [join $s ""]]

   }

}</lang> Demonstrating: <lang tcl>puts "Rule 90 (with default state):" ElementaryAutomaton create rule90 90 rule90 run 20 puts "\nRule 122:" [ElementaryAutomaton new 122] run 25 "..........#......…."</lang>

Output:
Rule 90 (with default state):
....................#....................
...................#.#...................
..................#...#..................
.................#.#.#.#.................
................#.......#................
...............#.#.....#.#...............
..............#...#...#...#..............
.............#.#.#.#.#.#.#.#.............
............#...............#............
...........#.#.............#.#...........
..........#...#...........#...#..........
.........#.#.#.#.........#.#.#.#.........
........#.......#.......#.......#........
.......#.#.....#.#.....#.#.....#.#.......
......#...#...#...#...#...#...#...#......
.....#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.....
....#...............................#....
...#.#.............................#.#...
..#...#...........................#...#..
.#.#.#.#.........................#.#.#.#.
#.......#.......................#.......#

Rule 122:
..........#..........
.........#.#.........
........#.#.#........
.......#.#.#.#.......
......#.#.#.#.#......
.....#.#.#.#.#.#.....
....#.#.#.#.#.#.#....
...#.#.#.#.#.#.#.#...
..#.#.#.#.#.#.#.#.#..
.#.#.#.#.#.#.#.#.#.#.
#.#.#.#.#.#.#.#.#.#.#
##.#.#.#.#.#.#.#.#.##
.##.#.#.#.#.#.#.#.##.
####.#.#.#.#.#.#.####
...##.#.#.#.#.#.##...
..####.#.#.#.#.####..
.##..##.#.#.#.##..##.
########.#.#.########
.......##.#.##.......
......####.####......
.....##..###..##.....
....######.######....
...##....###....##...
..####..##.##..####..
.##..###########..##.
######.........######