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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* PART2 is defined for part 2, but not for part 1 */
#ifdef PART2
#define MAXTIME 50
#else
#define MAXTIME 1
#endif
#define MAXDEPTH 50
#define MAXHEIGHT 50
#define MAXWIDTH 50
char space[MAXTIME][MAXDEPTH][MAXHEIGHT][MAXWIDTH];
char temp[MAXTIME][MAXDEPTH][MAXHEIGHT][MAXWIDTH];
/* Check the cubes around the given coordinates and update the temp array accordingly */
static void
update_cube(unsigned int const t, unsigned int const i, unsigned int const j, unsigned int const k)
{
unsigned int active = 0;
for (int tme = t - 1; tme <= (int) t + 1; tme++) {
if (tme < 0 || tme > MAXTIME - 1)
continue;
for (int dep = i - 1; dep <= (int) i + 1; dep++) {
if (dep < 0 || dep > MAXDEPTH - 1)
continue;
for (int hgt = j - 1; hgt <= (int) j + 1; hgt++) {
if (hgt < 0 || hgt > MAXHEIGHT - 1)
continue;
for (int wid = k - 1; wid <= (int) k + 1; wid++) {
if (wid < 0 || wid > MAXWIDTH - 1
|| (tme == (int) t && dep == (int) i && hgt == (int) j
&& wid == (int) k))
continue;
if (space[tme][dep][hgt][wid] == '#')
active++;
}
}
}
}
if (space[t][i][j][k] == '#' && (active == 2 || active == 3))
temp[t][i][j][k] = '#';
else if (space[t][i][j][k] == '.' && active == 3)
temp[t][i][j][k] = '#';
else
temp[t][i][j][k] = '.';
}
int
main(void)
{
/* Initialze space */
memset(space, '.', MAXTIME * MAXDEPTH * MAXHEIGHT * MAXWIDTH);
/* Read input */
FILE *fpt = fopen("input", "r");
char c;
unsigned int t, i, j, k;
j = k = 0;
while ((c = fgetc(fpt)) != EOF) {
if (c == '\n') {
j++;
k = 0;
}
else {
space[MAXTIME / 2][MAXDEPTH / 2][j + MAXHEIGHT / 2][k + MAXWIDTH / 2] = c;
k++;
}
}
/* Do the boot cycles */
for (int cycle = 0; cycle < 6; cycle++) {
for (t = 0; t < MAXTIME; t++)
for (i = 0; i < MAXDEPTH; i++)
for (j = 0; j < MAXHEIGHT; j++)
for (k = 0; k < MAXWIDTH; k++)
update_cube(t, i, j, k);
/* Copy temp to space */
for (t = 0; t < MAXTIME; t++)
for (i = 0; i < MAXDEPTH; i++)
for (j = 0; j < MAXHEIGHT; j++)
for (k = 0; k < MAXWIDTH; k++)
space[t][i][j][k] = temp[t][i][j][k];
}
/* Count the active cubes */
unsigned int count = 0;
for (t = 0; t < MAXTIME; t++)
for (i = 0; i < MAXDEPTH; i++)
for (j = 0; j < MAXHEIGHT; j++)
for (k = 0; k < MAXWIDTH; k++)
if (space[t][i][j][k] == '#')
count++;
printf("%u\n", count);
return EXIT_SUCCESS;
}
|