Elementary cellular automaton: Difference between revisions
m
→{{header|Wren}}: Minor tidy
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}}
<
V LINES = SIZE I/ 2
V RULE = 90
Line 48 ⟶ 51:
L 1..LINES
show(state)
evolve(&state)</
{{out}}
Line 72 ⟶ 75:
=={{header|Action!}}==
<
DEFINE MAX_COL="319"
DEFINE MAX_ROW="191"
Line 158 ⟶ 161:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{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}}
<
procedure Elementary_Cellular_Automaton is
Line 230 ⟶ 233:
Generations => 25);
end Elementary_Cellular_Automaton;
</syntaxhighlight>
{{Output}}
<pre>Rule 90 :
Line 297 ⟶ 300:
=={{header|ALGOL 68}}==
<
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</
{{out}}
<pre>
Line 382 ⟶ 385:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
rule := 90
output := "Rule: " rule
Line 428 ⟶ 431:
result .= val = 1 ? "#" : "."
return result
}</
{{Output}}
<pre>Rule: 90
Line 444 ⟶ 447:
=={{header|C}}==
64 cells, edges are cyclic.
<
#include <limits.h>
Line 474 ⟶ 477:
return 0;
}</
{{out}}
<pre>
Line 491 ⟶ 494:
=={{header|C sharp|C#}}==
<
using System;
using System.Collections;
Line 584 ⟶ 587:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 644 ⟶ 647:
=={{header|C++}}==
<
#include <stdio.h>
Line 674 ⟶ 677:
}
return 0;
}</
{{out}}
<pre> # |
Line 688 ⟶ 691:
=={{header|Ceylon}}==
<
shared Byte number;
Line 773 ⟶ 776:
automaton.evolve();
}
}</
{{out}}
<pre>Rule #90
Line 791 ⟶ 794:
=={{header|Common Lisp}}==
<
(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)</
{{Out}}
Line 827 ⟶ 830:
=={{header|D}}==
{{trans|Python}}
<
enum mod = (in int n, in int m) pure nothrow @safe @nogc => ((n % m) + m) % m;
Line 862 ⟶ 865:
eca.popFront;
}
}</
{{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)]
<
(lib 'types) ;; int32 vectors
(lib 'plot)
Line 979 ⟶ 1,064:
→ #( #( 0 0 0) #( 0 -5052980 0) #( 0 -5052980 -5052980))
</syntaxhighlight>
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
{{trans|Ruby}}
<
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)</
{{out}}
Line 1,044 ⟶ 1,129:
=={{header|F_Sharp|F#}}==
===The Function===
<
// 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>
===The Task===
<
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>
{{out}}
<pre>
Line 1,137 ⟶ 1,222:
@ @
</pre>
<
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>
{{out}}
<pre>
Line 1,225 ⟶ 1,310:
=={{header|Factor}}==
<
math.combinatorics sequences sequences.extras ;
Line 1,242 ⟶ 1,327:
[ dup show-state next-state ] times 2drop ;
90 show-automaton</
{{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}}==
<
#define NCELLS 400
#define border 16
Line 1,333 ⟶ 1,501:
end if
key = ucase(inkey)
loop until ucase(key) = "Q"</
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Elementary_cellular_automaton}}
'''Solution'''
[[File:Fōrmulæ - Elementary cellular automaton 01.png]]
'''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">
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>
{{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>
=={{header|Go}}==
<
import (
Line 1,605 ⟶ 1,869:
output()
}
}</
{{out}}
<pre>
Line 1,637 ⟶ 1,901:
Straight-forward implementation of CA on a cyclic domain, using immutable arrays:
<
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)</
The following gives decoding of the CA rule and prepares the initial CA state:
<
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</
Finally the IO stuff:
<
where result = fmap display . elems <$> runCA rule init
display 0 = ' '
display 1 = '*'</
{{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:
<
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</
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:
<
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)</
Rule definition and I/O routine is the same as in Array-based solution:
<
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 = '*'</
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:
<
' *'{~90 next^:(i.9) 0 0 0 0 0 0 1 0 0 0 0 0
*
Line 1,766 ⟶ 2,030:
* *
* * * *
* * </
Or, we can view this on a larger scale, graphically:
<
viewmat 90 next^:(i.200) 0=i:200</
=={{header|Java}}==
{{works with|Java|8}}
<
import java.awt.event.ActionEvent;
import javax.swing.*;
Line 1,857 ⟶ 2,121:
});
}
}</
[[File:ca java.png|900px]]
=={{header|JavaScript}}==
<
const dead = '.';
Line 1,906 ⟶ 2,170:
}
displayCA(90, 57, 31, 28);</
{{out}}
<pre>
Line 1,953 ⟶ 2,217:
'''Helper functions'''
<
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 ;</
'''Main function'''
<
def automaton(rule; steps):
Line 2,006 ⟶ 2,270:
| gsub("0"; ".") # pretty print
| gsub("1"; "#")
</syntaxhighlight>
Line 2,025 ⟶ 2,289:
=={{header|Julia}}==
<
struct Automaton
g₀::Vector{Bool}
rule::Function
Automaton(n::Int) = new(
i -> isone.(digits(n; base = 2, pad = 8))[i])
end
Base.iterate(a::Automaton, g = a.g₀) =
g, @. a.rule(4*[g[end];g[1:end-1]] + 2*g + [g[2:end];g[1]] + 1)
Base.show(io::IO, a::Automaton) =
for g in Iterators.take(a, 10)
println(io, join(c ? '#' : '.' for c ∈ g))
end
for n ∈ [90, 30, 14]
println("rule $n:")
show(Automaton(n))
println()
end</syntaxhighlight> {{output}}
<pre>
rule 90:
.....#.......#.....
....#.#.....#.#....
...#...#...#...#...
..#.#.#.#.#.#.#.#..
.#...............#.
#.#.............#.#
rule 30:
.........#.........
........###........
.......##..#.......
......##.####......
.....##..#...#.....
....##.####.###....
...##..#....#..#...
..##.####..######..
.##..#...###.....#.
##.####.##..#...###
rule 14:
.........#.........
........##.........
.......##..........
......##...........
.....##............
....##.............
...##..............
..##...............
.##................
##.................
</pre>
=={{header|Kotlin}}==
{{trans|C++}}
<
import java.util.BitSet
Line 2,131 ⟶ 2,389:
evolve(state)
}
}</
{{out}}
Line 2,155 ⟶ 2,413:
=={{header|Lua}}==
<
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</
{{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:
<
=={{header|MATLAB}}==
<
init(n + 1, :) = 0;
for k = 1 : n
init(k + 1, :) = bitget(rule, 1 + filter2([4 2 1], init(k, :)));
end</
{{out}}
<
ans =
*
Line 2,276 ⟶ 2,534:
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *</
=={{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}}
<
const
Line 2,333 ⟶ 2,780:
for _ in 1..Lines:
show(state)
evolve(state)</
{{out}}
Line 2,355 ⟶ 2,802:
=={{header|Octave}}==
<
E=200;
idx=round(E/2);
Line 2,370 ⟶ 2,817:
endfor
imagesc(reshape(z,E,E)'); % Medland map
</syntaxhighlight>
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
Line 2,409 ⟶ 2,856:
for (1..40) {
print "|$a|\n"; $a->next;
}</
{{out}}
<pre>| # |
Line 2,454 ⟶ 2,901:
=={{header|Phix}}==
String-based solution
<!--<
<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>
<!--</
Output matches that of D and Python:wrap for rule = 90, 30, 122 (if you edit/run 3 times)
=={{header|PicoLisp}}==
<
(extract
'((A B)
Line 2,504 ⟶ 2,951:
(cellular
".........#........."
90 )</
{{out}}
<pre>
Line 2,520 ⟶ 2,967:
=={{header|Prolog}}==
<
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).</
=={{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.''
<
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', '#')))</
{{out}}
Line 2,635 ⟶ 3,082:
===Python: wrap===
The ends of the cells wrap-around.
<
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>
{{out}}
Line 2,712 ⟶ 3,159:
Pad and extend with inverse of end cells on each iteration.
<
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', '#')))</
{{out}}
Line 2,766 ⟶ 3,213:
=={{header|Quackery}}==
<
( 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</
{{out}}
Line 2,884 ⟶ 3,331:
unmodified for [[Elementary cellular automaton/Infinite length]].
<
(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)))</
{{out}}
Line 3,021 ⟶ 3,468:
Using the <tt>Automaton</tt> class defined at [[One-dimensional_cellular_automata#Raku]]:
<syntaxhighlight lang="raku"
has $.rule;
has @.cells;
Line 3,045 ⟶ 3,492:
:cells(flat @padding, 1, @padding);
say $a++ for ^10;</
{{out}}
Line 3,062 ⟶ 3,509:
=={{header|Ruby}}==
<
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", ".#")}</
{{out}}
<pre>
Line 3,120 ⟶ 3,567:
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn main() {
struct ElementaryCA {
Line 3,163 ⟶ 3,610:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,203 ⟶ 3,650:
=={{header|Scala}}==
===Java Swing Interoperability===
<
import java.awt.event.ActionEvent
Line 3,275 ⟶ 3,722:
})
}</
=={{header|Scheme}}==
<
(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)</
{{out}}
Line 3,334 ⟶ 3,781:
=={{header|Sidef}}==
{{trans|Perl}}
<
method init {
Line 3,366 ⟶ 3,813:
print "|#{auto}|\n"
auto.next
}</
{{out}}
<pre>
Line 3,383 ⟶ 3,830:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
oo::class create ElementaryAutomaton {
Line 3,422 ⟶ 3,869:
puts [string map "0 . 1 #" [join $s ""]]
}
}</
Demonstrating:
<
ElementaryAutomaton create rule90 90
rule90 run 20
puts "\nRule 122:"
[ElementaryAutomaton new 122] run 25 "..........#......…."</
{{out}}
<pre>
Line 3,487 ⟶ 3,934:
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
<
local chars=(. '#') cell
for cell; do
Line 3,523 ⟶ 3,970:
automaton "$@"
</syntaxhighlight>
{{Out}}
<pre> 0: ................#...............
Line 3,546 ⟶ 3,993:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var SIZE = 32
Line 3,577 ⟶ 4,024:
show.call(state)
evolve.call(state)
}</
{{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}}==
<
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)] })
}</
<
r90.println(" rule 90");
do(20){ cells.apply(map.get).println(); cells=applyRule(r90,cells); }</
{{out}}
<pre>
|