-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24a.cpp
112 lines (97 loc) · 2.67 KB
/
day_24a.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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <fstream>
void PrintGrid(const std::vector<std::string>& view) {
for(const auto & row : view) {
for (const auto c : row) {
std::cout << c;
}
std::cout << '\n';
}
std::cout << '\n';
}
struct Pair {
int x, y;
};
struct hash_pair {
template <class T1, class T2>
size_t operator()(const Pair& p) const {
auto hash1 = std::hash<T1>{}(p.x);
auto hash2 = std::hash<T2>{}(p.y);
return hash1 ^ hash2;
}
};
int main(int argc, char * argv[]) {
std::vector<std::string> grid;
std::string input = "../input/day_24_input";
if (argc > 1) {
input = argv[1];
}
std::unordered_set<long long> scores;
std::ifstream file(input);
std::string line;
while (std::getline(file, line)) {
line.erase(std::remove_if(std::begin(line), std::end(line), [](auto c) { return !isprint(c); }), std::end(line));
if (line.empty()) break;
grid.emplace_back('.' + line + '.');
}
grid.insert(std::begin(grid), std::string(grid[0].size(), '.'));
grid.emplace_back(std::string(grid[0].size(), '.'));
auto bug_count = std::vector<std::vector<int>>(grid.size(), std::vector<int>(grid[0].size(), 0));
const auto deltas = std::vector<Pair> {
{1,0},
{0,1},
{-1,0},
{0,-1}
};
long long score = 0;
long long c_val = 1;
for (int row = 1; row < grid.size() -1; row++) {
for (int col = 1; col < grid[0].size() -1; col++) {
if (grid[row][col] == '#') {
score += c_val;
}
c_val *= 2;
}
}
while (scores.find(score) == scores.end()) {
PrintGrid(grid);
std::cout << '\n';
scores.insert(score);
for (int row = 1; row < grid.size() -1; row++) {
for (int col = 1; col < grid[0].size() -1; col++) {
int count = 0;
for (const auto& delta : deltas) {
if (grid[row + delta.x][col + delta.y] == '#') {
count++;
}
bug_count[row][col] = count;
}
}
}
for (int row = 1; row < grid.size() -1; row++) {
for (int col = 1; col < grid[0].size() -1; col++) {
if (grid[row][col] == '#' && bug_count[row][col] != 1) {
grid[row][col] = '.';
} else if (grid[row][col] == '.' && (bug_count[row][col] == 1 || bug_count[row][col] == 2)) {
grid[row][col] = '#';
}
}
}
c_val = 1;
score = 0;
for (int row = 1; row < grid.size() -1; row++) {
for (int col = 1; col < grid[0].size() -1; col++) {
if (grid[row][col] == '#') {
score += c_val;
}
c_val *= 2;
}
}
}
std::cout << "Score: " << score << '\n';
return score;
}