-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18b.cpp
64 lines (61 loc) · 1.99 KB
/
day_18b.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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int count(const int row, const int col, const std::vector<std::string>& grid) {
int total = 0;
for (int r = row - 1; r <= row + 1; r++) {
for(int c = col-1; c <= col+1; c++) {
total += (grid[r][c] == '#') ? 1 : 0;
}
}
total -= (grid[row][col] == '#') ? 1 : 0;
return total;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_18_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<std::string> grid;
while(std::getline(file, line)) {
line = '.' + line + '.';
grid.push_back(line);
}
const auto n_cols = grid[0].size();
grid.insert(grid.begin(), std::string(n_cols, '.'));
grid.push_back(std::string(n_cols, '.'));
grid[1][1] = '#';
grid[grid.size() - 2][1] = '#';
grid[1][grid[0].size() - 2] = '#';
grid[grid.size() - 2][grid[0].size() - 2] = '#';
int step = 0;
while(step < 100) {
const auto old_grid = grid;
for(int row = 1; row < grid.size() - 1; row++) {
for(int col = 1; col < grid[0].size() - 1; col++) {
if (row == 1 && col == 1) continue;
if (row == grid.size() - 2 && col == 1) continue;
if (row == 1 && col == grid[0].size() - 2) continue;
if (row == grid.size() - 2 && col == grid[0].size() - 2) continue;
const int n = count(row, col, old_grid);
if (old_grid[row][col] == '.' && n == 3) {
grid[row][col] = '#';
} else if (old_grid[row][col] == '#' && (n != 3 && n != 2)) {
grid[row][col] = '.';
}
}
}
step++;
}
int total = 0;
for (const auto & row : grid) {
for (const auto ele : row ) {
total += (ele == '#') ? 1 : 0;
}
}
std::cout << total << '\n';
return 0;
}