-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.cpp
150 lines (138 loc) · 3.76 KB
/
13.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
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <algorithm>
using Pattern = std::vector<std::vector<bool>>;
std::ostream& operator<<(std::ostream& os, const Pattern& p) {
for (const auto& row : p) {
for (const auto& x : row) os << x;
os << std::endl;
}
return os;
}
std::vector<bool> slice_j(const Pattern& p, int j) {
std::vector<bool> ret;
for (const auto& row : p) ret.push_back(row[j]);
return ret;
}
int part1_(const Pattern& pattern) {
int m = pattern.size(), n = pattern[0].size();
for (int i = 1; i < m; ++i) {
bool found = true;
int l = i - 1, r = i;
while (l >= 0 && r < m) {
if (pattern[l] != pattern[r]) {
found = false;
break;
}
--l; ++r;
}
if (found) return i * 100;
}
for (int j = 1; j < n; ++j) {
bool found = true;
int l = j - 1, r = j;
while (l >= 0 && r < n) {
if (slice_j(pattern, l) != slice_j(pattern, r)) {
found = false;
break;
}
--l; ++r;
}
if (found) return j;
}
return -1;
}
int part1(const std::vector<Pattern>& patterns) {
int ans = 0;
for (const auto& pattern : patterns) {
ans += part1_(pattern);
}
return ans;
}
bool one_bit_diff(int a, int b) {
int c = a ^ b;
return !(c & (c - 1));
}
int part2_(const Pattern& pattern) {
int m = pattern.size(), n = pattern[0].size();
std::vector<int> xs;
for (const auto& row : pattern) {
int x = 0;
for (const auto& v : row) x = (x + v) << 1;
xs.push_back(x);
}
for (int i = 1; i < m; ++i) {
bool found = true;
int l = i - 1, r = i;
bool used = false;
while (l >= 0 && r < m) {
if (xs[l] != xs[r]) {
if (used || !one_bit_diff(xs[l], xs[r])) {
found = false;
break;
}
used = true;
}
--l; ++r;
}
if (used && found) return i * 100;
}
xs.clear();
for (int j = 0; j < n; ++j) {
int x = 0;
for (const auto& v : slice_j(pattern, j)) x = (x + v) << 1;
xs.push_back(x);
}
for (int j = 1; j < n; ++j) {
bool found = true;
int l = j - 1, r = j;
bool used = false;
while (l >= 0 && r < n) {
if (xs[l] != xs[r]) {
if (used || !one_bit_diff(xs[l], xs[r])) {
found = false;
break;
}
used = true;
}
--l; ++r;
}
if (used && found) return j;
}
return -1;
}
int part2(const std::vector<Pattern>& patterns) {
// int maxm = 0, maxn = 0;
// for (const auto& pattern : patterns) {
// maxm = std::max(maxm, static_cast<int>(pattern.size()));
// maxn = std::max(maxn, static_cast<int>(pattern[0].size()));
// }
// std::cout << maxm << " " << maxn << std::endl;
// 17 17
int ans = 0;
for (const auto& pattern : patterns) {
if (part2_(pattern) == -1) std::cout << pattern << std::endl;
ans += part2_(pattern);
}
return ans;
}
int main() {
std::ifstream f("13.txt");
std::string line;
std::vector<Pattern> patterns(1);
while (std::getline(f, line)) {
if (line.size() == 0) {
patterns.push_back({});
continue;
}
auto& pattern = patterns.back();
pattern.push_back({});
for (const auto& c : line) {
if (c == '.') pattern.back().push_back(0);
else pattern.back().push_back(1);
}
}
std::cout << part2(patterns) << std::endl;
}