diff --git a/2024/23/main-1.cpp b/2024/23/main-1.cpp new file mode 100644 index 0000000..3966850 --- /dev/null +++ b/2024/23/main-1.cpp @@ -0,0 +1,39 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + unordered_set all; + unordered_map> m; + + string line; + while (getline(cin, line)) { + string a = line.substr(0, 2); + string b = line.substr(3, 5); + all.insert(a); + all.insert(b); + m[a].insert(b); + m[b].insert(a); + } + + int count = 0; + vector vm; + vm.assign(all.begin(), all.end()); + for (int i = 2; i < vm.size(); i++) { + for (int j = 1; j < i; j++) { + for (int k = 0; k < j; k++) { + const string &a = vm[i], &b = vm[j], &c = vm[k]; + if (a[0] != 't' && b[0] != 't' && c[0] != 't') { + continue; + } + if (m[a].contains(b) && m[b].contains(c) && m[c].contains(a)) { + count++; + } + } + } + } + cout << count << '\n'; +} diff --git a/2024/23/main-2.cpp b/2024/23/main-2.cpp new file mode 100644 index 0000000..d867b84 --- /dev/null +++ b/2024/23/main-2.cpp @@ -0,0 +1,72 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +typedef vector vs; + +unordered_map> m; +vector found; + +void rec(vs yes, vs maybe, vs no) { + if (maybe.empty() && no.empty()) { + found.push_back(yes); + return; + } + + while (!maybe.empty()) { + string s = maybe.back(); + vs tyes = yes; + tyes.push_back(s); + vs tmaybe; + for (string ss : maybe) { + if (m[s].contains(ss)) { + tmaybe.push_back(ss); + } + } + vs tno; + for (string ss : no) { + if (m[s].contains(ss)) { + tno.push_back(ss); + } + } + rec(tyes, tmaybe, tno); + maybe.pop_back(); + no.push_back(s); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + unordered_set all; + + string line; + while (getline(cin, line)) { + string a = line.substr(0, 2); + string b = line.substr(3, 5); + all.insert(a); + all.insert(b); + m[a].insert(b); + m[b].insert(a); + } + + vs vm; + vm.assign(all.begin(), all.end()); + rec({}, vm, {}); + + vs *ms = nullptr; + for (auto &c : found) { + if (ms == nullptr || ms->size() < c.size()) { + ms = &c; + } + } + vs &msr = *ms; + + sort(msr.begin(), msr.end()); + cout << msr[0]; + for (int i = 1; i < msr.size(); i++) { + cout << ',' << msr[i]; + } + cout << '\n'; +}