From c82a6bcffc67552384e530aea1ecf9c0256e0281 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Sun, 3 Dec 2023 21:49:21 -0800 Subject: [PATCH] 2023 day 4 --- 2023/4/main-1.cpp | 35 +++++++++++++++++++++++++++++++++ 2023/4/main-2.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 2023/4/main-1.cpp create mode 100644 2023/4/main-2.cpp diff --git a/2023/4/main-1.cpp b/2023/4/main-1.cpp new file mode 100644 index 0000000..246ae46 --- /dev/null +++ b/2023/4/main-1.cpp @@ -0,0 +1,35 @@ +#include +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 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'; +} diff --git a/2023/4/main-2.cpp b/2023/4/main-2.cpp new file mode 100644 index 0000000..9d35fc8 --- /dev/null +++ b/2023/4/main-2.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int ret = 0; + vector> lefts, rights; + vector 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 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); + } + 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'; +}