advent-of-code/2024/23/main-2.cpp
2024-12-22 22:33:15 -08:00

73 lines
1.2 KiB
C++

#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
typedef vector<string> vs;
unordered_map<string, unordered_set<string>> m;
vector<vs> 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<string> 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';
}