-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18a.cpp
139 lines (126 loc) · 3.64 KB
/
day_18a.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
#include <cassert>
#include <queue>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <thread>
using namespace std::literals::chrono_literals;
struct SurroundingTypes {
int n_open = 0;
int n_wooded = 0;
int n_yard = 0;
void reset() {
n_open = 0;
n_wooded = 0;
n_yard = 0;
}
friend std::ostream& operator << (std::ostream& os, const SurroundingTypes& s);
};
std::ostream& operator << (std::ostream& os, const SurroundingTypes& s) {
os << "(" << s.n_open << ", " << s.n_wooded << ", " << s.n_yard << ") ";
return os;
}
template<typename T2D>
void print(const T2D& t2d) {
for (const auto& row : t2d) {
for (const auto & ele : row ) {
std::cout << ele;
}
std::cout << '\n';
}
}
void updateSurroundings(const std::vector<std::string>& area, std::vector<std::vector<SurroundingTypes>>& surroundings, const std::vector<std::vector<int>>& deltas) {
for (int row = 1; row < area.size() - 1; row++) {
for (int col = 1; col < area[0].size() - 1; col++) {
surroundings[row][col].reset();
for (const auto& delta : deltas) {
const int new_row = row + delta[0];
const int new_col = col + delta[1];
if (area[new_row][new_col] == '#') {
surroundings[row][col].n_yard++;
} else if (area[new_row][new_col] == '.') {
surroundings[row][col].n_open++;
} else if (area[new_row][new_col] == '|') {
surroundings[row][col].n_wooded++;
}
}
}
}
}
void updateArea(std::vector<std::string>& area, const std::vector<std::vector<SurroundingTypes>>& surroundings, const std::vector<std::vector<int>>& deltas) {
for (int row = 1; row < area.size() - 1; row++) {
for (int col = 1; col < area[0].size() - 1; col++) {
const auto& s = surroundings[row][col];
auto& a = area[row][col];
if (a == '.' && s.n_wooded >=3) {
a = '|';
} else if (a == '|' && s.n_yard >=3) {
a = '#';
} else if (a == '#' && !(s.n_yard >= 1 && s.n_wooded >= 1)) {
a = '.';
}
}
}
}
SurroundingTypes count(const std::vector<std::string>& area) {
SurroundingTypes total;
for (int row = 1; row < area.size() - 1; row++) {
for (int col = 1; col < area[0].size() - 1; col++) {
if (area[row][col] == '#') {
total.n_yard++;
} else if (area[row][col] == '|') {
total.n_wooded++;
} else if (area[row][col] == '.') {
total.n_open++;
}
}
}
return total;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_18_input";
int n_minutes = 10;
if (argc > 1) {
input = argv[1];
}
if (argc > 2) {
n_minutes = std::stoi(argv[2]);
}
std::string line;
std::ifstream file(input);
std::vector<std::string> area;
area.emplace_back();
while(std::getline(file, line)) {
area.emplace_back("0" + line + "0");
}
area[0] = std::string(area[1].size(), '0');
area.emplace_back(std::string(area[1].size(), '0'));
std::vector<std::vector<SurroundingTypes>> surroundings(area.size(), std::vector<SurroundingTypes>(area[0].size()));
const std::vector<std::vector<int>> deltas {
{0, 1},
{1, 0},
{-1, 0},
{0, -1},
{1,1},
{1,-1},
{-1,-1},
{-1, 1}
};
// print(area);
for (int minute = 1; minute <= n_minutes; minute++) {
updateSurroundings(area, surroundings, deltas);
// print(surroundings);
updateArea(area, surroundings, deltas);
// print(area);
// std::cout << '\n';
}
const auto st = count(area);
std::cout << st.n_wooded * st.n_yard << '\n';
return 0;
}