Elementary cellular automaton/Infinite length: Difference between revisions
Elementary cellular automaton/Infinite length (view source)
Revision as of 00:03, 10 October 2021
, 2 years ago→{{header|C++}}
Alextretyak (talk | contribs) |
|||
Line 14:
More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.
=={{header|11l}}==
{{trans|Nim}}
<lang 11l>F step(cells, rule)
V result = ‘’
L(i) 0 .< cells.len - 2
V bin = 0
V b = 2
L(n) i .< i + 3
bin += Int(cells[n] == ‘*’) << b
b >>= 1
V a = I (rule [&] (1 << bin)) != 0 {‘*’} E ‘.’
result ‘’= a
R result
F addNoCells(&cells)
V left = I cells[0] == ‘*’ {‘.’} E ‘*’
V right = I cells.last == ‘*’ {‘.’} E ‘*’
cells = left‘’cells‘’right
cells = left‘’cells‘’right
F evolve(limit, rule)
print(‘Rule #’rule)
V cells = ‘*’
L 0 .< limit
addNoCells(&cells)
V width = 40 + (cells.len >> 1)
print(cells.rjust(width))
cells = step(cells, rule)
evolve(35, 90)</lang>
{{out}}
<pre>
Rule #90
..*..
..*.*..
..*...*..
..*.*.*.*..
..*.......*..
..*.*.....*.*..
..*...*...*...*..
..*.*.*.*.*.*.*.*..
..*...............*..
..*.*.............*.*..
..*...*...........*...*..
..*.*.*.*.........*.*.*.*..
..*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................*..
..*.*.............................*.*..
..*...*...........................*...*..
..*.*.*.*.........................*.*.*.*..
..*.......*.......................*.......*..
..*.*.....*.*.....................*.*.....*.*..
..*...*...*...*...................*...*...*...*..
..*.*.*.*.*.*.*.*.................*.*.*.*.*.*.*.*..
..*...............*...............*...............*..
..*.*.............*.*.............*.*.............*.*..
..*...*...........*...*...........*...*...........*...*..
..*.*.*.*.........*.*.*.*.........*.*.*.*.........*.*.*.*..
..*.......*.......*.......*.......*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................................................*..
..*.*.............................................................*.*..
..*...*...........................................................*...*..
</pre>
=={{header|C++}}==
|