-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_11a.cpp
109 lines (98 loc) · 2.93 KB
/
day_11a.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
#include <fstream>
#include <iostream>
#include <vector>
constexpr int size_of_conv_left = 1;
constexpr int size_of_conv_right = 1;
constexpr int size_of_conv_up = 1;
constexpr int size_of_conv_down = 1;
constexpr char chair = 'L';
constexpr char occupied = '#';
constexpr char floor = '.';
template <typename T>
void PrintMap(const T& map) {
std::cout << "----------------" << '\n';
for (int row = 0; row < map.size(); row++) {
for (int col = 0; col < map[0].size(); col++) {
std::cout << map[row][col] << ' ';
}
std::cout << '\n';
}
std::cout << "----------------" << '\n';
}
bool updateNMapAtCoord(const std::vector<std::string>& charmap,
std::vector<std::vector<int>>& nmap, const int row,
const int col) {
int count = 0;
for (int i = row - size_of_conv_up; i <= row + size_of_conv_down; ++i) {
if (i < 0 || i >= charmap.size()) continue;
for (int j = col - size_of_conv_left; j <= col + size_of_conv_down; ++j) {
if (j < 0 || j >= charmap[0].size()) continue;
if (i == row && j == col) continue;
if (charmap[i][j] == occupied) {
++count;
}
}
}
if (nmap[row][col] != count) {
nmap[row][col] = count;
return true;
}
return false;
}
bool updateCharMapAtCoord(std::vector<std::string>& charmap,
const std::vector<std::vector<int>>& nmap,
const int row, const int col) {
if (charmap[row][col] == occupied && nmap[row][col] >= 4) {
charmap[row][col] = chair;
return true;
} else if (charmap[row][col] == chair && nmap[row][col] == 0) {
charmap[row][col] = occupied;
return true;
}
return false;
}
int main() {
std::ifstream file{"../input/day_11_input"};
std::string line;
std::vector<std::string> charmap;
while (std::getline(file, line)) {
charmap.push_back(line);
}
const int n_rows = charmap.size();
const int n_cols = charmap[0].size();
std::vector<std::vector<int>> nmap(n_rows, std::vector<int>(n_cols, -1));
for (int row = 0; row < n_rows; row++) {
for (int col = 0; col < n_cols; col++) {
updateNMapAtCoord(charmap, nmap, row, col);
}
}
for (int row = 0; row < n_rows; row++) {
for (int col = 0; col < n_cols; col++) {
updateCharMapAtCoord(charmap, nmap, row, col);
}
}
bool changed = true;
while (changed) {
changed = false;
for (int row = 0; row < n_rows; row++) {
for (int col = 0; col < n_cols; col++) {
if (updateNMapAtCoord(charmap, nmap, row, col)) {
changed = true;
}
}
}
for (int row = 0; row < n_rows; row++) {
for (int col = 0; col < n_cols; col++) {
updateCharMapAtCoord(charmap, nmap, row, col);
}
}
}
int count = 0;
for (int row = 0; row < n_rows; row++) {
for (int col = 0; col < n_cols; col++) {
if (charmap[row][col] == occupied) ++count;
}
}
std::cout << count << '\n';
return count;
}