3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
6 # Task: One-dimensional_cellular_automata
7 # SEE: <http://rosettacode.org/wiki/One-dimensional_cellular_automata>
8 module one_dimensional_cellular_automata
10 fun evolve
(cells
: SequenceRead[Char]): SequenceRead[Char]
12 var res
= new Array[Char]
13 for i
in [0..cells
.length
[ do
14 var n
= neighboors
(i
, cells
)
15 if cells
[i
] == '#' then
16 res
.add
(b_to_char
(n
== 1))
18 res
.add
(b_to_char
(n
== 2))
24 fun b_to_char
(b
: Bool): Char do if b
then return '#' else return '_'
26 fun neighboors
(i
: Int, cells
: SequenceRead[Char]): Int
29 if i
> 0 and cells
[i-1
] == '#' then res
+= 1
30 if i
< cells
.length
- 1 and cells
[i
+1] == '#' then res
+= 1
34 var ary
= "_###_##_#_#_#_#__#__".chars
38 if ary
== nxt
then break