aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-11 07:03:12 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-11 07:03:12 +0100
commit3418444a8da97e5a05f87f9737bed94edd43ec60 (patch)
treeb3ba5bd74f18179be287f367f70934b08fb29a15 /2024
parent6cee2c54045d253dc14c808f1e4f25765a675a56 (diff)
downloadaoc-3418444a8da97e5a05f87f9737bed94edd43ec60.tar.gz
aoc-3418444a8da97e5a05f87f9737bed94edd43ec60.zip
Day 11 2024
Diffstat (limited to '2024')
-rw-r--r--2024/11/Makefile24
-rw-r--r--2024/11/day11a.cpp57
-rw-r--r--2024/11/day11b.cpp62
-rw-r--r--2024/learned.txt1
4 files changed, 144 insertions, 0 deletions
diff --git a/2024/11/Makefile b/2024/11/Makefile
new file mode 100644
index 0000000..a5d27bb
--- /dev/null
+++ b/2024/11/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day11a.cpp
5
6b:
7 ${CC} -o b.out day11b.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/11/day11a.cpp b/2024/11/day11a.cpp
new file mode 100644
index 0000000..50c3ebf
--- /dev/null
+++ b/2024/11/day11a.cpp
@@ -0,0 +1,57 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <ranges>
6#include <set>
7#include <sstream>
8#include <string>
9#include <string_view>
10#include <vector>
11using namespace std;
12
13pair<uint64_t, uint64_t> split(uint64_t a) {
14 int digits = 0;
15 for (uint64_t b = a; b != 0; b /= 10)
16 digits++;
17
18 if (digits % 2 == 1)
19 return make_pair(0, 0);
20
21 uint64_t j = 1;
22 for (int k = 0; k < digits/2; k++)
23 j *= 10;
24
25 return make_pair(a/j, a%j);
26}
27
28uint64_t count(uint64_t a, int n) {
29 if (n == 0)
30 return 1;
31
32 if (a == 0)
33 return count(1, n-1);
34
35 if (auto [x, y] = split(a); x != 0) {
36 auto c1 = count(x, n-1);
37 auto c2 = count(y, n-1);
38 return c1+c2;
39 }
40
41 return count(a*2024, n-1);
42}
43
44int main() {
45 uint64_t x;
46 vector<uint64_t> old, v;
47 while (cin >> x)
48 v.push_back(x);
49
50 uint64_t tot = 0;
51 for (auto a : v)
52 tot += count(a, 25);
53
54 cout << tot << endl;
55
56 return 0;
57}
diff --git a/2024/11/day11b.cpp b/2024/11/day11b.cpp
new file mode 100644
index 0000000..7f78e8e
--- /dev/null
+++ b/2024/11/day11b.cpp
@@ -0,0 +1,62 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <ranges>
6#include <set>
7#include <sstream>
8#include <string>
9#include <string_view>
10#include <vector>
11using namespace std;
12
13map<pair<uint64_t, uint64_t>, uint64_t> t;
14
15pair<uint64_t, uint64_t> split(uint64_t a) {
16 int digits = 0;
17 for (uint64_t b = a; b != 0; b /= 10)
18 digits++;
19
20 if (digits % 2 == 1)
21 return make_pair(0, 0);
22
23 uint64_t j = 1;
24 for (int k = 0; k < digits/2; k++)
25 j *= 10;
26
27 return make_pair(a/j, a%j);
28}
29
30uint64_t count(uint64_t a, int n) {
31 if (n == 0)
32 return 1;
33
34 if (auto it = t.find(make_pair(a, n)); it != t.end())
35 return it->second;
36
37 if (a == 0)
38 return t[make_pair(a, n)] = count(1, n-1);
39
40 if (auto [x, y] = split(a); x != 0) {
41 auto c1 = count(x, n-1);
42 auto c2 = count(y, n-1);
43 return t[make_pair(a, n)] = c1+c2;
44 }
45
46 return t[make_pair(a, n)] = count(a*2024, n-1);
47}
48
49int main() {
50 uint64_t x;
51 vector<uint64_t> old, v;
52 while (cin >> x)
53 v.push_back(x);
54
55 uint64_t tot = 0;
56 for (auto a : v)
57 tot += count(a, 75);
58
59 cout << tot << endl;
60
61 return 0;
62}
diff --git a/2024/learned.txt b/2024/learned.txt
index 56fa515..440571a 100644
--- a/2024/learned.txt
+++ b/2024/learned.txt
@@ -8,3 +8,4 @@ List of things I learned (or refreshed) with this year's AoC.
8* Day 5: std::find and std::replace 8* Day 5: std::find and std::replace
9* Day 8: set::insert_range(), but it is from C++23 only 9* Day 8: set::insert_range(), but it is from C++23 only
10* Day 9: std::views 10* Day 9: std::views
11* Day 11: if(init; cond)

Generated with cgit - Back to sebastiano.tronto.net