From a937350ab20adc0f8172c4d99f4d711e92dfd947 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 5 Dec 2024 08:02:45 +0100 Subject: Day 5 2024 --- 2024/05/Makefile | 24 ++++++++++++++++ 2024/05/b.out | Bin 0 -> 171240 bytes 2024/05/day05a.cpp | 53 ++++++++++++++++++++++++++++++++++ 2024/05/day05b.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2024/learned.txt | 1 + 2024/template.cpp | 3 +- 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 2024/05/Makefile create mode 100755 2024/05/b.out create mode 100644 2024/05/day05a.cpp create mode 100644 2024/05/day05b.cpp diff --git a/2024/05/Makefile b/2024/05/Makefile new file mode 100644 index 0000000..f9ca961 --- /dev/null +++ b/2024/05/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day05a.cpp + +b: + ${CC} -o b.out day05b.cpp + +clean: + rm -f a b + +atest: a + ./a.out + +btest: b + ./b.out + +arun: a + ./a.out < input + +brun: b + ./b.out < input + +.PHONY: a b clean atest btest arun brun diff --git a/2024/05/b.out b/2024/05/b.out new file mode 100755 index 0000000..6676a53 Binary files /dev/null and b/2024/05/b.out differ diff --git a/2024/05/day05a.cpp b/2024/05/day05a.cpp new file mode 100644 index 0000000..be2037e --- /dev/null +++ b/2024/05/day05a.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +vector readints(string &s) { + vector v; + stringstream sin(s); + int x; + + while (sin >> x) + v.push_back(x); + + return v; +} + +bool valid(vector &v, vector> &rules) { + for (unsigned i = 0; i < v.size(); i++) + for (unsigned j = i+1; j < v.size(); j++) + for (auto r : rules) + if (v[i] == r.second && v[j] == r.first) + return false; + return true; +} + +int main() { + string line; + vector> rules; + while (getline(cin, line)) { + if (line == "") + break; + + replace(line.begin(), line.end(), '|', ' '); + auto v = readints(line); + int x = v[0]; + int y = v[1]; + rules.push_back(make_pair(x, y)); + } + + + int tot = 0; + while (getline(cin, line)) { + replace(line.begin(), line.end(), ',', ' '); + auto v = readints(line); + if (valid(v, rules)) + tot += v[v.size()/2]; + } + cout << tot << endl; + return 0; +} diff --git a/2024/05/day05b.cpp b/2024/05/day05b.cpp new file mode 100644 index 0000000..d9168ac --- /dev/null +++ b/2024/05/day05b.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector readints(string &s) { + vector v; + stringstream sin(s); + int x; + + while (sin >> x) + v.push_back(x); + + return v; +} + +bool valid(vector &v, vector> &rules) { + for (unsigned i = 0; i < v.size(); i++) + for (unsigned j = i+1; j < v.size(); j++) + for (auto r : rules) + if (v[i] == r.second && v[j] == r.first) + return false; + return true; +} + +bool canbenext(int x, vector &v, bool picked[], vector> &rules) { + for (unsigned i = 0; i < v.size(); i++) { + if (picked[i]) + continue; + for (auto r : rules) + if (r.first == v[i] && r.second == x) + return false; + } + + return true; +} + +vector sort(vector &v, vector> &rules) { + vector w; + bool picked[v.size()] = { 0 }; + + while (w.size() < v.size()) { + for (unsigned i = 0; i < v.size(); i++) { + if (!picked[i] && canbenext(v[i], v, picked, rules)) { + picked[i] = true; + w.push_back(v[i]); + } + } + } + + return w; +} + +int main() { + string line; + vector> rules; + while (getline(cin, line)) { + if (line == "") + break; + + replace(line.begin(), line.end(), '|', ' '); + auto v = readints(line); + int x = v[0]; + int y = v[1]; + rules.push_back(make_pair(x, y)); + } + + int tot = 0; + while (getline(cin, line)) { + replace(line.begin(), line.end(), ',', ' '); + auto v = readints(line); + if (!valid(v, rules)) { + auto w = sort(v, rules); + tot += w[w.size()/2]; + } + } + cout << tot << endl; + return 0; +} diff --git a/2024/learned.txt b/2024/learned.txt index 4ab29b1..3a01fc0 100644 --- a/2024/learned.txt +++ b/2024/learned.txt @@ -5,3 +5,4 @@ List of things I learned (or refreshed) with this year's AoC. * Day 3: std::string_view * Day 4: nothing special, some practice with classic and a weird trick to assign two variables at once with a pair of references pair +* Day 5: std::find and std::replace diff --git a/2024/template.cpp b/2024/template.cpp index 3707052..7508a23 100644 --- a/2024/template.cpp +++ b/2024/template.cpp @@ -7,7 +7,8 @@ using namespace std; int main() { - while (cin >> TODO) + string line; + while (getline(cin, line)) ; return 0; } -- cgit v1.3