From 2af5aa7121e8528d3d7dadccf7df495c7fd77b0d Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Wed, 18 Dec 2024 21:41:33 -0800 Subject: [PATCH] 2024 day 19 --- 2024/19/main-1.cpp | 41 +++++++++++++++++++++++++++++++++++ 2024/19/main-2.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 2024/19/main-1.cpp create mode 100644 2024/19/main-2.cpp diff --git a/2024/19/main-1.cpp b/2024/19/main-1.cpp new file mode 100644 index 0000000..8cb4d10 --- /dev/null +++ b/2024/19/main-1.cpp @@ -0,0 +1,41 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +vector towels; + +bool rec(string_view s) { + if (s.empty()) { + return true; + } + for (auto &t : towels) { + if (s.starts_with(t) && rec(s.substr(t.size()))) { + return true; + } + } + return false; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + getline(cin, line); + istringstream ll(line); + string s; + while (getline(ll, s, ',')) { + towels.push_back(s); + ll.ignore(); + } + + cin.ignore(); + ull count = 0; + while (getline(cin, line)) { + if (rec(line)) { + count++; + } + } + + cout << count << '\n'; +} diff --git a/2024/19/main-2.cpp b/2024/19/main-2.cpp new file mode 100644 index 0000000..4eca9dc --- /dev/null +++ b/2024/19/main-2.cpp @@ -0,0 +1,53 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +unordered_map m; +vector towels; +vector designs; + +ull rec(string_view s) { + if (s.empty()) { + return 1; + } + if (m.contains(s)) { + return m[s]; + } + ull c = 0; + for (auto &t : towels) { + if (s.starts_with(t)) { + c += rec(s.substr(t.size())); + } + } + m[s] = c; + return c; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + getline(cin, line); + istringstream ll(line); + string s; + while (getline(ll, s, ',')) { + towels.push_back(s); + ll.ignore(); + } + + cin.ignore(); + while (getline(cin, line)) { + designs.push_back(line); + } + + // NOTE: designs gets their own global vector to make sure that the design + // strings live for as long as the memo does (it happened to work on the + // full data the first try without it but don't rely on that) + ull count = 0; + for (string &design : designs) { + count += rec(design); + } + + cout << count << '\n'; +}