aboutsummaryrefslogtreecommitdiff
path: root/2024/11/day11b-hash.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-22 15:32:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-22 15:32:23 +0200
commitaf6dfd26da9f872ff9d1402c03237ba26df23007 (patch)
tree22f661f1355aa4561c050e23cd7d7a87d9fc8eda /2024/11/day11b-hash.cpp
parent9bfb1bc1a3b2d265d59316143cc7f89fb2cc650b (diff)
downloadaoc-af6dfd26da9f872ff9d1402c03237ba26df23007.tar.gz
aoc-af6dfd26da9f872ff9d1402c03237ba26df23007.zip
Try out unordered_map for old problem
Diffstat (limited to '2024/11/day11b-hash.cpp')
-rw-r--r--2024/11/day11b-hash.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/2024/11/day11b-hash.cpp b/2024/11/day11b-hash.cpp
new file mode 100644
index 0000000..e575512
--- /dev/null
+++ b/2024/11/day11b-hash.cpp
@@ -0,0 +1,69 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <unordered_map>
5#include <ranges>
6#include <set>
7#include <sstream>
8#include <string>
9#include <string_view>
10#include <vector>
11using namespace std;
12
13struct hash_pair {
14 template <class T1, class T2>
15 size_t operator()(const std::pair<T1, T2>& p) const {
16 return std::hash<T1>{}(p.first) ^ std::hash<T2>{}(p.second);
17 }
18};
19
20unordered_map<pair<uint64_t, uint64_t>, uint64_t, hash_pair> t;
21
22pair<uint64_t, uint64_t> split(uint64_t a) {
23 int digits = 0;
24 for (uint64_t b = a; b != 0; b /= 10)
25 digits++;
26
27 if (digits % 2 == 1)
28 return make_pair(0, 0);
29
30 uint64_t j = 1;
31 for (int k = 0; k < digits/2; k++)
32 j *= 10;
33
34 return make_pair(a/j, a%j);
35}
36
37uint64_t count(uint64_t a, int n) {
38 if (n == 0)
39 return 1;
40
41 if (auto it = t.find(make_pair(a, n)); it != t.end())
42 return it->second;
43
44 if (a == 0)
45 return t[make_pair(a, n)] = count(1, n-1);
46
47 if (auto [x, y] = split(a); x != 0) {
48 auto c1 = count(x, n-1);
49 auto c2 = count(y, n-1);
50 return t[make_pair(a, n)] = c1+c2;
51 }
52
53 return t[make_pair(a, n)] = count(a*2024, n-1);
54}
55
56int main() {
57 uint64_t x;
58 vector<uint64_t> old, v;
59 while (cin >> x)
60 v.push_back(x);
61
62 uint64_t tot = 0;
63 for (auto a : v)
64 tot += count(a, 75);
65
66 cout << tot << endl;
67
68 return 0;
69}

Generated with cgit - Back to sebastiano.tronto.net