aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit 428575264d66816639dc62e1aa7260561db5101e
parent e61c826e1313651f11fec940fb88145e9b4b6dc5
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Tue, 22 Jul 2025 15:32:23 +0200

Try out unordered_map for old problem

Diffstat:
A2024/11/day11b-hash.cpp | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+), 0 deletions(-)

diff --git a/2024/11/day11b-hash.cpp b/2024/11/day11b-hash.cpp @@ -0,0 +1,69 @@ +#include <algorithm> +#include <cstdint> +#include <iostream> +#include <unordered_map> +#include <ranges> +#include <set> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +using namespace std; + +struct hash_pair { + template <class T1, class T2> + size_t operator()(const std::pair<T1, T2>& p) const { + return std::hash<T1>{}(p.first) ^ std::hash<T2>{}(p.second); + } +}; + +unordered_map<pair<uint64_t, uint64_t>, uint64_t, hash_pair> t; + +pair<uint64_t, uint64_t> 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<uint64_t> 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; +}