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/day07b.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2024/07/day07b.cpp (limited to '2024/07/day07b.cpp') 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