2023 day 4: remove goto and replace vector with set
Due to popular demand
This commit is contained in:
parent
40024cef5b
commit
9057173ca5
|
@ -15,17 +15,16 @@ int main() {
|
|||
string left = line.substr(start + 2, bar - start - 3);
|
||||
string right = line.substr(bar + 2);
|
||||
istringstream lss(left), rss(right);
|
||||
vector<int> rv;
|
||||
while (!rss.eof()) {
|
||||
int n;
|
||||
rss >> n;
|
||||
rv.push_back(n);
|
||||
}
|
||||
set<int> ls;
|
||||
while (!lss.eof()) {
|
||||
int n;
|
||||
lss >> n;
|
||||
for (auto no : rv) {
|
||||
if (no == n)
|
||||
ls.insert(n);
|
||||
}
|
||||
while (!rss.eof()) {
|
||||
int n;
|
||||
rss >> n;
|
||||
if (ls.find(n) != ls.end()) {
|
||||
count <<= 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,45 +5,40 @@ int main() {
|
|||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
int ret = 0;
|
||||
vector<vector<int>> lefts, rights;
|
||||
vector<int> copies;
|
||||
vector<int> counts, copies;
|
||||
|
||||
string line;
|
||||
while (getline(cin, line) && !line.empty()) {
|
||||
copies.push_back(1);
|
||||
int start = line.find(':');
|
||||
int bar = line.find('|');
|
||||
string left = line.substr(start + 2, bar - start - 3);
|
||||
string right = line.substr(bar + 2);
|
||||
istringstream lss(left), rss(right);
|
||||
vector<int> rv, lv;
|
||||
while (!rss.eof()) {
|
||||
int n;
|
||||
rss >> n;
|
||||
rv.push_back(n);
|
||||
}
|
||||
rights.push_back(rv);
|
||||
set<int> ls;
|
||||
int count = 0;
|
||||
while (!lss.eof()) {
|
||||
int n;
|
||||
lss >> n;
|
||||
lv.push_back(n);
|
||||
ls.insert(n);
|
||||
}
|
||||
lefts.push_back(lv);
|
||||
while (!rss.eof()) {
|
||||
int n;
|
||||
rss >> n;
|
||||
if (ls.find(n) != ls.end())
|
||||
count++;
|
||||
}
|
||||
for (int cn = 0; cn < copies.size(); cn++) {
|
||||
counts.push_back(count);
|
||||
copies.push_back(1);
|
||||
}
|
||||
int ret = 0;
|
||||
for (int cn = 0; cn < counts.size(); cn++) {
|
||||
ret += copies[cn];
|
||||
int n = cn;
|
||||
for (int ln : lefts[cn]) {
|
||||
for (int rn : rights[cn]) {
|
||||
if (ln == rn) {
|
||||
for (int i = 0; i < counts[cn]; i++) {
|
||||
if (++n == copies.size())
|
||||
goto end;
|
||||
break;
|
||||
copies[n] += copies[cn];
|
||||
}
|
||||
}
|
||||
}
|
||||
end:;
|
||||
}
|
||||
cout << ret << '\n';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue