2023 day 4

This commit is contained in:
eriedaberrie 2023-12-03 21:49:21 -08:00
parent 5a9a3ff312
commit c009e8af25
2 changed files with 86 additions and 0 deletions

35
2023/4/main-1.cpp Normal file
View file

@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int ret = 0;
string line;
while (getline(cin, line) && !line.empty()) {
int count = 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;
while (!rss.eof()) {
int n;
rss >> n;
rv.push_back(n);
}
while (!lss.eof()) {
int n;
lss >> n;
for (auto no : rv) {
if (no == n)
count <<= 1;
}
}
ret += count >> 1;
}
cout << ret << '\n';
}

51
2023/4/main-2.cpp Normal file
View file

@ -0,0 +1,51 @@
#include <bits/stdc++.h>
#include <utility>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int ret = 0;
vector<vector<int>> lefts, rights;
vector<int> copies;
string line;
while (getline(cin, line) && !line.empty()) {
int count = 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);
while (!lss.eof()) {
int n;
lss >> n;
lv.push_back(n);
}
lefts.push_back(lv);
copies.push_back(1);
}
for (int cn = 0; cn < copies.size(); cn++) {
ret += copies[cn];
int n = cn;
for (int ln : lefts[cn]) {
for (int rn : rights[cn]) {
if (ln == rn) {
if (++n == copies.size())
goto end;
copies[n] += copies[cn];
}
}
}
end:;
}
cout << ret << '\n';
}