Elementary cellular automaton
1
class Automaton(rule, cells) {
2
3
method init {
4
rule = sprintf("%08b", rule).chars.map{.to_i}.reverse
5
}
6
7
method next {
8
var previous = cells.map{_}
9
var len = previous.len
10
cells[] = rule[
11
previous.range.map { |i|
12
4*previous[i-1 % len] +
13
2*previous[i] +
14
previous[i+1 % len]
15
}
16
]
17
}
18
19
method to_s {
20
cells.map { _ ? '#' : ' ' }.join
21
}
22
}
23
24
var size = 20
25
var arr = size.of(0)
26
arr[size/2] = 1
27
28
var auto = Automaton(90, arr)
29
30
(size/2).times {
31
print "|#{auto}|\n"
32
auto.next
33
}
Copied!

Output:

1
| # |
2
| # # |
3
| # # |
4
| # # # # |
5
| # # |
6
| # # # # |
7
| # # # # |
8
| # # # # # # # # |
9
| # # |
10
| # # # #|
Copied!
Last modified 1yr ago
Copy link
Contents
Output: