aoc

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

day11b.cpp (1082B)


      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>
     11 using namespace std;
     12 
     13 map<pair<uint64_t, uint64_t>, uint64_t> t;
     14 
     15 pair<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 
     30 uint64_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 
     49 int 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 }