-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.1.cpp
54 lines (45 loc) · 1.18 KB
/
day19.1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cmath>
#include <iostream>
#include <fstream>
#include <queue>
#include <string>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
bool back(string& want, unordered_set<string>& avail, unordered_set<int>& v, int i) {
if (v.contains(i)) return false;
v.insert(i);
if (i == want.size()) return true;
for (int len = 1;len <= want.size() - i;len++) {
string part = want.substr(i, len);
if (!avail.contains(part)) continue;
if (back(want, avail, v, i + len)) return true;
}
return false;
}
int main(int argc, char* argv[]) {
ifstream file(argv[1]);
unordered_set<string> avail;
string line;
getline(file, line);
for (int i = 0;i < line.size();) {
int next = line.find(", ", i);
string design = line.substr(i, next-i);
avail.insert(design);
if (next == string::npos) break;
i = next + 2;
}
int res = 0;
getline(file, line); // Empty line
while (getline(file, line)) {
unordered_set<int> v;
res += back(line, avail, v, 0);
}
cout << res << endl;
return 0;
}
/*
*/