-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10a.cpp
61 lines (54 loc) · 1.29 KB
/
day_10a.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
#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 lookup_score = [&](const char c) {
if (c == ')') return 3;
else if (c == ']') return 57;
else if (c == '}') return 1197;
else if (c == '>') return 25137;
else return 0;
};
long long syntax_score = 0;
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;
syntax_score += lookup_score(c);
break;
} else {
s.pop();
}
}
}
}
std::cout << syntax_score << '\n';
return 0;
}