Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ON '#'
- #define OFF '.'
- #define WIDTH 16
- #define HEIGHT 16
- #define ALIVE 0x40
- #define RULE(x) (x==(ALIVE|2) || x==(ALIVE|3) || x==3)
- static const int nbors[] = {
- -1, -1, 0, -1, 1, -1,
- -1, 0, 1, 0,
- -1, 1, 0, 1, 1, 1,
- };
- static const int nsize = sizeof(nbors) / sizeof(int) / 2;
- #define SIZE (WIDTH*HEIGHT)
- static void set_cell(char *buf, size_t cell);
- int main()
- {
- char *buf = calloc(1, SIZE);
- for (size_t i=0; i<SIZE; ++i) {
- char c = getchar();
- if (c == '\n') c = getchar();
- if (c == ON) {
- set_cell(buf, i);
- }
- }
- for (size_t i=0; i<SIZE; ++i) {
- putchar(RULE(buf[i]) ? ON : OFF);
- if (i % WIDTH == WIDTH-1) {
- puts("");
- }
- }
- free(buf);
- return 0;
- }
- static void set_cell(char *buf, size_t cell)
- {
- buf[cell] |= ALIVE;
- int x = cell % WIDTH, y = cell / WIDTH;
- for (int n=0; n<nsize; ++n) {
- int cx = x + nbors[n*2];
- int cy = y + nbors[n*2+1];
- if (cx >= 0 && cx < WIDTH && cy >= 0 && cy < HEIGHT) {
- char *c = buf + cy*WIDTH + cx;
- *c = ((*c & ~ALIVE) + 1) | (*c & ALIVE);
- }
- }
- }
Add Comment
Please, Sign In to add comment