-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_18b.cpp
60 lines (57 loc) · 1.63 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
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
#include <algorithm>
bool is_trap(const bool l, const bool c, const bool r) {
if (l && c && !r) return true;
if (!l && c && r) return true;
if (l && !c && !r) return true;
if (!l && !c && r) return true;
return false;
}
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;
while(std::getline(file, line)) {
int count = 0;
std::vector<std::vector<bool>> map(1, std::vector<bool>());
for (const char c : line) {
if (c == '.') {
count++;
map[0].push_back(false);
} else {
map[0].push_back(true);
}
}
while(map.size() < 400000) {
const auto& prev = map.back();
std::vector<bool> next_row;
next_row.reserve(prev.size());
next_row.push_back(is_trap(false, prev[0], prev[1]));
if (!next_row.back()) count++;
for (int i = 1; i < prev.size() - 1; i++) {
next_row.push_back(is_trap(prev[i-1], prev[i], prev[i+1]));
if (!next_row.back()) count++;
}
next_row.push_back(is_trap(prev[prev.size()-2], prev[prev.size()-1], false));
if (!next_row.back()) count++;
map.push_back(next_row);
}
// for (const auto& row : map) {
// for (const auto ele : row) {
// std::cout << (ele ? '^' : '.');
// }
// std::cout << '\n';
// }
std::cout << count << '\n';
}
return 0;
}