diff options
Diffstat (limited to '2024/src')
| -rw-r--r-- | 2024/src/day19-alt.cpp | 112 | ||||
| -rw-r--r-- | 2024/src/day19.cpp | 7 |
2 files changed, 114 insertions, 5 deletions
diff --git a/2024/src/day19-alt.cpp b/2024/src/day19-alt.cpp new file mode 100644 index 0000000..77c03db --- /dev/null +++ b/2024/src/day19-alt.cpp | |||
| @@ -0,0 +1,112 @@ | |||
| 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 ( const auto& part: parts ) { | ||
| 63 | if ( line.starts_with(part) ) { | ||
| 64 | if ( match(line.substr(part.size())) ) { | ||
| 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 ( const auto& part: parts ) { | ||
| 95 | if ( line.starts_with(part) ) { | ||
| 96 | counter += match(line.substr(part.size())); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return cache[line] = counter; | ||
| 101 | }; | ||
| 102 | |||
| 103 | cout << accumulate(lines.begin(), lines.end(), 0L, [&](auto init, const auto& line) { return init + match(line); }) << endl; | ||
| 104 | } | ||
| 105 | |||
| 106 | int | ||
| 107 | main() | ||
| 108 | { | ||
| 109 | const auto data = read_file("data/day19.txt"); | ||
| 110 | part1(data); | ||
| 111 | part2(data); | ||
| 112 | } | ||
diff --git a/2024/src/day19.cpp b/2024/src/day19.cpp index 43995b1..c114d2a 100644 --- a/2024/src/day19.cpp +++ b/2024/src/day19.cpp | |||
| @@ -92,11 +92,8 @@ part2(const tuple<set<string>, vector<string>>& data) | |||
| 92 | 92 | ||
| 93 | long counter = 0; | 93 | long counter = 0; |
| 94 | for ( size_t i = 0; i != line.size(); ++i ) { | 94 | for ( size_t i = 0; i != line.size(); ++i ) { |
| 95 | auto lhs = line.substr(0, i + 1); | 95 | if ( parts.contains(line.substr(0, i + 1)) ) { |
| 96 | auto rhs = line.substr(i + 1); | 96 | counter += match(line.substr(i + 1)); |
| 97 | |||
| 98 | if ( parts.contains(lhs) ) { | ||
| 99 | counter += match(rhs); | ||
| 100 | } | 97 | } |
| 101 | } | 98 | } |
| 102 | 99 | ||
