diff options
Diffstat (limited to '2016/src/day04.cpp')
| -rw-r--r-- | 2016/src/day04.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/2016/src/day04.cpp b/2016/src/day04.cpp new file mode 100644 index 0000000..713b16f --- /dev/null +++ b/2016/src/day04.cpp | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <regex> | ||
| 5 | #include <set> | ||
| 6 | #include <sstream> | ||
| 7 | #include <string> | ||
| 8 | #include <tuple> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | using namespace std; | ||
| 12 | |||
| 13 | vector<string> | ||
| 14 | split(const string& line, const regex& sep) | ||
| 15 | { | ||
| 16 | return { sregex_token_iterator(line.begin(), line.end(), sep, -1), {} }; | ||
| 17 | } | ||
| 18 | |||
| 19 | vector<tuple<vector<string>, long, string>> | ||
| 20 | read_file(string_view filename) | ||
| 21 | { | ||
| 22 | fstream input{ filename }; | ||
| 23 | |||
| 24 | vector<tuple<vector<string>, long, string>> result; | ||
| 25 | |||
| 26 | for ( string line; getline(input, line); ) { | ||
| 27 | static const regex sep{ "[\\[\\]-]" }; | ||
| 28 | |||
| 29 | auto parts = split(line, sep); | ||
| 30 | |||
| 31 | auto checksum = parts.at(parts.size() - 1); | ||
| 32 | parts.pop_back(); | ||
| 33 | |||
| 34 | auto selector = stol(parts.at(parts.size() - 1)); | ||
| 35 | parts.pop_back(); | ||
| 36 | |||
| 37 | result.emplace_back(parts, selector, checksum); | ||
| 38 | } | ||
| 39 | |||
| 40 | return result; | ||
| 41 | } | ||
| 42 | |||
| 43 | void | ||
| 44 | part1(const vector<tuple<vector<string>, long, string>>& data) | ||
| 45 | { | ||
| 46 | long sum = 0; | ||
| 47 | for ( const auto& [ids, selector, checksum]: data ) { | ||
| 48 | map<char, long> counts; | ||
| 49 | for ( const auto& id: ids ) { | ||
| 50 | for ( const auto chr: id ) { | ||
| 51 | ++counts[chr]; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | set<tuple<long, char>> tops; | ||
| 56 | for ( const auto& [chr, count]: counts ) { | ||
| 57 | tops.emplace(-count, chr); | ||
| 58 | } | ||
| 59 | |||
| 60 | string result; | ||
| 61 | for ( const auto& [count, chr]: tops ) { | ||
| 62 | result += chr; | ||
| 63 | } | ||
| 64 | |||
| 65 | if ( result.starts_with(checksum) ) { | ||
| 66 | sum += selector; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | cout << sum << endl; | ||
| 70 | } | ||
| 71 | |||
| 72 | void | ||
| 73 | part2(const vector<tuple<vector<string>, long, string>>& lines) | ||
| 74 | { | ||
| 75 | auto rot = [](string str, int n) { | ||
| 76 | for ( auto& chr: str ) { | ||
| 77 | chr = ((chr - 'a' + n) % 26) + 'a'; | ||
| 78 | } | ||
| 79 | return str; | ||
| 80 | }; | ||
| 81 | |||
| 82 | auto is_northpole = [&](const string& str, int n) { | ||
| 83 | return rot(str, n) == "northpole"; | ||
| 84 | }; | ||
| 85 | |||
| 86 | for ( const auto& line: lines ) { | ||
| 87 | const auto& ids = get<0>(line); | ||
| 88 | const auto selector = get<1>(line); | ||
| 89 | if ( ranges::any_of(ids, [&](auto& str) { return is_northpole(str, (int) selector); }) ) { | ||
| 90 | cout << selector << endl; | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | int | ||
| 97 | main() | ||
| 98 | { | ||
| 99 | auto data = read_file("data/day04.txt"); | ||
| 100 | part1(data); | ||
| 101 | part2(data); | ||
| 102 | } | ||
