diff options
Diffstat (limited to '2015/src/day08.cpp')
| -rw-r--r-- | 2015/src/day08.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/2015/src/day08.cpp b/2015/src/day08.cpp new file mode 100644 index 0000000..9cd6e21 --- /dev/null +++ b/2015/src/day08.cpp | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <string> | ||
| 4 | #include <vector> | ||
| 5 | |||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | auto | ||
| 9 | read_file(string_view filename) | ||
| 10 | { | ||
| 11 | fstream input{ filename }; | ||
| 12 | vector<string> lines; | ||
| 13 | |||
| 14 | for ( string line; getline(input, line); ) { | ||
| 15 | lines.emplace_back(line); | ||
| 16 | } | ||
| 17 | |||
| 18 | return lines; | ||
| 19 | } | ||
| 20 | |||
| 21 | void | ||
| 22 | part1(const vector<string>& lines) | ||
| 23 | { | ||
| 24 | auto count = [](const string& line) -> size_t { | ||
| 25 | size_t length = 0; | ||
| 26 | auto iter = begin(line); | ||
| 27 | |||
| 28 | while ( iter != end(line) ) { | ||
| 29 | if ( *iter == '\\' ) { | ||
| 30 | ++iter; | ||
| 31 | if ( *iter == '\\' || *iter == '"' ) { | ||
| 32 | ++length; | ||
| 33 | ++iter; | ||
| 34 | } | ||
| 35 | else if ( *iter == 'x' ) { | ||
| 36 | ++length; | ||
| 37 | iter += 3; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | ++iter; | ||
| 42 | ++length; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | return length - 2; | ||
| 46 | }; | ||
| 47 | |||
| 48 | size_t sum = 0; | ||
| 49 | for ( const auto& line: lines ) { | ||
| 50 | sum += line.size() - count(line); | ||
| 51 | } | ||
| 52 | cout << sum << endl; | ||
| 53 | } | ||
| 54 | |||
| 55 | void | ||
| 56 | part2(const vector<string>& lines) | ||
| 57 | { | ||
| 58 | long sum = 0; | ||
| 59 | for ( const auto& line: lines ) { | ||
| 60 | sum += 2 + count_if(begin(line), end(line), [](auto chr) { return chr == '\\' || chr == '"'; }); | ||
| 61 | } | ||
| 62 | cout << sum << endl; | ||
| 63 | } | ||
| 64 | |||
| 65 | int | ||
| 66 | main() | ||
| 67 | { | ||
| 68 | auto lines = read_file("data/day08.txt"); | ||
| 69 | part1(lines); | ||
| 70 | part2(lines); | ||
| 71 | } | ||
