Jump to content

One-dimensional cellular automata: Difference between revisions

add Ruby
(→‎{{header|J}}: Removed two spurious constructions.)
(add Ruby)
Line 893:
a |= endvals
a = ((a&((a<<1) | (a>>1))) ^ ((a<<1)&(a>>1))) & endmask</lang>
 
=={{header|Ruby}}==
<lang ruby>def evolve(ary)
new = Array.new(ary.length)
new[0] = (ary[0] == 1 and ary[1] == 1) ? 1 : 0
(1..new.length - 2).each {|i| new[i] = ary[i-1] + ary[i] + ary[i+1] == 2 ? 1 : 0}
new[-1] = (ary[-2] == 1 and ary[-1] == 1) ? 1 : 0
new
end
 
def printit(ary)
s = ary.join("")
s.gsub!(/1/,"#")
s.gsub!(/0/,".")
puts s
end
 
ary = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]
printit ary
while ary != new=evolve(ary)
printit new
ary = new
end</lang>
<pre>.###.##.#.#.#.#..#..
.#.#####.#.#.#......
..##...##.#.#.......
..##...###.#........
..##...#.##.........
..##....###.........
..##....#.#.........
..##.....#..........
..##................</pre>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.