~quf/computers-are-fast-2020

computers-are-fast-2020/src/17.c -rw-r--r-- 8.2 KiB
6f56515eLukas Himbert Draw the rest of the fine owl. 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

/* input is 8x8; we need 6 cells to grow into and one buffer row/column in each direction */
#define INPUT 8
#define STEPS 6
#define S1 (INPUT + 2*STEPS+2)
#define S2 S1
#define S3 (2*STEPS+3)
#define S4 S3

#define index3(s1, s2, s3)     (              (s3) + S3 * ((s2) + S2 * (s1))  )
#define index4(s1, s2, s3, s4) ( (s4) + S4 * ((s3) + S3 * ((s2) + S2 * (s1))) )

/* This only works for statically allocated arrays! */
#define LENGTH(arr) ((sizeof arr) / (sizeof *arr))

int main(void) {
  bool cells3[S1*S2*S3] = { 0, };
  int neighbours3[S1*S2*S3] = { 0, };

  bool cells4[S1*S2*S3*S4] = { 0, };
  int neighbours4[S1*S2*S3*S4] = { 0, };

  for (size_t s2 = 0; s2 < S2; ++s2) {
    for (size_t s1 = 0; s1 < S1; ++s1) {
      int c = getchar();
      if (c == '\n' || c == EOF || s1 >= INPUT || s2 >= INPUT) {
        s1 = 0;
        break;
      }
      bool x = (c == '#');
      size_t d1 = STEPS+1;
      size_t d2 = STEPS+1;
      size_t m3 = S3 / 2;
      size_t m4 = S3 / 2;
      cells3[index3(s1+d1, s2+d2, m3)] = x;
      cells4[index4(s1+d1, s2+d2, m3, m4)] = x;
    }
  }
  if (ferror(stdin)) {
    fprintf(stderr, "Input error: %s.\n", strerror(errno));
    return 1;
  }

  for (size_t i = 0; i < STEPS; ++i) {
    /* update neighbour counts */
    for (size_t s1 = STEPS - i; s1 < S1 - STEPS + i; ++s1) {
    for (size_t s2 = STEPS - i; s2 < S2 - STEPS + i; ++s2) {
    for (size_t s3 = STEPS - i; s3 < S3 - STEPS + i; ++s3) {
      /* 3d  */
      neighbours3[index3(s1, s2, s3)] = \
        cells3[index3(s1    , s2    , s3 + 1)] + \
        cells3[index3(s1    , s2    , s3 - 1)] + \
        cells3[index3(s1    , s2 + 1, s3    )] + \
        cells3[index3(s1    , s2 + 1, s3 + 1)] + \
        cells3[index3(s1    , s2 + 1, s3 - 1)] + \
        cells3[index3(s1    , s2 - 1, s3    )] + \
        cells3[index3(s1    , s2 - 1, s3 + 1)] + \
        cells3[index3(s1    , s2 - 1, s3 - 1)] + \
        cells3[index3(s1 + 1, s2    , s3    )] + \
        cells3[index3(s1 + 1, s2    , s3 + 1)] + \
        cells3[index3(s1 + 1, s2    , s3 - 1)] + \
        cells3[index3(s1 + 1, s2 + 1, s3    )] + \
        cells3[index3(s1 + 1, s2 + 1, s3 + 1)] + \
        cells3[index3(s1 + 1, s2 + 1, s3 - 1)] + \
        cells3[index3(s1 + 1, s2 - 1, s3    )] + \
        cells3[index3(s1 + 1, s2 - 1, s3 + 1)] + \
        cells3[index3(s1 + 1, s2 - 1, s3 - 1)] + \
        cells3[index3(s1 - 1, s2    , s3    )] + \
        cells3[index3(s1 - 1, s2    , s3 + 1)] + \
        cells3[index3(s1 - 1, s2    , s3 - 1)] + \
        cells3[index3(s1 - 1, s2 + 1, s3    )] + \
        cells3[index3(s1 - 1, s2 + 1, s3 + 1)] + \
        cells3[index3(s1 - 1, s2 + 1, s3 - 1)] + \
        cells3[index3(s1 - 1, s2 - 1, s3    )] + \
        cells3[index3(s1 - 1, s2 - 1, s3 + 1)] + \
        cells3[index3(s1 - 1, s2 - 1, s3 - 1)];
      /* 4d */
    for (size_t s4 = STEPS - i; s4 < S4 - STEPS + i; ++s4) {
      neighbours4[index4(s1, s2, s3, s4)] =
        cells4[index4(s1    , s2    , s3    , s4 + 1)] + \
        cells4[index4(s1    , s2    , s3    , s4 - 1)] + \
        cells4[index4(s1    , s2    , s3 + 1, s4    )] + \
        cells4[index4(s1    , s2    , s3 + 1, s4 + 1)] + \
        cells4[index4(s1    , s2    , s3 + 1, s4 - 1)] + \
        cells4[index4(s1    , s2    , s3 - 1, s4    )] + \
        cells4[index4(s1    , s2    , s3 - 1, s4 + 1)] + \
        cells4[index4(s1    , s2    , s3 - 1, s4 - 1)] + \
        cells4[index4(s1    , s2 + 1, s3    , s4    )] + \
        cells4[index4(s1    , s2 + 1, s3    , s4 + 1)] + \
        cells4[index4(s1    , s2 + 1, s3    , s4 - 1)] + \
        cells4[index4(s1    , s2 + 1, s3 + 1, s4    )] + \
        cells4[index4(s1    , s2 + 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1    , s2 + 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1    , s2 + 1, s3 - 1, s4    )] + \
        cells4[index4(s1    , s2 + 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1    , s2 + 1, s3 - 1, s4 - 1)] + \
        cells4[index4(s1    , s2 - 1, s3    , s4    )] + \
        cells4[index4(s1    , s2 - 1, s3    , s4 + 1)] + \
        cells4[index4(s1    , s2 - 1, s3    , s4 - 1)] + \
        cells4[index4(s1    , s2 - 1, s3 + 1, s4    )] + \
        cells4[index4(s1    , s2 - 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1    , s2 - 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1    , s2 - 1, s3 - 1, s4    )] + \
        cells4[index4(s1    , s2 - 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1    , s2 - 1, s3 - 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2    , s3    , s4    )] + \
        cells4[index4(s1 + 1, s2    , s3    , s4 + 1)] + \
        cells4[index4(s1 + 1, s2    , s3    , s4 - 1)] + \
        cells4[index4(s1 + 1, s2    , s3 + 1, s4    )] + \
        cells4[index4(s1 + 1, s2    , s3 + 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2    , s3 + 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2    , s3 - 1, s4    )] + \
        cells4[index4(s1 + 1, s2    , s3 - 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2    , s3 - 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3    , s4    )] + \
        cells4[index4(s1 + 1, s2 + 1, s3    , s4 + 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3    , s4 - 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3 + 1, s4    )] + \
        cells4[index4(s1 + 1, s2 + 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3 - 1, s4    )] + \
        cells4[index4(s1 + 1, s2 + 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2 + 1, s3 - 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3    , s4    )] + \
        cells4[index4(s1 + 1, s2 - 1, s3    , s4 + 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3    , s4 - 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3 + 1, s4    )] + \
        cells4[index4(s1 + 1, s2 - 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3 - 1, s4    )] + \
        cells4[index4(s1 + 1, s2 - 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1 + 1, s2 - 1, s3 - 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2    , s3    , s4    )] + \
        cells4[index4(s1 - 1, s2    , s3    , s4 + 1)] + \
        cells4[index4(s1 - 1, s2    , s3    , s4 - 1)] + \
        cells4[index4(s1 - 1, s2    , s3 + 1, s4    )] + \
        cells4[index4(s1 - 1, s2    , s3 + 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2    , s3 + 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2    , s3 - 1, s4    )] + \
        cells4[index4(s1 - 1, s2    , s3 - 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2    , s3 - 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3    , s4    )] + \
        cells4[index4(s1 - 1, s2 + 1, s3    , s4 + 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3    , s4 - 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3 + 1, s4    )] + \
        cells4[index4(s1 - 1, s2 + 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3 - 1, s4    )] + \
        cells4[index4(s1 - 1, s2 + 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2 + 1, s3 - 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3    , s4    )] + \
        cells4[index4(s1 - 1, s2 - 1, s3    , s4 + 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3    , s4 - 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3 + 1, s4    )] + \
        cells4[index4(s1 - 1, s2 - 1, s3 + 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3 + 1, s4 - 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3 - 1, s4    )] + \
        cells4[index4(s1 - 1, s2 - 1, s3 - 1, s4 + 1)] + \
        cells4[index4(s1 - 1, s2 - 1, s3 - 1, s4 - 1)];
    }
    }}}
    /* update cells for 3d cells */
    for (size_t j = 0; j < LENGTH(cells3); ++j) {
      cells3[j] &= (neighbours3[j] == 2);
      cells3[j] |= (neighbours3[j] == 3);
    }
    /* update cells for 4d cells */
    for (size_t j = 0; j < LENGTH(cells4); ++j) {
      cells4[j] &= (neighbours4[j] == 2);
      cells4[j] |= (neighbours4[j] == 3);
    }
  }

  /* print number of set cells */
  size_t n = 0;
  for (size_t i = 0; i < LENGTH(cells3); ++i) {
    n += cells3[i];
  }
  printf("%zu\n", n);

  n = 0;
  for (size_t i = 0; i < LENGTH(cells4); ++i) {
    n += cells4[i];
  }
  printf("%zu\n", n);

  return 0;
}