aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-07 07:51:09 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-07 07:51:09 +0100
commit79b905aea4bbe0c15c448f0af3f8cc733f763617 (patch)
tree4cee2342949b7723328bb6fc2c2d079a2a9164cc /2024
parentc2b969794f3e981d46852d7070cc64802be99e0d (diff)
downloadaoc-79b905aea4bbe0c15c448f0af3f8cc733f763617.tar.gz
aoc-79b905aea4bbe0c15c448f0af3f8cc733f763617.zip
Day 7 2024
Diffstat (limited to '2024')
-rw-r--r--2024/07/Makefile24
-rwxr-xr-x2024/07/b.outbin0 -> 125104 bytes
-rw-r--r--2024/07/day07a.cpp34
-rw-r--r--2024/07/day07b.cpp51
4 files changed, 109 insertions, 0 deletions
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 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day07a.cpp
5
6b:
7 ${CC} -o b.out day07b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: b
22 ./b.out < input
23
24.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
--- /dev/null
+++ b/2024/07/b.out
Binary files 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 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9bool possible(long long x, vector<long long>& a, int i) {
10 if (i == 0)
11 return a[0] == x;
12 if (a[i] != 0 && x % a[i] == 0)
13 return possible(x/a[i], a, i-1) || possible(x-a[i], a, i-1);
14 return possible(x-a[i], a, i-1);
15}
16
17int main() {
18 string line;
19 long long tot = 0;
20 while (getline(cin, line)) {
21 long long x, i;
22 vector<long long> a;
23 stringstream s(line);
24 s >> x;
25 char colon;
26 s >> colon;
27 while (s >> i)
28 a.push_back(i);
29 if (possible(x, a, a.size()-1))
30 tot += x;
31 }
32 cout << tot << endl;
33 return 0;
34}
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 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9long long uncat(long long x, long long y) {
10 if (y == 0)
11 return x % 10 == 0 ? x / 10 : -1;
12 while (y > 0) {
13 if (x % 10 != y % 10)
14 return -1;
15 x /= 10;
16 y /= 10;
17 }
18 return x;
19}
20
21bool possible(long long x, vector<long long>& a, int i) {
22 if (i == 0)
23 return a[0] == x;
24
25 bool p = possible(x-a[i], a, i-1);
26 if (a[i] != 0 && x % a[i] == 0)
27 p = p || possible(x/a[i], a, i-1);
28 auto u = uncat(x, a[i]);
29 if (u != -1)
30 p = p || possible(u, a, i-1);
31 return p;
32}
33
34int main() {
35 string line;
36 long long tot = 0;
37 while (getline(cin, line)) {
38 long long x, i;
39 vector<long long> a;
40 stringstream s(line);
41 s >> x;
42 char colon;
43 s >> colon;
44 while (s >> i)
45 a.push_back(i);
46 if (possible(x, a, a.size()-1))
47 tot += x;
48 }
49 cout << tot << endl;
50 return 0;
51}

Generated with cgit - Back to sebastiano.tronto.net