aboutsummaryrefslogtreecommitdiff
path: root/2024/19/day19a.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2024/19/day19a.cpp')
-rw-r--r--2024/19/day19a.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/2024/19/day19a.cpp b/2024/19/day19a.cpp
new file mode 100644
index 0000000..28fb684
--- /dev/null
+++ b/2024/19/day19a.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>
12using namespace std;
13
14vector<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
24bool ispossible(const string& p, const vector<string>& towels, vector<int>& mem) {
25 if (p.size() == 0)
26 return true;
27 if (mem[p.size()] != -1)
28 return mem[p.size()];
29 for (auto t : towels) {
30 if (t == p.substr(0, t.size()))
31 if((mem[p.size()] = ispossible(p.substr(
32 t.size(), p.size() - t.size()), towels, mem)))
33 return true;
34 }
35 return (mem[p.size()] = false);
36}
37
38int 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<int> mem(1000);
47 int count = 0;
48 for (auto p : patterns) {
49 for (auto& m : mem) m = -1;
50 count += ispossible(p, towels, mem);
51 }
52 cout << count << endl;
53 return 0;
54}

Generated with cgit - Back to sebastiano.tronto.net