Elementary cellular automaton/Infinite length: Difference between revisions
Content added Content deleted
m (julia example) |
|||
Line 442: | Line 442: | ||
Looks like a [[Sierpinski_triangle|Sierpinski triangle]] |
Looks like a [[Sierpinski_triangle|Sierpinski triangle]] |
||
=={{header|Julia}}== |
|||
{{trans|Python}} |
|||
<lang julia>notcell(cell) = (cell == '1') ? '0' : '1' |
|||
function ecainfinite(cells, rule, n) |
|||
celllength = length(cells) |
|||
rulebits = string(rule, base = 2, pad = 8) |
|||
neighbors2next = Dict(string(n -1, base=2, pad=3) => rulebits[n] for n in 1:8) |
|||
ret = String[] |
|||
for i in 1:n |
|||
push!(ret, cells) |
|||
cells = notcell(cells[1])^2 * cells * notcell(cells[end])^2 # Extend/pad ends |
|||
cells = join([neighbors2next[cells[i:i+2]] for i in 1:length(cells)-2], "") |
|||
end |
|||
ret |
|||
end |
|||
function testinfcells(lines::Integer) |
|||
for rule in [90, 30] |
|||
println("\nRule: $rule") |
|||
s = ecainfinite("1", rule, lines) |
|||
for i in 1:lines |
|||
println("$i: ", " "^(lines - i), replace(replace(s[i], "0" => "."), "1" => "#")) |
|||
end |
|||
end |
|||
end |
|||
testinfcells(25) |
|||
</lang>{{out}} |
|||
<pre> |
|||
Rule: 90 |
|||
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: #...............#...............#...............# |
|||
Rule: 30 |
|||
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: ###..##..#...##..#...###......#....##.###.#.##..# |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |