2024 day 19
This commit is contained in:
parent
8834ee5feb
commit
cf08978c4e
40
2024/19/main-1.cpp
Normal file
40
2024/19/main-1.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#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 << endl;
|
||||
}
|
52
2024/19/main-2.cpp
Normal file
52
2024/19/main-2.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#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 << endl;
|
||||
}
|
Loading…
Reference in a new issue