diff options
Diffstat (limited to '2016/src/day07.cpp')
| -rw-r--r-- | 2016/src/day07.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/2016/src/day07.cpp b/2016/src/day07.cpp new file mode 100644 index 0000000..faa306f --- /dev/null +++ b/2016/src/day07.cpp | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <set> | ||
| 4 | #include <sstream> | ||
| 5 | #include <string> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<string> | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | |||
| 15 | vector<string> lines; | ||
| 16 | |||
| 17 | for ( string line; getline(input, line); ) { | ||
| 18 | lines.emplace_back(line); | ||
| 19 | } | ||
| 20 | |||
| 21 | return lines; | ||
| 22 | } | ||
| 23 | |||
| 24 | void | ||
| 25 | part1(const vector<string>& lines) | ||
| 26 | { | ||
| 27 | long sum = 0; | ||
| 28 | for ( const auto& line: lines ) { | ||
| 29 | long found_inner = 0; | ||
| 30 | long found_outer = 0; | ||
| 31 | |||
| 32 | bool inner = false; | ||
| 33 | for ( size_t i = 0; i + 3 < line.length(); ++i ) { | ||
| 34 | if ( line[i] == '[' ) { | ||
| 35 | inner = true; | ||
| 36 | } | ||
| 37 | if ( line[i] == ']' ) { | ||
| 38 | inner = false; | ||
| 39 | } | ||
| 40 | |||
| 41 | if ( line[i + 0] == line[i + 3] && line[i + 0] != line[i + 1] && line[i + 1] == line[i + 2] ) { | ||
| 42 | if ( inner ) { | ||
| 43 | ++found_inner; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | ++found_outer; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | sum += (found_inner == 0 && found_outer != 0) ? 1 : 0; | ||
| 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 | set<tuple<char, char>> inners; | ||
| 61 | set<tuple<char, char>> outers; | ||
| 62 | |||
| 63 | bool inner = false; | ||
| 64 | for ( size_t i = 0; i + 2 < line.length(); ++i ) { | ||
| 65 | if ( line[i] == '[' ) { | ||
| 66 | inner = true; | ||
| 67 | } | ||
| 68 | if ( line[i] == ']' ) { | ||
| 69 | inner = false; | ||
| 70 | } | ||
| 71 | |||
| 72 | if ( line[i + 0] == line[i + 2] && line[i + 0] != line[i + 1] ) { | ||
| 73 | if ( inner ) { | ||
| 74 | inners.emplace(line[i + 0], line[i + 1]); | ||
| 75 | if ( outers.contains({ line[i + 1], line[i + 0] }) ) { | ||
| 76 | ++sum; | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | outers.emplace(line[i + 0], line[i + 1]); | ||
| 82 | if ( inners.contains({ line[i + 1], line[i + 0] }) ) { | ||
| 83 | ++sum; | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | cout << sum << endl; | ||
| 91 | } | ||
| 92 | |||
| 93 | int | ||
| 94 | main() | ||
| 95 | { | ||
| 96 | auto lines = read_file("data/day07.txt"); | ||
| 97 | part1(lines); | ||
| 98 | part2(lines); | ||
| 99 | } | ||
