diff options
Diffstat (limited to '2024/src/day19.cpp')
| -rw-r--r-- | 2024/src/day19.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/2024/src/day19.cpp b/2024/src/day19.cpp new file mode 100644 index 0000000..43995b1 --- /dev/null +++ b/2024/src/day19.cpp | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <numeric> | ||
| 5 | #include <set> | ||
| 6 | #include <string> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | vector<string> | ||
| 12 | split(string_view line, string_view delimiter) | ||
| 13 | { | ||
| 14 | size_t pos_start = 0; | ||
| 15 | size_t pos_end = 0; | ||
| 16 | |||
| 17 | vector<string> res; | ||
| 18 | |||
| 19 | while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { | ||
| 20 | auto token = line.substr(pos_start, pos_end - pos_start); | ||
| 21 | pos_start = pos_end + delimiter.length(); | ||
| 22 | |||
| 23 | res.emplace_back(token); | ||
| 24 | } | ||
| 25 | |||
| 26 | res.emplace_back(line.substr(pos_start)); | ||
| 27 | return res; | ||
| 28 | } | ||
| 29 | |||
| 30 | tuple<set<string>, vector<string>> | ||
| 31 | read_file(string_view filename) | ||
| 32 | { | ||
| 33 | fstream input{ filename }; | ||
| 34 | |||
| 35 | vector<string> lines; | ||
| 36 | for ( string line; getline(input, line); ) { | ||
| 37 | lines.emplace_back(line); | ||
| 38 | } | ||
| 39 | |||
| 40 | const auto parts = split(lines[0], ", "); | ||
| 41 | |||
| 42 | return { { parts.begin(), parts.end() }, { lines.begin() + 2, lines.end() } }; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | part1(const tuple<set<string>, vector<string>>& data) | ||
| 47 | { | ||
| 48 | const auto& parts = get<0>(data); | ||
| 49 | const auto& lines = get<1>(data); | ||
| 50 | |||
| 51 | map<string, bool> cache; | ||
| 52 | |||
| 53 | function<bool(const string&)> match = [&](const string& line) { | ||
| 54 | if ( line.empty() ) { | ||
| 55 | return true; | ||
| 56 | } | ||
| 57 | |||
| 58 | if ( cache.contains(line) ) { | ||
| 59 | return cache.at(line); | ||
| 60 | } | ||
| 61 | |||
| 62 | for ( size_t i = 0; i != line.size(); ++i ) { | ||
| 63 | if ( parts.contains(line.substr(0, i + 1)) ) { | ||
| 64 | if ( match(line.substr(i + 1)) ) { | ||
| 65 | return cache[line] = true; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | return cache[line] = false; | ||
| 71 | }; | ||
| 72 | |||
| 73 | cout << ranges::count_if(lines, match) << endl; | ||
| 74 | } | ||
| 75 | |||
| 76 | void | ||
| 77 | part2(const tuple<set<string>, vector<string>>& data) | ||
| 78 | { | ||
| 79 | const auto& parts = get<0>(data); | ||
| 80 | const auto& lines = get<1>(data); | ||
| 81 | |||
| 82 | map<string, long> cache; | ||
| 83 | |||
| 84 | function<long(const string&)> match = [&](const string& line) -> long { | ||
| 85 | if ( line.empty() ) { | ||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | if ( cache.contains(line) ) { | ||
| 90 | return cache.at(line); | ||
| 91 | } | ||
| 92 | |||
| 93 | long counter = 0; | ||
| 94 | for ( size_t i = 0; i != line.size(); ++i ) { | ||
| 95 | auto lhs = line.substr(0, i + 1); | ||
| 96 | auto rhs = line.substr(i + 1); | ||
| 97 | |||
| 98 | if ( parts.contains(lhs) ) { | ||
| 99 | counter += match(rhs); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | return cache[line] = counter; | ||
| 104 | }; | ||
| 105 | |||
| 106 | cout << accumulate(lines.begin(), lines.end(), 0L, [&](auto init, const auto& line) { return init + match(line); }) << endl; | ||
| 107 | } | ||
| 108 | |||
| 109 | int | ||
| 110 | main() | ||
| 111 | { | ||
| 112 | const auto data = read_file("data/day19.txt"); | ||
| 113 | part1(data); | ||
| 114 | part2(data); | ||
| 115 | } | ||
