diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-19 07:21:47 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-19 07:21:47 +0100 |
| commit | 3c8331701f9f77c28847d678b935d5efb9a5d575 (patch) | |
| tree | fbcc7d334c3879fa2b7ecf65acec46ecc4b484bd | |
| parent | 509f58966da1c4da1b532fc5a2e271eeae7afd10 (diff) | |
| download | aoc-3c8331701f9f77c28847d678b935d5efb9a5d575.tar.gz aoc-3c8331701f9f77c28847d678b935d5efb9a5d575.zip | |
Improved part 2 day 19 2024 with string_view
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | 2024/19/day19b-string.cpp | 54 | ||||
| -rw-r--r-- | 2024/19/day19b.cpp | 29 | ||||
| -rw-r--r-- | 2024/learned.txt | 1 |
4 files changed, 70 insertions, 16 deletions
| @@ -1,3 +1,3 @@ | |||
| 1 | */*/*.out | 1 | */*/*.out |
| 2 | */*/*.txt | 2 | */*/*.txt |
| 3 | */*/.swp | 3 | */*/*.swp |
diff --git a/2024/19/day19b-string.cpp b/2024/19/day19b-string.cpp new file mode 100644 index 0000000..6f8b8f4 --- /dev/null +++ b/2024/19/day19b-string.cpp | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdint> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <queue> | ||
| 6 | #include <ranges> | ||
| 7 | #include <set> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | vector<string> gettowels(const string& line) { | ||
| 15 | vector<string> ret; | ||
| 16 | int j = 0; | ||
| 17 | for (int i = 0; j != -1; i = j+2) { | ||
| 18 | j = line.find(",", i); | ||
| 19 | ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i)); | ||
| 20 | } | ||
| 21 | return ret; | ||
| 22 | } | ||
| 23 | |||
| 24 | int64_t countpossible(const string& p, const vector<string>& towels, vector<int64_t>& mem) { | ||
| 25 | if (p.size() == 0) | ||
| 26 | return 1; | ||
| 27 | if (mem[p.size()] != -1) | ||
| 28 | return mem[p.size()]; | ||
| 29 | int64_t ret = 0; | ||
| 30 | for (auto t : towels) { | ||
| 31 | if (t == p.substr(0, t.size())) | ||
| 32 | ret += countpossible(p.substr( | ||
| 33 | t.size(), p.size() - t.size()), towels, mem); | ||
| 34 | } | ||
| 35 | return (mem[p.size()] = ret); | ||
| 36 | } | ||
| 37 | |||
| 38 | int main() { | ||
| 39 | string line; | ||
| 40 | getline(cin, line); | ||
| 41 | auto towels = gettowels(line); | ||
| 42 | getline(cin, line); | ||
| 43 | vector<string> patterns; | ||
| 44 | while (getline(cin, line)) | ||
| 45 | patterns.push_back(line); | ||
| 46 | vector<int64_t> mem(1000); | ||
| 47 | int64_t count = 0; | ||
| 48 | for (auto p : patterns) { | ||
| 49 | for (auto& m : mem) m = -1; | ||
| 50 | count += countpossible(p, towels, mem); | ||
| 51 | } | ||
| 52 | cout << count << endl; | ||
| 53 | return 0; | ||
| 54 | } | ||
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; |
diff --git a/2024/learned.txt b/2024/learned.txt index 440571a..ba00c6c 100644 --- a/2024/learned.txt +++ b/2024/learned.txt | |||
| @@ -9,3 +9,4 @@ List of things I learned (or refreshed) with this year's AoC. | |||
| 9 | * Day 8: set::insert_range(), but it is from C++23 only | 9 | * Day 8: set::insert_range(), but it is from C++23 only |
| 10 | * Day 9: std::views | 10 | * Day 9: std::views |
| 11 | * Day 11: if(init; cond) | 11 | * Day 11: if(init; cond) |
| 12 | * Day 19: more practice with std::string_view for constant string | ||
