aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit a937350ab20adc0f8172c4d99f4d711e92dfd947
parent 59892b65a9810044ca7f135e39c8d44094224c85
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu,  5 Dec 2024 08:02:45 +0100

Day 5 2024

Diffstat:
A2024/05/Makefile | 24++++++++++++++++++++++++
A2024/05/b.out | 0
A2024/05/day05a.cpp | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
A2024/05/day05b.cpp | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M2024/learned.txt | 1+
M2024/template.cpp | 3++-
6 files changed, 163 insertions(+), 1 deletion(-)

diff --git a/2024/05/Makefile 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 Binary files differ. diff --git a/2024/05/day05a.cpp b/2024/05/day05a.cpp @@ -0,0 +1,53 @@ +#include <algorithm> +#include <iostream> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +using namespace std; + +vector<int> readints(string &s) { + vector<int> v; + stringstream sin(s); + int x; + + while (sin >> x) + v.push_back(x); + + return v; +} + +bool valid(vector<int> &v, vector<pair<int, int>> &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<pair<int, int>> 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 @@ -0,0 +1,83 @@ +#include <algorithm> +#include <iostream> +#include <sstream> +#include <string> +#include <string_view> +#include <set> +#include <vector> +using namespace std; + +vector<int> readints(string &s) { + vector<int> v; + stringstream sin(s); + int x; + + while (sin >> x) + v.push_back(x); + + return v; +} + +bool valid(vector<int> &v, vector<pair<int, int>> &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<int> &v, bool picked[], vector<pair<int, int>> &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<int> sort(vector<int> &v, vector<pair<int, int>> &rules) { + vector<int> 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<pair<int, int>> 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 @@ -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<int&, int&> +* Day 5: std::find and std::replace diff --git 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; }