2024 day 19

This commit is contained in:
eriedaberrie 2024-12-18 21:41:33 -08:00
parent 8834ee5feb
commit 2af5aa7121
2 changed files with 94 additions and 0 deletions

41
2024/19/main-1.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
vector<string> 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';
}

53
2024/19/main-2.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
unordered_map<string_view, ull> m;
vector<string> towels;
vector<string> 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';
}