blob: 60b0d3b5381c1cbe474b8c32651a7b9bd570683e (
plain)
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
|
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
namespace {
string
remove_garbage(string_view line, int& removed)
{
string result;
removed = 0;
for ( size_t pos = 0; pos < line.size(); ) {
if ( line.at(pos) == '<' ) {
++pos;
while ( pos < line.size() && line.at(pos) != '>' ) {
if ( line.at(pos) == '!' ) {
pos += 2;
}
else {
++pos;
++removed;
}
}
++pos;
}
else {
result += line.at(pos);
++pos;
}
}
return result;
}
string
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
return { istreambuf_iterator<char>{ file }, {} };
}
void
solve(string_view data)
{
int score = 0;
int depth = 0;
int removed = 0;
for ( const auto chr: remove_garbage(data, removed) ) {
if ( chr == '{' ) {
++depth;
}
else if ( chr == '}' ) {
score += depth;
--depth;
}
}
cout << "Part1: " << score << '\n';
cout << "Part2: " << removed << '\n';
}
} // namespace
int
main()
{
auto data = read_file("data/day09.txt");
solve(data);
}
|