-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementary_cellular_automaton_generalized.sf
More file actions
66 lines (47 loc) · 1.71 KB
/
elementary_cellular_automaton_generalized.sf
File metadata and controls
66 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 16 October 2019
# https://github.com/trizen
# Generalization of the elementary cellular automaton, by using `n` color-states and looking at `k` neighbors left-to-right.
# For example, a value of `n = 3` and `k = 2` uses three different color-states and looks at 2 neighbors to the left and 2 neighbors to the right.
# See also:
# https://en.wikipedia.org/wiki/Cellular_automaton
# https://en.wikipedia.org/wiki/Elementary_cellular_automaton
# https://rosettacode.org/wiki/Elementary_cellular_automaton
# YouTube lectures:
# https://www.youtube.com/watch?v=S3tYzCPuVsA
# https://www.youtube.com/watch?v=pGGIE5uhPRQ
func automaton(n, k, callback, rule=120, iter=50, cells=[1]) {
var states = @(^n).variations_with_repetition(2*k + 1)
var digits = rule.digits(n)
var lookup = Array()
for i,s in (states.kv) {
lookup[s.digits2num(n)] = digits[i]\\0
}
var padding = ((iter-cells.len)/2 -> of(0))
cells = [padding..., cells..., padding...]
var len = cells.len
var neighbors_range = @(-k .. k)
(iter/2).times {
callback(cells)
cells[] = lookup[
cells.range.map { |i|
neighbors_range.map {|j| cells[(i+j) % len] }.digits2num(n)
}...
]
}
return cells
}
var chars = [' ', '*', '.', '#']
say "\n=> 2x1 Automaton"
automaton(2, 1, rule: 90, callback: {|row|
say row.map { chars[_] }.join
})
say "\n=> 3x1 Automaton"
automaton(3, 1, rule: 843693805713, callback: {|row|
say row.map { chars[_] }.join
})
say "\n=> 3x2 Automaton"
automaton(3, 2, rule: 590193390821886729275563552433397050190, iter: 80, callback: {|row|
say row.map { chars[_] }.join
})