-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.cpp
80 lines (70 loc) · 1.77 KB
/
day25.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
#include "utils/helpers.hpp"
typedef std::vector<int> Sequence;
typedef std::vector<Sequence> Sequences;
typedef std::map<Sequence, std::set<Sequence>> Pairs;
void sortGrid(Grid<char>& g, Sequences& k, Sequences& l) {
Sequence s;
int count;
bool start = true;
Grid<char>::browser br;
br = g.columnBrowse(br);
while (br.has_value()) {
if (std::get<1>(br.value()) == 1) {
if (start) {
start = false;
}
else
s.push_back(count);
count = -1;
}
if (g.get(std::get<0>(br.value())) == '#')
++count;
br = g.columnBrowse(br);
}
s.push_back(count);
if (g.get(Coord(0, 0)) == '#')
l.push_back(s);
else k.push_back(s);
}
bool fit(const Sequence& l, const Sequence& k) {
for (int i = 0; i < 5; ++i) {
if (l[i] + k[i] >= 6)
return false;
}
return true;
}
int main() {
std::ifstream input;
ui part = 5;
Pairs pairs;
ui result = 0;
Sequences keys;
Sequences locks;
Grid<char> read;
std::string line;
if (getFileAndPart(25, input, part))
return errno;
while (!input.eof()) {
getline(input, line);
if (line.empty()){
sortGrid(read, keys, locks);
read.clear();
}
else {
read.addBackLine(line);
}
}
sortGrid(read, keys, locks);
for (const Sequence& k: keys) {
for (const Sequence& l: locks) {
if (fit(l, k)) {
pairs[k].insert(l);
}
}
}
for (Pairs::const_iterator cit = pairs.begin(); cit != pairs.end(); ++cit) {
result += cit->second.size();
}
std::cout << "result is " << result << '\n';
return 0;
}