Elementary cellular automaton: Difference between revisions
m
→{{header|Wren}}: Minor tidy
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Minor tidy) |
||
(9 intermediate revisions by 7 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 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}}==
Line 1,420 ⟶ 1,505:
=={{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}}==
Line 1,512 ⟶ 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}}==
Line 2,109 ⟶ 2,290:
=={{header|Julia}}==
<syntaxhighlight lang="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>
Line 2,361 ⟶ 2,536:
* * * * * * * * * * * * * * * *</syntaxhighlight>
=={{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}}
Line 3,629 ⟶ 3,993:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var SIZE = 32
Line 3,681 ⟶ 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>
|