From 5a23d15ff9bb69d9e9b52e543596e75af3b090f9 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 2 Dec 2024 06:46:28 +0100 Subject: Day 2 2024 --- 2024/02/Makefile | 24 ++++++++++++++++++++++++ 2024/02/b.out | Bin 0 -> 152640 bytes 2024/02/day02a.cpp | 30 ++++++++++++++++++++++++++++++ 2024/02/day02b.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 2024/02/Makefile create mode 100755 2024/02/b.out create mode 100644 2024/02/day02a.cpp create mode 100644 2024/02/day02b.cpp (limited to '2024/02') diff --git a/2024/02/Makefile b/2024/02/Makefile new file mode 100644 index 0000000..c3cdac5 --- /dev/null +++ b/2024/02/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day02a.cpp + +b: + ${CC} -o b.out day02b.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/02/b.out b/2024/02/b.out new file mode 100755 index 0000000..fc29aef Binary files /dev/null and b/2024/02/b.out differ diff --git a/2024/02/day02a.cpp b/2024/02/day02a.cpp new file mode 100644 index 0000000..cdd9efa --- /dev/null +++ b/2024/02/day02a.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +using namespace std; + +bool isvalid(const string &line) { + int x, y, b; + istringstream s(line); + + s >> x >> y; + b = x > y ? 1 : -1; + do { + int d = b * (x-y); + if (d < 1 || d > 3) + return false; + x = y; + } while (s >> y); + + return true; +} + +int main() { + int tot = 0; + for (string line; getline(cin, line); ) + tot += isvalid(line); + cout << tot << endl; + return 0; +} diff --git a/2024/02/day02b.cpp b/2024/02/day02b.cpp new file mode 100644 index 0000000..2e3dcf4 --- /dev/null +++ b/2024/02/day02b.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +using namespace std; + +bool isvalid_v(const vector &v) { + int b = v[0] > v[1] ? 1 : -1; + for (unsigned i = 0; i < v.size()-1; i++) { + int d = b * (v[i] - v[i+1]); + if (d < 1 || d > 3) + return false; + } + return true; +} + +vector getv(const string &line) { + int x; + vector ret; + istringstream s(line); + + while(s >> x) + ret.push_back(x); + + return ret; +} + +bool isvalid(const string &line) { + vector v0 = getv(line); + if (isvalid_v(v0)) + return true; + for (unsigned i = 0; i < v0.size(); i++) { + vector v = v0; + v.erase(v.begin() + i); + if (isvalid_v(v)) + return true; + } + return false; +} + +int main() { + int tot = 0; + for (string line; getline(cin, line); ) + tot += isvalid(line); + cout << tot << endl; + return 0; +} -- cgit v1.3