-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_13a.cpp
83 lines (77 loc) · 2.42 KB
/
day_13a.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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
bool check_row(const std::string& row, const int col) {
for (int lhs = col, rhs = col+1; lhs >= 0 && rhs < row.size(); lhs--, rhs++) {
if (row[lhs] != row[rhs]) return false;
}
return true;
}
// Make possible_values a set
std::pair<bool , int> find_reflection(const std::vector<std::string>& pattern) {
bool is_center = false;
std::vector<bool> possible_values(pattern[0].size()-1, true);
for (int col = 0; col < pattern[0].size() - 1; col++) {
possible_values[col] = check_row(pattern[0], col);
}
for (int row_idx = 1; row_idx < pattern.size(); row_idx++) {
for (int values_idx = 0; values_idx < possible_values.size(); values_idx++) {
if (!possible_values[values_idx]) {
continue;
}
possible_values[values_idx] = check_row(pattern[row_idx], values_idx);
}
}
for (int values_idx = 0; values_idx < possible_values.size(); values_idx++) {
if (possible_values[values_idx]) {
return {true, values_idx};
}
}
return {false, -1};
}
std::vector<std::string> rotate_pattern_anti_cw (const std::vector<std::string>& pattern) {
std::vector<std::string> rotated(pattern[0].size(), std::string(pattern.size(), ' ')); // Sanity check on size
for (int row = 0; row < pattern.size(); row++) {
for (int col = 0; col < pattern[0].size(); col++) {
rotated[pattern[0].size() - col - 1][row] = pattern[row][col];
}
}
return rotated;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_13_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<std::string>> patterns;
while(std::getline(file, line)) {
patterns.emplace_back();
auto& pattern = patterns.back();
while (!line.empty()) {
pattern.push_back(line);
std::getline(file, line);
}
}
std::size_t ans = 0;
for (const auto& pattern : patterns) {
const auto [found, center] = find_reflection(pattern);
if (found) {
ans += (center + 1);
continue;
}
const auto [found_rotated, center_rotated] = find_reflection(rotate_pattern_anti_cw(pattern));
if (found_rotated) {
ans += (center_rotated + 1) * 100;
continue;
}
// std::cout << "This should not happen" << '\n'; // Sanity check
}
std::cout << ans << '\n';
return 0;
}