RosettaCodeData/Task/One-dimensional-cellular-automata/Ruby/one-dimensional-cellular-automata.rb

14 lines
251 B
Ruby
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
def evolve(ary)
2013-10-27 22:24:23 +00:00
([0]+ary+[0]).each_cons(3).map{|a,b,c| a+b+c == 2 ? 1 : 0}
2013-04-10 23:57:08 -07:00
end
def printit(ary)
2013-10-27 22:24:23 +00:00
puts ary.join.tr("01",".#")
2013-04-10 23:57:08 -07:00
end
ary = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]
printit ary
2013-10-27 22:24:23 +00:00
until ary == (new = evolve(ary))
printit ary = new
2013-04-10 23:57:08 -07:00
end