commit 79b905aea4bbe0c15c448f0af3f8cc733f763617
parent c2b969794f3e981d46852d7070cc64802be99e0d
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sat, 7 Dec 2024 07:51:09 +0100
Day 7 2024
Diffstat:
4 files changed, 109 insertions(+), 0 deletions(-)
diff --git a/2024/07/Makefile 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
Binary files differ.
diff --git a/2024/07/day07a.cpp b/2024/07/day07a.cpp
@@ -0,0 +1,34 @@
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+using namespace std;
+
+bool possible(long long x, vector<long long>& 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<long long> 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
@@ -0,0 +1,51 @@
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+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<long long>& 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<long long> 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;
+}