Skip to content

Instantly share code, notes, and snippets.

@modul
Created August 20, 2012 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modul/3408476 to your computer and use it in GitHub Desktop.
Save modul/3408476 to your computer and use it in GitHub Desktop.
cellular automata printer
/*
* Cellular Automata Printer
*
* Usage: binary-name [RULE INITAL]
* where RULE sets the CA rule (def. 30)
* and INITIAL the initial hex start pattern
* (def. 0x100000000 i.e. a centered 1).
*
* All rights reversed.
* 2012 Remo Giermann <mo@liberejo.de>
*/
#include <stdio.h>
int main(int argc, const char *argv[])
{
long i, w=0, r=30, g=1L<<32;
if (argc == 3) {
sscanf(argv[1], "%u", &r);
sscanf(argv[2], "%lx", &g);
}
while (1) {
for (w=g,i=1,g=0; i<sizeof(long)*8; i++) {
long nbhood = (w&(7L<<(i-1)))>>(i-1);
g |= (r&(1L<<nbhood))<<(i-nbhood);
putchar(w&(1L<<i)?'X':' ');
}
if (w == g || (w&(1L<<63) && w&(1L<<1))) break;
putchar(10); usleep(10000);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment