Elementary cellular automaton: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(12 intermediate revisions by 9 users not shown)
Line 1:
{{task}}[[Category:Cellular automata]]
 
An '''[[wp:elementary cellular automaton|elementary cellular automaton]]''' is a one-dimensional [[wp:cellular automaton|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.
Line 13:
This task is basically a generalization of [[one-dimensional cellular automata]].
 
 
;Related tasks:
* [[One-dimensional_cellular_automata|One-dimensional cellular automata]]
 
;See also
Line 21 ⟶ 24:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">V SIZE = 32
V LINES = SIZE I/ 2
V RULE = 90
Line 48 ⟶ 51:
L 1..LINES
show(state)
evolve(&state)</langsyntaxhighlight>
 
{{out}}
Line 72 ⟶ 75:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE ROW_LEN="320"
DEFINE MAX_COL="319"
DEFINE MAX_ROW="191"
Line 158 ⟶ 161:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Elementary_cellular_automaton.png Screenshot from Atari 8-bit computer]
Line 164 ⟶ 167:
=={{header|Ada}}==
{{works with|Ada 2012}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
procedure Elementary_Cellular_Automaton is
Line 230 ⟶ 233:
Generations => 25);
end Elementary_Cellular_Automaton;
</syntaxhighlight>
</lang>
{{Output}}
<pre>Rule 90 :
Line 297 ⟶ 300:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # elementary cellular automaton #
COMMENT returns the next state from state using rule; s must be at least 2 characters long and consist of # and - only COMMENT
PROC next state = ( STRING state, INT rule )STRING:
Line 339 ⟶ 342:
test( "---------#---------", 60 );
test( "---------#---------", 90 )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 382 ⟶ 385:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">state := StrSplit("0000000001000000000")
rule := 90
output := "Rule: " rule
Line 428 ⟶ 431:
result .= val = 1 ? "#" : "."
return result
}</langsyntaxhighlight>
{{Output}}
<pre>Rule: 90
Line 444 ⟶ 447:
=={{header|C}}==
64 cells, edges are cyclic.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <limits.h>
 
Line 474 ⟶ 477:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 491 ⟶ 494:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections;
Line 584 ⟶ 587:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 644 ⟶ 647:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <bitset>
#include <stdio.h>
 
Line 674 ⟶ 677:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> # |
Line 688 ⟶ 691:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">class Rule(number) satisfies Correspondence<Boolean[3], Boolean> {
shared Byte number;
Line 773 ⟶ 776:
automaton.evolve();
}
}</langsyntaxhighlight>
{{out}}
<pre>Rule #90
Line 791 ⟶ 794:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun automaton (init rule &optional (stop 10))
(labels ((next-gen (cells)
(mapcar #'new-cell
Line 811 ⟶ 814:
do (pretty-print cells))))
 
(automaton '(0 0 0 0 0 0 1 0 0 0 0 0 0) 90)</langsyntaxhighlight>
 
{{Out}}
Line 827 ⟶ 830:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.range, std.algorithm, std.typecons;
 
enum mod = (in int n, in int m) pure nothrow @safe @nogc => ((n % m) + m) % m;
Line 862 ⟶ 865:
eca.popFront;
}
}</langsyntaxhighlight>
{{out}}
<pre>Rules: [90, 30, 122]
Line 915 ⟶ 918:
48: #.....#.....#.....# #...##.####....##.. ####...........####
49: ##...#.#...#.#...## ##.##..#...#..##.## ...##.........##...</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
global celln[] cell[] .
len map[] 8
#
proc evolve . .
for i = 1 to len cell[]
ind = 0
for j = i + 1 downto i - 1
ind = ind * 2 + cell[j mod1 len cell[]]
.
celln[i] = map[ind + 1]
.
swap celln[] cell[]
.
proc show . .
c$[] = [ "." "#" ]
for v in cell[]
write c$[v + 1]
.
print ""
.
proc run map count inp[] . .
for i to 8
map[i] = map mod 2
map = map div 2
.
swap cell[] inp[]
len celln[] len cell[]
show
for i to count
evolve
show
.
print ""
.
run 104 9 [ 0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 ]
run 90 9 [ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ]
run 122 15 [ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ]
</syntaxhighlight>
{{out}}
<pre>
.###.##.#.#.#.#..#..
.#.#####.#.#.#......
..##...##.#.#.......
..##...###.#........
..##...#.##.........
..##....###.........
..##....#.#.........
..##.....#..........
..##................
..##................
 
.......#.......
......#.#......
.....#...#.....
....#.#.#.#....
...#.......#...
..#.#.....#.#..
.#...#...#...#.
#.#.#.#.#.#.#.#
#.............#
##...........##
 
.......#.......
......#.#......
.....#.#.#.....
....#.#.#.#....
...#.#.#.#.#...
..#.#.#.#.#.#..
.#.#.#.#.#.#.#.
#.#.#.#.#.#.#.#
##.#.#.#.#.#.##
.##.#.#.#.#.##.
####.#.#.#.####
...##.#.#.##...
..####.#.####..
.##..##.##..##.
###############
...............
</pre>
 
=={{header|EchoLisp}}==
Pictures of the (nice) generated colored bit-maps : The Escher like [http://www.echolalie.org/echolisp/images/automaton-1.png (task 90 5)] and the fractal like [http://www.echolalie.org/echolisp/images/automaton-2.png (task 22 1)]
 
<langsyntaxhighlight lang="scheme">
(lib 'types) ;; int32 vectors
(lib 'plot)
Line 979 ⟶ 1,064:
→ #( #( 0 0 0) #( 0 -5052980 0) #( 0 -5052980 -5052980))
 
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Elementary_cellular_automaton do
def run(start_str, rule, times) do
IO.puts "rule : #{rule}"
Line 1,010 ⟶ 1,095:
pad = String.duplicate("0", 14)
str = pad <> "1" <> pad
Elementary_cellular_automaton.run(str, 18, 25)</langsyntaxhighlight>
 
{{out}}
Line 1,044 ⟶ 1,129:
=={{header|F_Sharp|F#}}==
===The Function===
<langsyntaxhighlight lang="fsharp">
// Elementary Cellular Automaton . Nigel Galloway: July 31st., 2019
let eca N=
let N=Array.init 8 (fun n->(N>>>n)%2)
Seq.unfold(fun G->Some(G,[|yield Array.last G; yield! G; yield Array.head G|]|>Array.windowed 3|>Array.map(fun n->N.[n.[2]+2*n.[1]+4*n.[0]])))
</syntaxhighlight>
</lang>
===The Task===
<langsyntaxhighlight lang="fsharp">
eca 90 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,137 ⟶ 1,222:
@ @
</pre>
<langsyntaxhighlight lang="fsharp">
eca 110 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,225 ⟶ 1,310:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping io kernel math math.bits
math.combinatorics sequences sequences.extras ;
 
Line 1,242 ⟶ 1,327:
[ dup show-state next-state ] times 2drop ;
 
90 show-automaton</langsyntaxhighlight>
{{out}}
<pre>
Line 1,263 ⟶ 1,348:
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
</pre>
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<br>
<syntaxhighlight lang="forth">#! /usr/bin/gforth
 
\ Elementary cellular automaton
 
\ checks if bit ix (where 0 is the least significant one) of u is set
: bit? ( u ix -- f )
1 over lshift rot and swap rshift 1 =
;
 
\ displays a bit-array with n bits starting at address addr
: .arr ( addr n -- )
0 DO
dup @ IF 1 ELSE 0 THEN .
cell +
LOOP
drop
;
 
\ applies rule r to the bit-array with n bits starting at address addr
: generation { r addr n -- }
addr n 1- cells + @ IF 2 ELSE 0 THEN
addr @ tuck IF 1 ELSE 0 THEN
+
n 0 ?DO
i n 1- = IF swap ELSE addr i 1+ cells + @ THEN
IF 1 ELSE 0 THEN
swap 1 lshift +
r over bit?
addr i cells + !
3 and
LOOP
drop
;
 
\ evolves a rule over several generations
: evolve { r addr n m -- }
." P1" cr
n . m . cr
addr n .arr cr
m 1- 0 ?DO
r addr n generation
addr n .arr cr
LOOP
;
 
\ evolves a rule over several generations and saves the result as a pbm-image
\ and writes the result to file c-addr u
: evolve-pbm ( r addr n m c-addr u -- )
w/o create-file throw dup 2>r
['] evolve r> outfile-execute
r> close-file throw
;
 
CREATE arr 1000 cells allot
 
arr 1000 cells erase
true arr 500 cells + !
30 arr 1000 2000 s" rule-030.pbm" evolve-pbm
 
arr 1000 cells erase
true arr 500 cells + !
60 arr 1000 2000 s" rule-060.pbm" evolve-pbm
 
arr 1000 cells erase
true arr 500 cells + !
90 arr 1000 2000 s" rule-090.pbm" evolve-pbm
 
arr 1000 cells erase
true arr 500 cells + !
110 arr 1000 2000 s" rule-110.pbm" evolve-pbm
 
bye
</syntaxhighlight>
 
{{out}}
starting with 1000 cells, where all but one in the middle are initially empty, evolving over 2000 generations:
 
* rule 30 [https://commons.wikimedia.org/wiki/File:Rule-030.png]
* rule 60 [https://commons.wikimedia.org/wiki/File:Rule-060.png]
* rule 90 [https://commons.wikimedia.org/wiki/File:Rule-090.png]
* rule 110 [https://commons.wikimedia.org/wiki/File:Rule-110.png]
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#define NCELLS 400
#define border 16
Line 1,333 ⟶ 1,501:
end if
key = ucase(inkey)
loop until ucase(key) = "Q"</langsyntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Elementary_cellular_automaton}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Elementary cellular automaton 01.png]]
In '''[https://formulae.org/?example=Elementary_cellular_automaton this]''' page you can see the program(s) related to this task and their results.
 
'''Test case.''' Generating all the 256 values:
 
[[File:Fōrmulæ - Elementary cellular automaton 02.png]]
 
[[File:Fōrmulæ - Elementary cellular automaton 03.png]]
 
=={{header|Furor}}==
<syntaxhighlight lang="furor">
<lang Furor>
argc 4 < { ."Usage: " 0 argv sprint SPACE 1 argv sprint SPACE ."rule size\n"
."The \"rule\" and \"size\" are numbers.\n"
Line 1,388 ⟶ 1,562:
 
// =================================================
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,429 ⟶ 1,603:
</pre>
 
=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude system.uh
###sysinclude args.uh
###sysinclude str.uh
#g argc 4 < { ."Usage: " 0 argv sprint SPACE 1 argv sprint SPACE ."rule size\n"
."The \"rule\" and \"size\" are numbers.\n"
."0<=rule<=255\n" end }
terminallines 3 - sto gen
3 argv #s (#g) sto maxcell
2 argv (#g) sto rule
#g argc 5 >= { 4 argv #s (#g) sto gen }
@maxcell mem !maximize sto livingspace
@maxcell mem sto originallivingspace
@maxcell {{ @livingspace {{}} 0 inv [] }} @livingspace @maxcell #g 2 / 1 inv []
{..
originallivingspace @livingspace #s =
livingspace {~ #k {~?~} { '* }{ '. } print ~} NL
#g
livingspace~ lsp: {{
zero aa
@originallivingspace {{}} ? @maxcell -- [] { 4 sto aa }
@originallivingspace {{}} [] { @aa 2 | sto aa }
@originallivingspace {{+}} @maxcell == { 0 }{ {{+}} } [] { @aa 1 | sto aa }
8 {{ @rule 1 {{}} << & {
{{}} @aa == { @livingspace {{}}§lsp 1 inv [] {{<}}§lsp }
}
}}
z: @livingspace {{}} 0 inv [] {{<}}
}}
 
#s @originallivingspace @livingspace == { {.>.} }
@gen { {..} @gen #g == { {.>.} } }
{..} sto l
..}
."Generations: " l #g printnl
@livingspace inv mem
@originallivingspace inv mem
end
{ „maxcell” }
{ „rule” }
{ „state” }
{ „livingspace” }
{ „originallivingspace” }
{ „aa” }
{ „gen” }
 
// ===============================================================================
 
</syntaxhighlight>
{{out}}
<pre>
peri eca.upu 90 100 33
..................................................*.................................................
.................................................*.*................................................
................................................*...*...............................................
...............................................*.*.*.*..............................................
..............................................*.......*.............................................
.............................................*.*.....*.*............................................
............................................*...*...*...*...........................................
...........................................*.*.*.*.*.*.*.*..........................................
..........................................*...............*.........................................
.........................................*.*.............*.*........................................
........................................*...*...........*...*.......................................
.......................................*.*.*.*.........*.*.*.*......................................
......................................*.......*.......*.......*.....................................
.....................................*.*.....*.*.....*.*.....*.*....................................
....................................*...*...*...*...*...*...*...*...................................
...................................*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..................................
..................................*...............................*.................................
.................................*.*.............................*.*................................
................................*...*...........................*...*...............................
...............................*.*.*.*.........................*.*.*.*..............................
..............................*.......*.......................*.......*.............................
.............................*.*.....*.*.....................*.*.....*.*............................
............................*...*...*...*...................*...*...*...*...........................
...........................*.*.*.*.*.*.*.*.................*.*.*.*.*.*.*.*..........................
..........................*...............*...............*...............*.........................
.........................*.*.............*.*.............*.*.............*.*........................
........................*...*...........*...*...........*...*...........*...*.......................
.......................*.*.*.*.........*.*.*.*.........*.*.*.*.........*.*.*.*......................
......................*.......*.......*.......*.......*.......*.......*.......*.....................
.....................*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*....................
....................*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...................
...................*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..................
..................*...............................................................*.................
.................*.*.............................................................*.*................
Generations: 33
</pre>
 
=={{header|GFA Basic}}==
 
<syntaxhighlight lang="text">
'
' Elementary One-Dimensional Cellular Automaton
Line 1,558 ⟶ 1,822:
RETURN result%
ENDFUNC
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,605 ⟶ 1,869:
output()
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,637 ⟶ 1,901:
Straight-forward implementation of CA on a cyclic domain, using immutable arrays:
 
<langsyntaxhighlight Haskelllang="haskell">import Data.Array (listArray, (!), bounds, elems)
 
step rule a = listArray (l,r) res
Line 1,645 ⟶ 1,909:
[rule (a!(r-1)) (a!r) (a!l) ]
 
runCA rule = iterate (step rule)</langsyntaxhighlight>
 
The following gives decoding of the CA rule and prepares the initial CA state:
<langsyntaxhighlight Haskelllang="haskell">rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2
 
initial n = listArray (0,n-1) . center . padRight n
where
padRight n lst = take n $ lst ++ repeat 0
center = take n . drop (n `div` 2+1) . cycle</langsyntaxhighlight>
 
Finally the IO stuff:
<langsyntaxhighlight Haskelllang="haskell">displayCA n rule init = mapM_ putStrLn $ take n result
where result = fmap display . elems <$> runCA rule init
display 0 = ' '
display 1 = '*'</langsyntaxhighlight>
 
{{Out}}
Line 1,709 ⟶ 1,973:
The cyclic CA domain is represented by an infinite ''zipper list''. First we provide the datatype, the viewer and constructor:
 
<langsyntaxhighlight Haskelllang="haskell">{-# LANGUAGE DeriveFunctor #-}
 
import Control.Comonad
Line 1,722 ⟶ 1,986:
-- zero cycle length ensures that elements of the empty cycle will never be accessed
fromList lst = let x:::r = Inf.cycle lst
in Cycle (length lst) (last lst) x r</langsyntaxhighlight>
 
In order to run the CA on the domain we make it an instance of <code>Comonad</code> class. Running the CA turns to be just an iterative comonadic ''extension'' of the rule:
 
<langsyntaxhighlight Haskelllang="haskell">instance Comonad Cycle where
extract (Cycle _ _ x _) = x
duplicate x@(Cycle n _ _ _) = fromList $ take n $ iterate shift x
Line 1,733 ⟶ 1,997:
step rule (Cycle _ l x (r:::_)) = rule l x r
 
runCA rule = iterate (=>> step rule)</langsyntaxhighlight>
 
 
Rule definition and I/O routine is the same as in Array-based solution:
 
<langsyntaxhighlight Haskelllang="haskell">rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2
 
initial n lst = fromList $ center $ padRight n lst
Line 1,748 ⟶ 2,012:
where result = fmap display . view <$> runCA rule init
display 0 = ' '
display 1 = '*'</langsyntaxhighlight>
 
See also [[Elementary cellular automaton/Infinite length#Haskell]]
Line 1,756 ⟶ 2,020:
We'll define a state transition mechanism, and then rely on the language for iteration and display:
 
<langsyntaxhighlight Jlang="j"> next=: ((8$2) #: [) {~ 2 #. 1 - [: |: |.~"1 0&_1 0 1@]
' *'{~90 next^:(i.9) 0 0 0 0 0 0 1 0 0 0 0 0
*
Line 1,766 ⟶ 2,030:
* *
* * * *
* * </langsyntaxhighlight>
 
Or, we can view this on a larger scale, graphically:
 
<langsyntaxhighlight Jlang="j"> require'viewmat'
viewmat 90 next^:(i.200) 0=i:200</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
Line 1,857 ⟶ 2,121:
});
}
}</langsyntaxhighlight>
[[File:ca java.png|900px]]
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">const alive = '#';
const dead = '.';
 
Line 1,906 ⟶ 2,170:
}
 
displayCA(90, 57, 31, 28);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,953 ⟶ 2,217:
 
'''Helper functions'''
<langsyntaxhighlight lang="jq"># The ordinal value of the relevant states:
def states:
{"111": 1, "110": 2, "101": 3, "100": 4, "011": 5, "010": 6, "001": 7, "000": 8};
Line 1,979 ⟶ 2,243:
| .[1:] # remove the leading 0
| join("")
end ;</langsyntaxhighlight>
 
'''Main function'''
<langsyntaxhighlight lang="jq"># "rule" can be given as a decimal or string of 0s and 1s:
def automaton(rule; steps):
 
Line 2,006 ⟶ 2,270:
| gsub("0"; ".") # pretty print
| gsub("1"; "#")
</syntaxhighlight>
</lang>
 
 
Line 2,025 ⟶ 2,289:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
struct Automaton
const lines = 10
g₀::Vector{Bool}
const start = ".........#........."
rule::Function
const rules = [90, 30, 14]
Automaton(n::Int) = new(
rule2poss(rule) = [rule & (1 << (i - 1))[c !== 0'#' for ic in 1:8".........#........."],
i -> isone.(digits(n; base = 2, pad = 8))[i])
cells2bools(cells) = [cells[i] == '#' for i in 1:length(cells)]
bools2cells(bset) = prod([bset[i] ? "#" : "." for i in 1:length(bset)])
function transform(bset, ruleposs)
newbset = map(x->ruleposs[x],
[bset[i - 1] * 4 + bset[i] * 2 + bset[i + 1] + 1
for i in 2:length(bset)-1])
vcat(newbset[end], newbset, newbset[1])
end
 
Base.iterate(a::Automaton, g = a.g₀) =
const startset = cells2bools(start)
g, @. a.rule(4*[g[end];g[1:end-1]] + 2*g + [g[2:end];g[1]] + 1)
 
for rul in rules
Base.show(io::IO, a::Automaton) =
println("\nUsing Rule $rul:")
for g in Iterators.take(a, 10)
bset = vcat(startset[end], startset, startset[1]) # wrap ends
println(io, join(c ? '#' : '.' for c ∈ g))
rp = rule2poss(rul)
for _ in 1:lines
println(bools2cells(bset[2:end-1])) # unwrap ends
bset = transform(bset, rp)
end
end
</lang> {{output}} <pre>
Using Rule 90:
.........#.........
........#.#........
.......#...#.......
......#.#.#.#......
.....#.......#.....
....#.#.....#.#....
...#...#...#...#...
..#.#.#.#.#.#.#.#..
.#...............#.
#.#.............#.#
 
for n ∈ [90, 30, 14]
Using Rule 30:
println("rule $n:")
.........#.........
show(Automaton(n))
........###........
println()
.......##..#.......
end</syntaxhighlight> {{output}}
......##.####......
<pre>
.....##..#...#.....
rule 90:
....##.####.###....
...##..#....#..#.......
..##.####.....#.#####........
.##......#...###.....#..
......#.#.####.##..#...###.
.....#.......#.....
....#.#.....#.#....
...#...#...#...#...
..#.#.#.#.#.#.#.#..
.#...............#.
#.#.............#.#
 
rule 30:
.........#.........
........###........
.......##..#.......
......##.####......
.....##..#...#.....
....##.####.###....
...##..#....#..#...
..##.####..######..
.##..#...###.....#.
##.####.##..#...###
 
rule 14:
.........#.........
........##.........
.......##..........
......##...........
.....##............
....##.............
...##..............
..##...............
.##................
##.................
 
Using Rule 14:
.........#.........
........##.........
.......##..........
......##...........
.....##............
....##.............
...##..............
..##...............
.##................
##.................
</pre>
 
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import java.util.BitSet
Line 2,131 ⟶ 2,389:
evolve(state)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,155 ⟶ 2,413:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local CA = {
state = "..............................#..............................",
bstr = { [0]="...", "..#", ".#.", ".##", "#..", "#.#", "##.", "###" },
Line 2,180 ⟶ 2,438:
print(string.format("%-66s%-66s%-66s%-61s", ca[1].state, ca[2].state, ca[3].state, ca[4].state))
for j = 1, 4 do ca[j]:evolve() end
end</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">..............................#.............................. ..............................#.............................. ..............................#.............................. ..............................#..............................
Line 2,250 ⟶ 2,508:
Mathematica provides built-in functions for cellular automata. For example visualizing the first 100 rows of rule 30 on an 8-bit grid with a single initial cell:
 
<langsyntaxhighlight Mathematicalang="mathematica">ArrayPlot[CellularAutomaton[30, {0, 0, 0, 0, 1, 0, 0, 0}, 100]]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function init = cellularAutomaton(rule, init, n)
init(n + 1, :) = 0;
for k = 1 : n
init(k + 1, :) = bitget(rule, 1 + filter2([4 2 1], init(k, :)));
end</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight MATLABlang="matlab">>> char(cellularAutomaton(90, ~(-15:15), 15) * 10 + 32)
ans =
*
Line 2,276 ⟶ 2,534:
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">ElementaryCellularAutomaton = {"state": null, "rules": [], "ruleNum": 0}
 
ElementaryCellularAutomaton.init = function(n, state)
oneD = new ElementaryCellularAutomaton
oneD.state = state.split("")
oneD.ruleNum = n
oneD.rules = oneD.__createRules(n)
return oneD
end function
 
ElementaryCellularAutomaton.__createRules = function(n)
bits = self.__bitString(n)
rules = []
for i in range(7,0)
if bits[7-i] == "*" then rules.push(self.__bitString(i,3))
end for
return rules
end function
 
ElementaryCellularAutomaton.__bitString = function(n, width = 8)
s = ""
while s.len < width
s = char(46 - (n % 2) * 4) + s
n = floor(n / 2)
end while
return s
end function
 
ElementaryCellularAutomaton.evolve = function
length = self.state.len
newState = []
for i in range(0, length - 1)
s = ""
for j in [-1,0,1]
s += self.state[(i + j + length) % length]
end for
if self.rules.indexOf(s) != null then
newState.push "*"
else
newState.push "."
end if
end for
self.state = newState
end function
 
ElementaryCellularAutomaton.getState = function
output = self.state.join("")
return output
end function
 
getAllStates = function(cells)
s = ""
for i in range(0, cells.len - 1)
s += " " + cells[i].getState
end for
return s
end function
 
// Main program
automata = []
startState = "." * 15 + "*" + "." * 15
 
s = " "
for i in [30,60,90]
s += " " * 13 + "Rule " + i + " " * 12
automata.push(ElementaryCellularAutomaton.init(i, startState))
end for
print s
 
for i in range(0, 99)
s = "0" * 2 + str(i)
s = s[-3:]
s += getAllStates(automata)
print s
for j in range(0, automata.len - 1)
automata[j].evolve
end for
end for
 
// display last evolution
s = "100"
s += getAllStates(automata)
print s
</syntaxhighlight>
 
{{out}}
<pre> Rule 30 Rule 60 Rule 90
000 ...............*............... ...............*............... ...............*...............
001 ..............***.............. ...............**.............. ..............*.*..............
002 .............**..*............. ...............*.*............. .............*...*.............
003 ............**.****............ ...............****............ ............*.*.*.*............
004 ...........**..*...*........... ...............*...*........... ...........*.......*...........
005 ..........**.****.***.......... ...............**..**.......... ..........*.*.....*.*..........
006 .........**..*....*..*......... ...............*.*.*.*......... .........*...*...*...*.........
007 ........**.****..******........ ...............********........ ........*.*.*.*.*.*.*.*........
008 .......**..*...***.....*....... ...............*.......*....... .......*...............*.......
009 ......**.****.**..*...***...... ...............**......**...... ......*.*.............*.*......
010 .....**..*....*.****.**..*..... ...............*.*.....*.*..... .....*...*...........*...*.....
011 ....**.****..**.*....*.****.... ...............****....****.... ....*.*.*.*.........*.*.*.*....
012 ...**..*...***..**..**.*...*... ...............*...*...*...*... ...*.......*.......*.......*...
013 ..**.****.**..***.***..**.***.. ...............**..**..**..**.. ..*.*.....*.*.....*.*.....*.*..
014 .**..*....*.***...*..***..*..*. ...............*.*.*.*.*.*.*.*. .*...*...*...*...*...*...*...*.
015 **.****..**.*..*.*****..******* ...............**************** *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
016 ...*...***..****.*....***...... *..............*............... *.............................*
017 ..***.**..***....**..**..*..... **.............**.............. **...........................**
018 .**...*.***..*..**.***.****.... *.*............*.*............. .**.........................**.
019 **.*.**.*..******..*...*...*... ****...........****............ ****.......................****
020 *..*.*..****.....****.***.***.* *...*..........*...*........... ...**.....................**...
021 .***.****...*...**....*...*...* **..**.........**..**.......... ..****...................****..
022 .*...*...*.***.**.*..***.***.** *.*.*.*........*.*.*.*......... .**..**.................**..**.
023 .**.***.**.*...*..****...*...*. ********.......********........ ********...............********
024 **..*...*..**.*****...*.***.*** *.......*......*.......*....... .......**.............**.......
025 ..****.*****..*....*.**.*...*.. **......**.....**......**...... ......****...........****......
026 .**....*....****..**.*..**.***. *.*.....*.*....*.*.....*.*..... .....**..**.........**..**.....
027 **.*..***..**...***..****..*..* ****....****...****....****.... ....********.......********....
028 ...****..***.*.**..***...****** *...*...*...*..*...*...*...*... ...**......**.....**......**...
029 *.**...***...*.*.***..*.**..... **..**..**..**.**..**..**..**.. ..****....****...****....****..
030 *.*.*.**..*.**.*.*..***.*.*...* *.*.*.*.*.*.*.**.*.*.*.*.*.*.*. .**..**..**..**.**..**..**..**.
031 ..*.*.*.***.*..*.****...*.**.** ***************.*************** ***************.***************
032 ***.*.*.*...****.*...*.**.*..*. ...............**.............. ..............*.*..............
033 *...*.*.**.**....**.**.*..****. ...............*.*............. .............*...*.............
034 **.**.*.*..*.*..**..*..****.... ...............****............ ............*.*.*.*............
035 *..*..*.****.****.******...*..* ...............*...*........... ...........*.......*...........
036 .******.*....*....*.....*.***** ...............**..**.......... ..........*.*.....*.*..........
037 .*......**..***..***...**.*.... ...............*.*.*.*......... .........*...*...*...*.........
038 ***....**.***..***..*.**..**... ...............********........ ........*.*.*.*.*.*.*.*........
039 *..*..**..*..***..***.*.***.*.* ...............*.......*....... .......*...............*.......
040 .******.******..***...*.*...*.* ...............**......**...... ......*.*.............*.*......
041 .*......*.....***..*.**.**.**.* ...............*.*.....*.*..... .....*...*...........*...*.....
042 .**....***...**..***.*..*..*..* ...............****....****.... ....*.*.*.*.........*.*.*.*....
043 .*.*..**..*.**.***...********** ...............*...*...*...*... ...*.......*.......*.......*...
044 .*.****.***.*..*..*.**......... ...............**..**..**..**.. ..*.*.....*.*.....*.*.....*.*..
045 **.*....*...*******.*.*........ ...............*.*.*.*.*.*.*.*. .*...*...*...*...*...*...*...*.
046 *..**..***.**.......*.**......* ...............**************** *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
047 .***.***...*.*.....**.*.*....** *..............*............... *.............................*
048 .*...*..*.**.**...**..*.**..**. **.............**.............. **...........................**
049 ***.*****.*..*.*.**.***.*.***.* *.*............*.*............. .**.........................**.
050 ....*.....****.*.*..*...*.*...* ****...........****............ ****.......................****
051 *..***...**....*.*****.**.**.** *...*..........*...*........... ...**.....................**...
052 .***..*.**.*..**.*.....*..*..*. **..**.........**..**.......... ..****...................****..
053 **..***.*..****..**...********* *.*.*.*........*.*.*.*......... .**..**.................**..**.
054 ..***...****...***.*.**........ ********.......********........ ********...............********
055 .**..*.**...*.**...*.*.*....... *.......*......*.......*....... .......**.............**.......
056 **.***.*.*.**.*.*.**.*.**...... **......**.....**......**...... ......****...........****......
057 *..*...*.*.*..*.*.*..*.*.*....* *.*.....*.*....*.*.....*.*..... .....**..**.........**..**.....
058 .****.**.*.****.*.****.*.**..** ****....****...****....****.... ....********.......********....
059 .*....*..*.*....*.*....*.*.***. *...*...*...*..*...*...*...*... ...**......**.....**......**...
060 ***..*****.**..**.**..**.*.*..* **..**..**..**.**..**..**..**.. ..****....****...****....****..
061 ...***.....*.***..*.***..*.**** *.*.*.*.*.*.*.**.*.*.*.*.*.*.*. .**..**..**..**.**..**..**..**.
062 *.**..*...**.*..***.*..***.*... ***************.*************** ***************.***************
063 *.*.****.**..****...****...**.* ...............**.............. ..............*.*..............
064 ..*.*....*.***...*.**...*.**..* ...............*.*............. .............*...*.............
065 ***.**..**.*..*.**.*.*.**.*.*** ...............****............ ............*.*.*.*............
066 ....*.***..****.*..*.*.*..*.*.. ...............*...*........... ...........*.......*...........
067 ...**.*..***....****.*.****.**. ...............**..**.......... ..........*.*.....*.*..........
068 ..**..****..*..**....*.*....*.* ...............*.*.*.*......... .........*...*...*...*.........
069 ***.***...******.*..**.**..**.* ...............********........ ........*.*.*.*.*.*.*.*........
070 ....*..*.**......****..*.***..* ...............*.......*....... .......*...............*.......
071 *..*****.*.*....**...***.*..*** ...............**......**...... ......*.*.............*.*......
072 .***.....*.**..**.*.**...****.. ...............*.*.....*.*..... .....*...*...........*...*.....
073 **..*...**.*.***..*.*.*.**...*. ...............****....****.... ....*.*.*.*.........*.*.*.*....
074 *.****.**..*.*..***.*.*.*.*.**. ...............*...*...*...*... ...*.......*.......*.......*...
075 *.*....*.***.****...*.*.*.*.*.. ...............**..**..**..**.. ..*.*.....*.*.....*.*.....*.*..
076 *.**..**.*...*...*.**.*.*.*.*** ...............*.*.*.*.*.*.*.*. .*...*...*...*...*...*...*...*.
077 ..*.***..**.***.**.*..*.*.*.*.. ...............**************** *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
078 .**.*..***..*...*..****.*.*.**. *..............*............... *.............................*
079 **..****..****.*****....*.*.*.* **.............**.............. **...........................**
080 ..***...***....*....*..**.*.*.* *.*............*.*............. .**.........................**.
081 ***..*.**..*..***..*****..*.*.* ****...........****............ ****.......................****
082 ...***.*.******..***....***.*.* *...*..........*...*........... ...**.....................**...
083 *.**...*.*.....***..*..**...*.* **..**.........**..**.......... ..****...................****..
084 ..*.*.**.**...**..******.*.**.* *.*.*.*........*.*.*.*......... .**..**.................**..**.
085 ***.*.*..*.*.**.***......*.*..* ********.......********........ ********...............********
086 ....*.****.*.*..*..*....**.**** *.......*......*.......*....... .......**.............**.......
087 *..**.*....*.********..**..*... **......**.....**......**...... ......****...........****......
088 ****..**..**.*.......***.****.* *.*.....*.*....*.*.....*.*..... .....**..**.........**..**.....
089 ....***.***..**.....**...*....* ****....****...****....****.... ....********.......********....
090 *..**...*..***.*...**.*.***..** *...*...*...*..*...*...*...*... ...**......**.....**......**...
091 .***.*.*****...**.**..*.*..***. **..**..**..**.**..**..**..**.. ..****....****...****....****..
092 **...*.*....*.**..*.***.****..* *.*.*.*.*.*.*.**.*.*.*.*.*.*.*. .**..**..**..**.**..**..**..**.
093 ..*.**.**..**.*.***.*...*...*** ***************.*************** ***************.***************
094 ***.*..*.***..*.*...**.***.**.. ...............**.............. ..............*.*..............
095 *...****.*..***.**.**..*...*.** ...............*.*............. .............*...*.............
096 .*.**....****...*..*.****.**.*. ...............****............ ............*.*.*.*............
097 **.*.*..**...*.*****.*....*..** ...............*...*........... ...........*.......*...........
098 ...*.****.*.**.*.....**..*****. ...............**..**.......... ..........*.*.....*.*..........
099 ..**.*....*.*..**...**.***....* ...............*.*.*.*......... .........*...*...*...*.........
100 ***..**..**.****.*.**..*..*..** ...............********........ ........*.*.*.*.*.*.*.*........
</pre>
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import bitops
 
const
Line 2,333 ⟶ 2,780:
for _ in 1..Lines:
show(state)
evolve(state)</langsyntaxhighlight>
 
{{out}}
Line 2,355 ⟶ 2,802:
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">clear all
E=200;
idx=round(E/2);
Line 2,370 ⟶ 2,817:
endfor
imagesc(reshape(z,E,E)'); % Medland map
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,409 ⟶ 2,856:
for (1..40) {
print "|$a|\n"; $a->next;
}</langsyntaxhighlight>
{{out}}
<pre>| # |
Line 2,454 ⟶ 2,901:
=={{header|Phix}}==
String-based solution
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">".........#........."</span><span style="color: #0000FF;">,</span>
Line 2,473 ⟶ 2,920:
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Output matches that of D and Python:wrap for rule = 90, 30, 122 (if you edit/run 3 times)
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de dictionary (N)
(extract
'((A B)
Line 2,504 ⟶ 2,951:
(cellular
".........#........."
90 )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,520 ⟶ 2,967:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">play :- initial(I), do_auto(50, I).
 
initial([0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]).
Line 2,550 ⟶ 2,997:
 
writ(0) :- write('.').
writ(1) :- write(1).</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,557 ⟶ 3,004:
:''You can deal with the limit conditions (what happens on the borders of the space) in any way you please.''
 
<langsyntaxhighlight lang="python">def eca(cells, rule):
lencells = len(cells)
c = "0" + cells + "0" # Zero pad the ends
Line 2,577 ⟶ 3,024:
i = data[0]
cells = data[1:]
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))</langsyntaxhighlight>
 
{{out}}
Line 2,635 ⟶ 3,082:
===Python: wrap===
The ends of the cells wrap-around.
<langsyntaxhighlight lang="python">def eca_wrap(cells, rule):
lencells = len(cells)
rulebits = '{0:08b}'.format(rule)
Line 2,652 ⟶ 3,099:
cells = data[1:]
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,712 ⟶ 3,159:
 
Pad and extend with inverse of end cells on each iteration.
<langsyntaxhighlight lang="python">def _notcell(c):
return '0' if c == '1' else '1'
 
Line 2,734 ⟶ 3,181:
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', '#')))</langsyntaxhighlight>
 
{{out}}
Line 2,766 ⟶ 3,213:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> ( the Cellular Automaton is on the stack as 3 items, the )
( Rule (R), the Size of the space (S) and the Current )
( state (C). make-ca sets this up from a string indicating )
Line 2,819 ⟶ 3,266:
say "Rule 30, 50 generations:" cr cr
$ ".........#........." 30 50 generations</langsyntaxhighlight>
 
{{out}}
Line 2,884 ⟶ 3,331:
unmodified for [[Elementary cellular automaton/Infinite length]].
 
<langsyntaxhighlight lang="racket">#lang racket
(require racket/fixnum)
(provide usable-bits/fixnum usable-bits/fixnum-1 CA-next-generation
Line 2,969 ⟶ 3,416:
(show-automaton v #:step step #:sig-bits 19)
(newline)
(ng/122/19-bits v o)))</langsyntaxhighlight>
 
{{out}}
Line 3,021 ⟶ 3,468:
Using the <tt>Automaton</tt> class defined at [[One-dimensional_cellular_automata#Raku]]:
 
<syntaxhighlight lang="raku" perl6line>class Automaton {
has $.rule;
has @.cells;
Line 3,045 ⟶ 3,492:
:cells(flat @padding, 1, @padding);
 
say $a++ for ^10;</langsyntaxhighlight>
 
{{out}}
Line 3,062 ⟶ 3,509:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class ElemCellAutomat
include Enumerable
Line 3,083 ⟶ 3,530:
 
eca = ElemCellAutomat.new('1'.center(39, "0"), 18, true)
eca.take(30).each{|line| puts line.tr("01", ".#")}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,120 ⟶ 3,567:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
<lang Rust>
fn main() {
struct ElementaryCA {
Line 3,163 ⟶ 3,610:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,203 ⟶ 3,650:
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight Scalalang="scala">import java.awt._
import java.awt.event.ActionEvent
 
Line 3,275 ⟶ 3,722:
})
 
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">; uses SRFI-1 library http://srfi.schemers.org/srfi-1/srfi-1.html
 
(define (evolve ls r)
Line 3,306 ⟶ 3,753:
n))
 
(automaton '(0 1 0 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 1) 30 20)</langsyntaxhighlight>
{{out}}
 
Line 3,334 ⟶ 3,781:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class Automaton(rule, cells) {
 
method init {
Line 3,366 ⟶ 3,813:
print "|#{auto}|\n"
auto.next
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,383 ⟶ 3,830:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create ElementaryAutomaton {
Line 3,422 ⟶ 3,869:
puts [string map "0 . 1 #" [join $s ""]]
}
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts "Rule 90 (with default state):"
ElementaryAutomaton create rule90 90
rule90 run 20
puts "\nRule 122:"
[ElementaryAutomaton new 122] run 25 "..........#......…."</langsyntaxhighlight>
{{out}}
<pre>
Line 3,487 ⟶ 3,934:
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
<langsyntaxhighlight lang="bash">function print_cells {
local chars=(. '#') cell
for cell; do
Line 3,523 ⟶ 3,970:
 
automaton "$@"
</syntaxhighlight>
</lang>
{{Out}}
<pre> 0: ................#...............
Line 3,546 ⟶ 3,993:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv
 
var SIZE = 32
Line 3,577 ⟶ 4,024:
show.call(state)
evolve.call(state)
}</langsyntaxhighlight>
 
{{out}}
Line 3,598 ⟶ 4,045:
* * * * * * * *
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">def Width = 32, Rule = 90; \ = %0101_1010
int Gen, Old, New, Shift, Triad;
[New:= 1<<(Width/2);
for Gen:= 0 to 15 do
[Old:= New;
repeat ChOut(0, if New&1 then ^* else ^ );
New:= New>>1;
until New = 0;
CrLf(0);
New:= 0; Shift:= 1;
repeat Triad:= Old & %111;
if Rule & 1<<Triad then
New:= New + 1<<Shift;
Old:= Old>>1;
Shift:= Shift+1;
until Shift >= Width;
];
]</syntaxhighlight>
{{out}}
<pre>
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn rule(n){ n=n.toString(2); "00000000"[n.len() - 8,*] + n }
fcn applyRule(rule,cells){
cells=String(cells[-1],cells,cells[0]); // wrap cell ends
(cells.len() - 2).pump(String,'wrap(n){ rule[7 - cells[n,3].toInt(2)] })
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">cells:="0000000000000001000000000000000"; r90:=rule(90); map:=" *";
r90.println(" rule 90");
do(20){ cells.apply(map.get).println(); cells=applyRule(r90,cells); }</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits