One-dimensional cellular automata: Difference between revisions
Content added Content deleted
(→Insitux: implementation) |
|||
Line 3,031: | Line 3,031: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Simple julia version implemented as an iterable struct |
|||
This solution creates an automaton with with either empty or periodic bounds. The empty bounds case, is typical of many of the solutions here. The periodic bounds case is a typical physics approach where, in effect, the beginning and end of the list touch each other to form a circular rather than linear array. In practice, the effects of boundary conditions are subtle for long arrays. |
|||
<syntaxhighlight lang="julia"> |
<syntaxhighlight lang="julia"> |
||
struct Automaton g₀::BitVector end |
|||
function next_gen(a::BitArray{1}, isperiodic=false) |
|||
b = copy(a) |
|||
if isperiodic |
|||
ncnt = prepend!(a[1:end-1], [a[end]]) + append!(a[2:end], [a[1]]) |
|||
else |
|||
ncnt = prepend!(a[1:end-1], [false]) + append!(a[2:end], [false]) |
|||
end |
|||
b[ncnt .== 0] = false |
|||
b[ncnt .== 2] = ~b[ncnt .== 2] |
|||
return b |
|||
end |
|||
Base.iterate(a::Automaton, g = a.g₀) = |
|||
function show_gen(a::BitArray{1}) |
|||
g, @. (g + [g[2:end]; false] + [false; g[1:end-1]]) == 2 |
|||
s = "\u25ba"*s*"\u25c4" |
|||
end |
|||
Base.show(io::IO, a::Automaton) = for g in Iterators.take(a, 10) |
|||
hi = 70 |
|||
println(io, join(alive ? '#' : '_' for alive ∈ g)) end |
|||
a = bitrand(hi) |
|||
b = falses(hi) |
|||
println("A 1D Cellular Atomaton with ", hi, " cells and empty bounds.") |
|||
while any(a) && any(a .!= b) |
|||
println(" ", show_gen(a)) |
|||
b = copy(a) |
|||
a = next_gen(a) |
|||
end |
|||
a = bitrand(hi) |
|||
b = falses(hi) |
|||
println() |
|||
println("A 1D Cellular Atomaton with ", hi, " cells and periodic bounds.") |
|||
while any(a) && any(a .!= b) |
|||
println(" ", show_gen(a)) |
|||
b = copy(a) |
|||
a = next_gen(a, true) |
|||
end |
|||
</syntaxhighlight> |
|||
Automaton([c == '#' for c ∈ "_###_##_#_#_#_#__#__"])</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
_###_##_#_#_#_#__#__ |
|||
A 1D Cellular Atomaton with 70 cells and empty bounds. |
|||
_#_#####_#_#_#______ |
|||
► ███ ██ █ ██ ███ █ ███ ██ █ █ ██████ █ ██ █ █ █ █ ██ ███ ◄ |
|||
__##___##_#_#_______ |
|||
► █ █ ██ ███ █ ██ █ █ ██ █ █ ██ ███ █ █ ███ █ █ ◄ |
|||
__##___###_#________ |
|||
► █ ██ █ █ ███ █ ██ ██ █ ██ █ █ █ █ ◄ |
|||
__##___#_##_________ |
|||
► ██ █ █ █ ██ ██ ████ █ ◄ |
|||
__##____###_________ |
|||
► ██ █ ██ ██ █ █ ◄ |
|||
__##____#_#_________ |
|||
► ██ ██ ██ ◄ |
|||
__##_____#__________ |
|||
__##________________ |
|||
A 1D Cellular Atomaton with 70 cells and periodic bounds. |
|||
__##________________ |
|||
►████ ██ █ █ █ ██ ██ █ █ ████ █ ███ ███ ██ ██ ██ ◄ |
|||
►█ █ ███ █ ██ ███ █ █ █ █ █ █ ████ ██████◄ |
|||
►█ █ █ ██ █ ██ █ ██ █ █ ◄ |
|||
► █ ██ ███ ██ ◄ |
|||
► ██ █ █ ██ ◄ |
|||
► ██ █ ██ ◄ |
|||
► ██ ██ ◄ |
|||
</pre> |
</pre> |
||