aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day09.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2017/src/day09.cpp')
-rw-r--r--2017/src/day09.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/2017/src/day09.cpp b/2017/src/day09.cpp
new file mode 100644
index 0000000..60b0d3b
--- /dev/null
+++ b/2017/src/day09.cpp
@@ -0,0 +1,75 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <iterator>
5#include <string>
6
7using namespace std;
8
9namespace {
10
11string
12remove_garbage(string_view line, int& removed)
13{
14 string result;
15
16 removed = 0;
17 for ( size_t pos = 0; pos < line.size(); ) {
18 if ( line.at(pos) == '<' ) {
19 ++pos;
20 while ( pos < line.size() && line.at(pos) != '>' ) {
21 if ( line.at(pos) == '!' ) {
22 pos += 2;
23 }
24 else {
25 ++pos;
26 ++removed;
27 }
28 }
29 ++pos;
30 }
31 else {
32 result += line.at(pos);
33 ++pos;
34 }
35 }
36
37 return result;
38}
39
40string
41read_file(const filesystem::path& filename)
42{
43 ifstream file{ filename };
44 return { istreambuf_iterator<char>{ file }, {} };
45}
46
47void
48solve(string_view data)
49{
50 int score = 0;
51 int depth = 0;
52 int removed = 0;
53
54 for ( const auto chr: remove_garbage(data, removed) ) {
55 if ( chr == '{' ) {
56 ++depth;
57 }
58 else if ( chr == '}' ) {
59 score += depth;
60 --depth;
61 }
62 }
63
64 cout << "Part1: " << score << '\n';
65 cout << "Part2: " << removed << '\n';
66}
67
68} // namespace
69
70int
71main()
72{
73 auto data = read_file("data/day09.txt");
74 solve(data);
75}