-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18b.cpp
169 lines (153 loc) · 4.7 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
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
#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;
}
struct AreaTime {
const int time;
const std::vector<std::string> area;
AreaTime(const int time, const std::vector<std::string>& area) : time(time), area(area) {}
bool operator == (const AreaTime& at) const {
return area == at.area;
}
};
struct hash_AreaTime {
std::size_t operator () (const AreaTime& at) const {
const auto ans = count(at.area);
return (ans.n_open << 4) + (ans.n_wooded << 2) + ans.n_yard;
}
};
int main(int argc, char* argv[]) {
std::string input = "../input/day_18_input";
long long n_minutes = 1000000000;
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()));
std::unordered_set<AreaTime, hash_AreaTime> seen;
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 (long long minute = 1; minute <= n_minutes; minute++) {
updateSurroundings(area, surroundings, deltas);
updateArea(area, surroundings, deltas);
const AreaTime at = AreaTime(minute, area);
if (const auto it = seen.find(at); it != seen.end()) {
// std::cout << "Current time = " << minute << '\n';
// std::cout << "Repeated pattern from time = " << it->time << '\n'; // first repeat
const auto delta = minute - it->time; // repeats every
const auto pattern_at_n_minutes = (n_minutes - it->time) % delta;
for (const auto& at : seen) {
if (at.time == (it->time + pattern_at_n_minutes)) {
const auto st = count(at.area);
std::cout << st.n_wooded * st.n_yard << '\n';
return 0;
}
}
}
seen.insert(at);
}
const auto st = count(area);
std::cout << st.n_wooded * st.n_yard << '\n';
return 0;
}