-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24b.cpp
201 lines (184 loc) · 6.89 KB
/
day_24b.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
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
194
195
196
197
198
199
200
201
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <fstream>
void PrintGrid(const std::vector<std::string>& view) {
for(const auto & row : view) {
for (const auto c : row) {
std::cout << c;
}
std::cout << '\n';
}
std::cout << '\n';
}
struct Pair {
int x, y;
};
struct pair3 {
int x, y, z;
};
struct hash_pair3 {
template <class T1, class T2>
size_t operator()(const pair3& p) const {
auto hash1 = std::hash<T1>{}(p.x);
auto hash2 = std::hash<T2>{}(p.y);
auto hash3 = std::hash<T2>{}(p.z);
return hash1 ^ hash2 ^ hash3;
}
};
struct hash_pair {
template <class T1, class T2>
size_t operator()(const Pair& p) const {
auto hash1 = std::hash<T1>{}(p.x);
auto hash2 = std::hash<T2>{}(p.y);
return hash1 ^ hash2;
}
};
std::vector<pair3> getNeighbours(const std::vector<std::vector<std::string>>& grids, const int level, const int row, const int col) {
const auto& grid = grids[0];
const int half_row_size = grid.size()/2;
const int half_col_size = grid[0].size()/2;
const int row_size = grid.size();
const int col_size = grid[0].size();
if (row == half_row_size && col == half_col_size) {
return {};
}
else if (row == 1 and col == 1) {
return {{level, row+1, col}, {level, row, col+1}, {level+1, half_row_size, half_col_size-1}, {level+1, half_row_size-1, half_col_size}};
}
else if (row == 1 and col == col_size-2) {
return {{level, row+1, col}, {level, row, col-1}, {level+1, half_row_size, half_col_size+1}, {level+1, half_row_size-1, half_col_size}};
}
else if (row == row_size-2 and col == 1) {
return {{level, row-1, col}, {level, row, col+1}, {level+1, half_row_size+1, half_col_size}, {level+1, half_row_size, half_col_size-1}};
}
else if (row == row_size-2 and col == col_size-2) {
return {{level, row-1, col}, {level, row, col-1}, {level+1, half_row_size+1, half_col_size}, {level+1, half_row_size, half_col_size+1}};
}
else if (row == 1) {
return {{level, row+1, col}, {level, row, col+1}, {level, row, col - 1}, {level+1, half_row_size-1, half_col_size}};
}
else if (row == row_size-2) {
return {{level, row-1, col}, {level, row, col+1}, {level, row, col - 1}, {level+1, half_row_size+1, half_col_size}};
}
else if (col == 1) {
return {{level, row+1, col}, {level, row-1, col}, {level, row, col+1}, {level+1, half_row_size, half_col_size-1}};
}
else if (col == col_size-2) {
return {{level, row+1, col}, {level, row-1, col}, {level, row, col-1}, {level+1, half_row_size, half_col_size+1}};
}
else if (row == half_row_size && col == half_col_size-1) {
std::vector<pair3> neighbours = {{level, row + 1, col}, {level, row-1, col}, {level, row, col-1}};
for (int i = 1; i < row_size - 1; i++) {
neighbours.push_back({level - 1, i, 1});
}
return neighbours;
}
else if (row == half_row_size && col == half_col_size+1) {
std::vector<pair3> neighbours = {{level, row + 1, col}, {level, row-1, col}, {level, row, col+1}};
for (int i = 1; i < row_size - 1; i++) {
neighbours.push_back({level - 1, i, col_size-2});
}
return neighbours;
}
else if (row == half_row_size-1 && col == half_col_size) {
std::vector<pair3> neighbours = {{level, row, col+1}, {level, row, col-1}, {level, row-1, col}};
for (int i = 1; i < col_size - 1; i++) {
neighbours.push_back({level - 1, 1, i});
}
return neighbours;
}
else if (row == half_row_size+1 && col == half_col_size) {
std::vector<pair3> neighbours = {{level, row, col+1}, {level, row, col-1}, {level, row+1, col}};
for (int i = 1; i < col_size - 1; i++) {
neighbours.push_back({level - 1, row_size-2, i});
}
return neighbours;
}
else {
return {{level, row+1, col}, {level, row, col+1}, {level, row, col - 1}, {level, row-1, col}};
}
}
int countNeighbouringBugs(const std::vector<std::vector<std::string>>& grids, const int level, const int row, const int col) {
const auto neighbours = getNeighbours(grids, level, row, col);
if (neighbours.size() > 8) {
std::cout << "WH" << neighbours.size() << '\n';
exit(0);
}
int count = 0;
for (const auto& neighbour : neighbours) {
if(grids[neighbour.x][neighbour.y][neighbour.z] == '#') {
count += 1;
}
}
// std::cout << count << '\n';
return count;
}
void updateGrid(std::vector<std::vector<std::string>>& grids, const std::vector<std::vector<std::vector<int>>>& bug_count) {
for (int level = 1; level < grids.size() - 1; level++) {
for (int row = 1; row < grids[0].size() -1; row++) {
for (int col = 1; col < grids[0][0].size() -1; col++) {
if (grids[level][row][col] == '#' && bug_count[level][row][col] != 1) {
grids[level][row][col] = '.';
} else if (grids[level][row][col] == '.' && (bug_count[level][row][col] == 1 || bug_count[level][row][col] == 2)) {
grids[level][row][col] = '#';
}
}
}
}
}
int countBugs(const std::vector<std::vector<std::string>>& grids) {
int count = 0;
for (int level = 1; level < grids.size() - 1; level++) {
for (int row = 1; row < grids[0].size() -1; row++) {
for (int col = 1; col < grids[0][0].size() -1; col++) {
if (grids[level][row][col] == '#') {
count += 1;
}
}
}
}
return count;
}
int main(int argc, char * argv[]) {
std::vector<std::string> grid;
std::string input = "../input/day_24_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
while (std::getline(file, line)) {
line.erase(std::remove_if(std::begin(line), std::end(line), [](auto c) { return !isprint(c); }), std::end(line));
if (line.empty()) break;
grid.emplace_back('.' + line + '.');
}
grid.insert(std::begin(grid), std::string(grid[0].size(), '.'));
grid.emplace_back(std::string(grid[0].size(), '.'));
const auto empty_grid = std::vector<std::string>(grid.size(), std::string(grid[0].size(), '.'));
auto grids = std::vector<std::vector<std::string>>(203, empty_grid);
grids[101] = grid;
auto bug_count = std::vector<std::vector<std::vector<int>>>(grids.size(), std::vector<std::vector<int>>(grids[0].size(), std::vector<int>(grids[0][0].size(), 0)));
for (int time_step = 0; time_step < 200; time_step++) {
// std::cout << time_step << '\n';
// PrintGrid(grids[101]);
auto prev_bug_count = bug_count;
for (int level = 1; level < grids.size() -1 ; level++) {
for (int row = 1; row < grids[0].size() -1; row++) {
for (int col = 1; col < grids[0][0].size() -1; col++) {
// std::cout << level << " " << row << " " << col << '\n';
bug_count[level][row][col] = countNeighbouringBugs(grids, level, row, col);
}
}
}
if (bug_count == prev_bug_count) {
std::cout << "No chage" << '\n';
}
updateGrid(grids, bug_count);
}
int count = countBugs(grids);
std::cout << "Count: " << count << '\n';
return count;
}