-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
123 lines (107 loc) · 3.28 KB
/
main.cpp
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
#include <iostream>
#include "boost/multi_array.hpp"
#include "boost/spirit/home/x3.hpp"
#include "utils.h"
using namespace boost::spirit;
struct Cell_ : x3::symbols<bool> {
Cell_() { add("#", true)(".", false); }
};
using Space = boost::multi_array<bool, 4>;
using extents = Space::extent_gen;
using range = Space::extent_range;
constexpr auto N = 20;
constexpr auto IL = -N;
constexpr auto IR = N;
Space parseFile()
{
auto row_ = +Cell_();
auto rows_ = row_ % x3::eol;
auto buffer = AOC::readFile("input.txt");
std::vector<std::vector<bool>> value;
auto result = x3::parse(buffer.begin(), buffer.end(), rows_, value);
assert(result);
auto extent = extents();
auto space = Space(extent[range(-N, N)][range(-N, N)][range(-N, N)][range(-N, N)]);
for (int i = 0; i < value.size(); ++i) {
for (int j = 0; j < value[i].size(); ++j) { space[i][j][0][0] = value[i][j]; }
}
return space;
}
int neighborCount3(const Space &space, int m, int n, int o)
{
int result = 0;
for (int i = -1; i <= 1; ++i)
for (int j = -1; j <= 1; ++j)
for (int k = -1; k <= 1; ++k) { result += space[m + i][n + j][o + k][0]; }
result -= space[m][n][o][0];
return result;
}
int neighborCount4(const Space &space, int m, int n, int o, int p)
{
int result = 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) { result += space[m + i][n + j][o + k][p + l]; }
result -= space[m][n][o][p];
return result;
}
auto applyRule1(const Space &space)
{
auto to = space;
const int start = IL + 1, end = IR - 1;
for (int i = start; i < end; ++i) {
for (int j = start; j < end; ++j) {
for (int k = start; k < end; ++k) {
auto nn = neighborCount3(space, i, j, k);
if (space[i][j][k][0]) {
to[i][j][k][0] = nn == 2 || nn == 3;
} else {
to[i][j][k][0] = nn == 3;
}
}
}
}
return to;
}
auto applyRule2(const Space &space)
{
auto to = space;
const int start = IL + 1, end = IR - 1;
for (int i = start; i < end; ++i) {
for (int j = start; j < end; ++j) {
for (int k = start; k < end; ++k) {
for (int l = start; l < end; ++l) {
auto nn = neighborCount4(space, i, j, k, l);
if (space[i][j][k][l]) {
to[i][j][k][l] = nn == 2 || nn == 3;
} else {
to[i][j][k][l] = nn == 3;
}
}
}
}
}
return to;
}
template <typename F> void f12(const Space &space, F f, int iterN)
{
auto now = space;
for (int iter = 0; iter < iterN; ++iter) { now = f(now); }
int totalOn = 0;
const int start = IL, end = IR;
for (int i = start; i < end; ++i) {
for (int j = start; j < end; ++j) {
for (int k = start; k < end; ++k) {
for (int l = start; l < end; ++l) { totalOn += now[i][j][k][l]; }
}
}
}
std::cout << totalOn << std::endl;
}
int main()
{
const auto space = parseFile();
f12(space, applyRule1, 6);
f12(space, applyRule2, 6);
}