-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10b.cpp
78 lines (70 loc) · 1.85 KB
/
day_10b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <stack>
#include <vector>
int main(int argc, char * argv[]) {
std::string input = "../input/day_10_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
const auto is_open_b = [](const auto c) {
return c == '(' ||
c == '[' ||
c == '{' ||
c == '<';
};
const auto match = [](const auto c1, const auto c2) {
if (c1 == '(') return c2 == ')';
else if(c1 == '[') return c2 == ']';
else if(c1 == '{') return c2 == '}';
else if(c1 == '<') return c2 == '>';
return false;
};
const auto get_match = [](const auto c) {
if (c == '(') return ')';
else if(c == '[') return ']';
else if(c == '{') return '}';
else if(c == '<') return '>';
return ' ';
};
const auto lookup_score = [&](const char c) {
if (c == ')') return 1;
else if (c == ']') return 2;
else if (c == '}') return 3;
else if (c == '>') return 4;
else return 0;
};
std::vector<long long> auto_complete_scores;
while(std::getline(file, line)) {
std::stack<char> s;
bool corrupt = false;
for (const auto c : line) {
if (is_open_b(c)) {
s.push(c);
} else {
if (!match(s.top(), c)) {
corrupt = true;
break;
} else {
s.pop();
}
}
}
if (!corrupt) {
long long auto_complete_score = 0;
while(!s.empty()) {
const char c = s.top();
s.pop();
auto_complete_score = auto_complete_score * 5 + lookup_score(get_match(c));
}
auto_complete_scores.push_back(auto_complete_score);
}
}
std::sort(std::begin(auto_complete_scores), std::end(auto_complete_scores));
std::cout << auto_complete_scores[auto_complete_scores.size()/2] << '\n';
return 0;
}