-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_19a.cpp
64 lines (61 loc) · 1.97 KB
/
day_19a.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
55
56
57
58
59
60
61
62
63
64
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
// Remove primitives that can be created with smaller primitives to reduce the set of primitives
bool check_if_buildable(const std::string s, const std::unordered_set<std::string_view>& primitives, const int depth, int max_size) {
if (s.empty()) return true;
for (int i = max_size; i >= 1; i--) {
if (s.size() < i) continue;
if (primitives.contains(s.substr(0,i))) {
if(check_if_buildable(s.substr(i, s.size() - i), primitives, depth + 1, max_size)) {
return true;
}
}
}
return false;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_19_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<std::string> primitives;
std::unordered_set<std::string_view> primitives_set;
std::getline(file, line);
std::size_t start = 0;
std::size_t end = line.find(", ");
std::size_t s = 0;
while(end != std::string::npos) {
primitives.push_back(line.substr(start, end - start));
s = std::max(s,primitives.back().size());
start = end + 2;
end = line.find(", ", start);
}
primitives.push_back(line.substr(start, end - start));
for (const auto& ele : primitives) {
primitives_set.insert(ele);
}
std::unordered_set<std::string_view> to_remove;
for (const auto& p : primitives) {
if(check_if_buildable(p, primitives_set, 0, p.size() - 1)) {
to_remove.insert(p);
}
}
for (const auto& p : to_remove) {
primitives_set.erase(p);
}
std::getline(file, line);
int count = 0;
while(std::getline(file,line)) {
const auto buildable = check_if_buildable(line, primitives_set, 0, s);
if (buildable) count++;
}
std::cout << count << '\n';;
return 0;
}