diff options
Diffstat (limited to '2024/19/day19b.cpp')
| -rw-r--r-- | 2024/19/day19b.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/2024/19/day19b.cpp b/2024/19/day19b.cpp index 6f8b8f4..dd6055d 100644 --- a/2024/19/day19b.cpp +++ b/2024/19/day19b.cpp | |||
| @@ -11,28 +11,27 @@ | |||
| 11 | #include <vector> | 11 | #include <vector> |
| 12 | using namespace std; | 12 | using namespace std; |
| 13 | 13 | ||
| 14 | vector<string> gettowels(const string& line) { | 14 | vector<string_view> gettowels(const string_view& line) { |
| 15 | vector<string> ret; | 15 | vector<string_view> ret; |
| 16 | int j = 0; | 16 | unsigned sz = line.size(); |
| 17 | for (int i = 0; j != -1; i = j+2) { | 17 | for (int i = 0, j = 0; j != -1; i = j+2) { |
| 18 | j = line.find(",", i); | 18 | j = line.find(",", i); |
| 19 | ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i)); | 19 | ret.push_back(line.substr(i, (j == -1 ? sz : j) - i)); |
| 20 | } | 20 | } |
| 21 | return ret; | 21 | return ret; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | int64_t countpossible(const string& p, const vector<string>& towels, vector<int64_t>& mem) { | 24 | int64_t f(const string_view& p, const vector<string_view>& towels, vector<int64_t>& mem) { |
| 25 | if (p.size() == 0) | 25 | unsigned lp = p.size(); |
| 26 | return 1; | 26 | if (lp == 0) return 1; |
| 27 | if (mem[p.size()] != -1) | 27 | if (mem[lp] != -1) return mem[lp]; |
| 28 | return mem[p.size()]; | ||
| 29 | int64_t ret = 0; | 28 | int64_t ret = 0; |
| 30 | for (auto t : towels) { | 29 | for (auto t : towels) { |
| 31 | if (t == p.substr(0, t.size())) | 30 | unsigned lt = t.size(); |
| 32 | ret += countpossible(p.substr( | 31 | if (t == p.substr(0, lt)) |
| 33 | t.size(), p.size() - t.size()), towels, mem); | 32 | ret += f(p.substr(lt, lp - lt), towels, mem); |
| 34 | } | 33 | } |
| 35 | return (mem[p.size()] = ret); | 34 | return (mem[lp] = ret); |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | int main() { | 37 | int main() { |
| @@ -47,7 +46,7 @@ int main() { | |||
| 47 | int64_t count = 0; | 46 | int64_t count = 0; |
| 48 | for (auto p : patterns) { | 47 | for (auto p : patterns) { |
| 49 | for (auto& m : mem) m = -1; | 48 | for (auto& m : mem) m = -1; |
| 50 | count += countpossible(p, towels, mem); | 49 | count += f(p, towels, mem); |
| 51 | } | 50 | } |
| 52 | cout << count << endl; | 51 | cout << count << endl; |
| 53 | return 0; | 52 | return 0; |
