From 0fabff92b8ccc9eea1beb5a94fb6523db7921cd2 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Wed, 4 Dec 2024 21:36:28 -0800 Subject: [PATCH] 2024 day 5 --- 2024/5/main-1.cpp | 47 ++++++++++++++++++++++++++++++++++++++ 2024/5/main-2.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 2024/5/main-1.cpp create mode 100644 2024/5/main-2.cpp diff --git a/2024/5/main-1.cpp b/2024/5/main-1.cpp new file mode 100644 index 0000000..c7f21c3 --- /dev/null +++ b/2024/5/main-1.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + map> order; + + string line; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + int a, b; + l >> a; + l.ignore(); + l >> b; + if (!order.contains(a)) { + set x; + order[a] = x; + } + order[a].insert(b); + } + + int ret = 0; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + int x; + vector elems; + while (l >> x) { + elems.push_back(x); + l.ignore(); + } + for (int i = 0; i < elems.size(); i++) { + if (order.contains(elems[i])) { + for (int j = 0; j < i; j++) { + if (order[elems[i]].contains(elems[j])) { + goto next; + } + } + } + } + ret += elems[elems.size() / 2]; + next:; + } + + cout << ret << '\n'; +} diff --git a/2024/5/main-2.cpp b/2024/5/main-2.cpp new file mode 100644 index 0000000..92a47d8 --- /dev/null +++ b/2024/5/main-2.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +map> order; + +bool ordered(vector& elems) { + for (int i = 0; i < elems.size(); i++) { + if (order.contains(elems[i])) { + for (int j = 0; j < i; j++) { + if (order[elems[i]].contains(elems[j])) { + int x = elems[i]; + elems[i] = elems[j]; + elems[j] = x; + return false; + } + } + } + } + return true; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + int a, b; + l >> a; + l.ignore(); + l >> b; + if (!order.contains(a)) { + set x; + order[a] = x; + } + order[a].insert(b); + } + + int ret = 0; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + int x; + vector elems; + while (l >> x) { + elems.push_back(x); + l.ignore(); + } + if (ordered(elems)) { + continue; + } + while (!ordered(elems)); + ret += elems[elems.size() / 2]; + } + + cout << ret << '\n'; +}