Elementary cellular automaton: Difference between revisions

m
imported>Maxima enthusiast
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 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 2,208 ⟶ 2,290:
=={{header|Julia}}==
<syntaxhighlight 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
</syntaxhighlight> {{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>
 
Line 2,460 ⟶ 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,728 ⟶ 3,993:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
 
var SIZE = 32
9,476

edits