summary refs log tree commit diff homepage
path: root/2020/day17.c
blob: 620563f22fb7b0ac914593391d68e9d3c49b0f28 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Dim3 {
	char _[32][32][32];
};
static int neighbors3(const struct Dim3 *d, int z, int y, int x) {
	int active = 0;
	for (int i = -1; i <= +1; ++i)
	for (int j = -1; j <= +1; ++j)
	for (int k = -1; k <= +1; ++k) {
		if (!i && !j && !k) continue;
		active += (d->_[z+i&31][y+j&31][x+k&31] == '#');
	}
	return active;
}
static struct Dim3 step3(const struct Dim3 *prev) {
	struct Dim3 next;
	for (int z = 0; z < 32; ++z)
	for (int y = 0; y < 32; ++y)
	for (int x = 0; x < 32; ++x) {
		int active = neighbors3(prev, z, y, x);
		if (prev->_[z][y][x] == '#') {
			next._[z][y][x] = (active == 2 || active == 3 ? '#' : '.');
		} else {
			next._[z][y][x] = (active == 3 ? '#' : '.');
		}
	}
	return next;
}
struct Dim4 {
	char _[32][32][32][32];
};
static int neighbors4(const struct Dim4 *d, int w, int z, int y, int x) {
	int active = 0;
	for (int i = -1; i <= +1; ++i)
	for (int j = -1; j <= +1; ++j)
	for (int k = -1; k <= +1; ++k)
	for (int l = -1; l <= +1; ++l) {
		if (!i && !j && !k && !l) continue;
		active += (d->_[w+i&31][z+j&31][y+k&31][x+l&31] == '#');
	}
	return active;
}
static struct Dim4 step4(const struct Dim4 *prev) {
	struct Dim4 next;
	for (int w = 0; w < 32; ++w)
	for (int z = 0; z < 32; ++z)
	for (int y = 0; y < 32; ++y)
	for (int x = 0; x < 32; ++x) {
		int active = neighbors4(prev, w, z, y, x);
		if (prev->_[w][z][y][x] == '#') {
			next._[w][z][y][x] = (active == 2 || active == 3 ? '#' : '.');
		} else {
			next._[w][z][y][x] = (active == 3 ? '#' : '.');
		}
	}
	return next;
}
int main(void) {
	struct Dim3 init3 = {0};
	struct Dim4 init4 = {0};
	for (int y = 0;; ++y) {
		if (scanf("%s\n", init3._[0][y]) < 1) break;
		strcpy(init4._[0][0][y], init3._[0][y]);
	}
	struct Dim3 prev3 = init3;
	for (int i = 0; i < 6; ++i) {
		struct Dim3 next3 = step3(&prev3);
		prev3 = next3;
	}
	int active = 0;
	for (int z = 0; z < 32; ++z)
	for (int y = 0; y < 32; ++y)
	for (int x = 0; x < 32; ++x) {
		active += (prev3._[z][y][x] == '#');
	}
	printf("%d\n", active);
	struct Dim4 prev4 = init4;
	for (int i = 0; i < 6; ++i) {
		struct Dim4 next4 = step4(&prev4);
		prev4 = next4;
	}
	active = 0;
	for (int w = 0; w < 32; ++w)
	for (int z = 0; z < 32; ++z)
	for (int y = 0; y < 32; ++y)
	for (int x = 0; x < 32; ++x) {
		active += (prev4._[w][z][y][x] == '#');
	}
	printf("%d\n", active);
}