diff --git a/day 19/day_19_felix.cpp b/day 19/day_19_felix.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d02840b993398771decf8732433021089829a626 --- /dev/null +++ b/day 19/day_19_felix.cpp @@ -0,0 +1,93 @@ +/* +Usage: + g++ -o main day_X_felix.cpp (or compile however you like) + ./main < input.txt (pipe the input) + + Options: + -d : debug mode +*/ + +#include <bits/stdc++.h> +using namespace std; +bool DEBUG = false; + +// Standard definitions for many problems +#define coord pair <int, int> +#define ll long long + +set<string> TOWELS; +vector<string> DESIGNS; +int MAX_TOWEL_SIZE = 0; + +void parse_input() { + string word; + bool reading_towels = true; + while (cin >> word) { + if (reading_towels && word.at(word.size() - 1) == ','){ + TOWELS.insert(word.substr(0, word.size() - 1)); + if (word.size() - 1 > MAX_TOWEL_SIZE) MAX_TOWEL_SIZE = word.size() - 1; + if (DEBUG) cout << "Towel: " << word.substr(0, word.size() - 1) << endl; + } else if (reading_towels) { + TOWELS.insert(word); + reading_towels = false; + if (word.size() > MAX_TOWEL_SIZE) MAX_TOWEL_SIZE = word.size(); + if (DEBUG) cout << "Towel: " << word << endl; + } else { + DESIGNS.push_back(word); + if (DEBUG) cout << "Design: " << word << endl; + } + } +} + +// cache +map<string, ll> ways_of_substring; + +ll ways_to_make(string towel) { + if (ways_of_substring.count(towel)) return ways_of_substring[towel]; + if (towel.size() == 0) return 1; + + ll ways = 0; + for (string sub_towel : TOWELS) if (towel.substr(0, sub_towel.size()) == sub_towel) { + if (DEBUG) cout << towel << " starts with " << sub_towel << endl; + ways += ways_to_make(towel.substr(sub_towel.size())); + } + ways_of_substring[towel] = ways; + + return ways; +} + +pair<ll, ll> parts() { + ll count = 0; + ll total_ways = 0; + + int n_checked = 0; + for (string design : DESIGNS){ + if (DEBUG) cout << "=======================================" << endl; + if (DEBUG) cout << "Testing design " << design << endl; + + ways_of_substring.clear(); + + ll ways = ways_to_make(design); + if (ways) count++; + total_ways += ways; + + n_checked++; + if (n_checked % 5 == 0) cout << "Checked " << n_checked << " designs" << endl; + } + return {count, total_ways}; +} + +int main (int argc, char* argv[]) { + for (int i = 0; i < argc; i++) { + if (string(argv[i]) == "-d") DEBUG = true; + } + if (DEBUG) cout << "Running in DEBUG mode" << endl; + + parse_input(); + + pair<ll, ll> p = parts(); + cout << "Part 1: " << p.first << endl; + cout << "Part 2: " << p.second << endl; + + return 0; +} \ No newline at end of file