Elementary cellular automaton: Difference between revisions

(→‎{{header|Perl 6}}: some speed tweaks)
Line 12:
 
This task is basically a generalization of [[one-dimensional cellular automaton]].
 
=={{header|C}}==
64 cells, edges are cyclic.
<lang c>#include <stdio.h>
 
typedef unsigned long long ull;
#define B(x) (1ULL << (x))
 
void evolve(ull state, int rule)
{
int i;
ull st;
 
printf("Rule %d:\n", rule);
do {
st = state;
for (i = 64; i--; ) putchar(st & B(i) ? '#' : '.');
putchar('\n');
 
for (state = i = 0; i < 64; i++)
if (rule & B(7 & (st>>(i-1) | st<<(65-i))))
state |= B(i);
} while (st != state);
}
 
int main(int argc, char **argv)
{
evolve(B(31), 90);
evolve(B(16)|B(48), 30); // well, enjoy the fireworks
 
return 0;
}</lang>
{{out}}
<pre>
Rule 90:
................................#...............................
...............................#.#..............................
..............................#...#.............................
.............................#.#.#.#............................
............................#.......#...........................
...........................#.#.....#.#..........................
..........................#...#...#...#.........................
.........................#.#.#.#.#.#.#.#........................
........................#...............#.......................
---(output snipped)---
</pre>
 
=={{header|Go}}==
Anonymous user