aoc

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

commit 5a23d15ff9bb69d9e9b52e543596e75af3b090f9
parent 0aefd410a18acf826af5187cdb038fd9b4f25fc5
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Mon,  2 Dec 2024 06:46:28 +0100

Day 2 2024

Diffstat:
A2024/02/Makefile | 24++++++++++++++++++++++++
A2024/02/b.out | 0
A2024/02/day02a.cpp | 30++++++++++++++++++++++++++++++
A2024/02/day02b.cpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/2024/02/Makefile 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 Binary files differ. diff --git a/2024/02/day02a.cpp b/2024/02/day02a.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <sstream> +#include <vector> +#include <algorithm> +#include <string> +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 @@ -0,0 +1,48 @@ +#include <iostream> +#include <sstream> +#include <vector> +#include <algorithm> +#include <string> +using namespace std; + +bool isvalid_v(const vector<int> &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<int> getv(const string &line) { + int x; + vector<int> 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; +}