Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created August 10, 2008 15:45
Show Gist options
  • Save jeremyBanks/4746 to your computer and use it in GitHub Desktop.
Save jeremyBanks/4746 to your computer and use it in GitHub Desktop.
[2010-01] experimenting with 1d life, led to my old logo/avatar
//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -o "$x" "$0")&&"$x" $*;exit
#import <stdio.h>
#import <stdint.h>
#import <time.h>
// Setting two adjacent bits produce an oscillator of period six.
// Setting two bits seperated by another produce an oscillator of period four.
// Setting ever other bit produces a still life.
uint8_t byte = 0x18;
uint8_t next;
uint8_t i;
uint8_t neighbours;
struct timespec cooldown = {0, 200000000};
struct timespec discarded;
#define getBit(sig) ((byte >> sig) & 1)
int main() {
for(;;) {
printf("|");
for(i = 0; i < 8; i++) {
printf(getBit(i) ? "*" : " ");
}
printf("| 0x%02X\n", byte);
next = byte;
for(i = 0; i < 8; i++) {
neighbours = getBit((i + 7) % 8) + getBit((i + 1) % 8);
if(neighbours == 1) {
next ^= (1 << i); // This cell is toggled next generation.
} else if(neighbours > 1) {
next &= ~(1 << i); // This cell is dead next generation.
}
}
if(byte == next) {
break;
}
byte = next;
nanosleep(&cooldown, &discarded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment