From af6dfd26da9f872ff9d1402c03237ba26df23007 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 22 Jul 2025 15:32:23 +0200 Subject: Try out unordered_map for old problem --- 2024/11/day11b-hash.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 2024/11/day11b-hash.cpp (limited to '2024/11/day11b-hash.cpp') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct hash_pair { + template + size_t operator()(const std::pair& p) const { + return std::hash{}(p.first) ^ std::hash{}(p.second); + } +}; + +unordered_map, uint64_t, hash_pair> t; + +pair split(uint64_t a) { + int digits = 0; + for (uint64_t b = a; b != 0; b /= 10) + digits++; + + if (digits % 2 == 1) + return make_pair(0, 0); + + uint64_t j = 1; + for (int k = 0; k < digits/2; k++) + j *= 10; + + return make_pair(a/j, a%j); +} + +uint64_t count(uint64_t a, int n) { + if (n == 0) + return 1; + + if (auto it = t.find(make_pair(a, n)); it != t.end()) + return it->second; + + if (a == 0) + return t[make_pair(a, n)] = count(1, n-1); + + if (auto [x, y] = split(a); x != 0) { + auto c1 = count(x, n-1); + auto c2 = count(y, n-1); + return t[make_pair(a, n)] = c1+c2; + } + + return t[make_pair(a, n)] = count(a*2024, n-1); +} + +int main() { + uint64_t x; + vector old, v; + while (cin >> x) + v.push_back(x); + + uint64_t tot = 0; + for (auto a : v) + tot += count(a, 75); + + cout << tot << endl; + + return 0; +} -- cgit v1.3