Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ON '#'
  5. #define OFF '.'
  6. #define WIDTH 16
  7. #define HEIGHT 16
  8.  
  9. #define ALIVE 0x40
  10. #define RULE(x) (x==(ALIVE|2) || x==(ALIVE|3) || x==3)
  11.  
  12. static const int nbors[] = {
  13. -1, -1, 0, -1, 1, -1,
  14. -1, 0, 1, 0,
  15. -1, 1, 0, 1, 1, 1,
  16. };
  17. static const int nsize = sizeof(nbors) / sizeof(int) / 2;
  18.  
  19. #define SIZE (WIDTH*HEIGHT)
  20.  
  21. static void set_cell(char *buf, size_t cell);
  22.  
  23. int main()
  24. {
  25. char *buf = calloc(1, SIZE);
  26. for (size_t i=0; i<SIZE; ++i) {
  27. char c = getchar();
  28. if (c == '\n') c = getchar();
  29. if (c == ON) {
  30. set_cell(buf, i);
  31. }
  32. }
  33. for (size_t i=0; i<SIZE; ++i) {
  34. putchar(RULE(buf[i]) ? ON : OFF);
  35. if (i % WIDTH == WIDTH-1) {
  36. puts("");
  37. }
  38. }
  39. free(buf);
  40. return 0;
  41. }
  42.  
  43. static void set_cell(char *buf, size_t cell)
  44. {
  45. buf[cell] |= ALIVE;
  46.  
  47. int x = cell % WIDTH, y = cell / WIDTH;
  48. for (int n=0; n<nsize; ++n) {
  49. int cx = x + nbors[n*2];
  50. int cy = y + nbors[n*2+1];
  51. if (cx >= 0 && cx < WIDTH && cy >= 0 && cy < HEIGHT) {
  52. char *c = buf + cy*WIDTH + cx;
  53. *c = ((*c & ~ALIVE) + 1) | (*c & ALIVE);
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment