-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday9.cc
55 lines (45 loc) · 1.41 KB
/
day9.cc
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
// Advent of Code 2017 day 9 solution
// Peter Kasting, Dec. 8, 2017
#include <iostream>
#include <string>
#include <tuple>
namespace {
constexpr bool kPart1 = false; // Use true for part 1, false for part 2.
// Counts the score of the groups in the stream and the number of total garbage
// characters. We need the former for part 1 and the latter for part 2.
std::tuple<int, int> ComputeScores(const std::string& stream) {
int total_score = 0;
int group_score = 0;
int garbage_chars = 0;
bool in_garbage = false;
for (auto i = stream.begin(); i != stream.end(); ++i) {
const char c = *i;
// Which checks come before/after the |in_garbage| check is important for
// precedence reasons.
if (c == '!')
++i;
else if (c == '>')
in_garbage = false;
else if (in_garbage)
++garbage_chars;
else if (c == '{')
++group_score;
else if (c == '}')
total_score += group_score--;
else if (c == '<')
in_garbage = true;
}
return {total_score, garbage_chars};
}
} // namespace
int main(int argc, char* argv[]) {
std::cout << "Enter stream: ";
std::string stream;
std::cin >> stream;
const auto scores = ComputeScores(stream);
if (kPart1)
std::cout << "Score: " << std::get<0>(scores) << std::endl;
else
std::cout << "Garbage characters: " << std::get<1>(scores) << std::endl;
return 0;
}