From 79b905aea4bbe0c15c448f0af3f8cc733f763617 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sat, 7 Dec 2024 07:51:09 +0100 Subject: Day 7 2024 --- 2024/07/Makefile | 24 ++++++++++++++++++++++++ 2024/07/b.out | Bin 0 -> 125104 bytes 2024/07/day07a.cpp | 34 ++++++++++++++++++++++++++++++++++ 2024/07/day07b.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 2024/07/Makefile create mode 100755 2024/07/b.out create mode 100644 2024/07/day07a.cpp create mode 100644 2024/07/day07b.cpp diff --git a/2024/07/Makefile b/2024/07/Makefile new file mode 100644 index 0000000..63f62cd --- /dev/null +++ b/2024/07/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day07a.cpp + +b: + ${CC} -o b.out day07b.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/07/b.out b/2024/07/b.out new file mode 100755 index 0000000..b512bd4 Binary files /dev/null and b/2024/07/b.out differ diff --git a/2024/07/day07a.cpp b/2024/07/day07a.cpp new file mode 100644 index 0000000..3b56dea --- /dev/null +++ b/2024/07/day07a.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +bool possible(long long x, vector& a, int i) { + if (i == 0) + return a[0] == x; + if (a[i] != 0 && x % a[i] == 0) + return possible(x/a[i], a, i-1) || possible(x-a[i], a, i-1); + return possible(x-a[i], a, i-1); +} + +int main() { + string line; + long long tot = 0; + while (getline(cin, line)) { + long long x, i; + vector a; + stringstream s(line); + s >> x; + char colon; + s >> colon; + while (s >> i) + a.push_back(i); + if (possible(x, a, a.size()-1)) + tot += x; + } + cout << tot << endl; + return 0; +} diff --git a/2024/07/day07b.cpp b/2024/07/day07b.cpp new file mode 100644 index 0000000..1c2d900 --- /dev/null +++ b/2024/07/day07b.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +long long uncat(long long x, long long y) { + if (y == 0) + return x % 10 == 0 ? x / 10 : -1; + while (y > 0) { + if (x % 10 != y % 10) + return -1; + x /= 10; + y /= 10; + } + return x; +} + +bool possible(long long x, vector& a, int i) { + if (i == 0) + return a[0] == x; + + bool p = possible(x-a[i], a, i-1); + if (a[i] != 0 && x % a[i] == 0) + p = p || possible(x/a[i], a, i-1); + auto u = uncat(x, a[i]); + if (u != -1) + p = p || possible(u, a, i-1); + return p; +} + +int main() { + string line; + long long tot = 0; + while (getline(cin, line)) { + long long x, i; + vector a; + stringstream s(line); + s >> x; + char colon; + s >> colon; + while (s >> i) + a.push_back(i); + if (possible(x, a, a.size()-1)) + tot += x; + } + cout << tot << endl; + return 0; +} -- cgit v1.3