advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 4724c41ca54f72dc21df59ecf3c957c8043c585e
parent a050f700e90dfd22421196e357639af4dc8a52a0
Author: bsandro <email@bsandro.tech>
Date:   Thu, 12 Dec 2024 02:28:02 +0200

Day 11 p2 slightly less puke-inducing

Diffstat:
Mday11/main.cpp | 23+++++------------------
1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/day11/main.cpp b/day11/main.cpp @@ -28,32 +28,19 @@ T read_file(const std::string &path) { typedef std::tuple<int, uint64_t, uint64_t> Memo; typedef std::map<uint64_t, Memo> Cache; -typedef std::map<uint64_t, std::vector<std::pair<int, uint64_t>>> GenCache; - -uint64_t findGenCache(GenCache &cache, uint64_t stone, int num) { - uint64_t val = 0; - for (auto v:cache[stone]) { - auto [blinks, sum] = v; - if (blinks==num) { - val=sum; - break; - } - } - return val; -} +typedef std::map<std::pair<uint64_t, int>, uint64_t> GenCache; uint64_t calc(uint64_t stone, int num, Cache &cache, GenCache &genCache) { if (num<=0) return 1; - if (genCache.contains(stone)) { - uint64_t v = findGenCache(genCache, stone, num); - if (v!=0) return v; + if (genCache.contains(std::make_pair(stone, num))) { + return genCache[std::make_pair(stone, num)]; } if (cache.contains(stone)) { auto [blinks, s1, s2] = cache[stone]; if (blinks>num) return 1; else if (s1==0) return 1; uint64_t res = calc(s1, num-blinks, cache, genCache)+calc(s2, num-blinks, cache, genCache); - genCache[stone].push_back(std::make_pair(num, res)); + genCache[std::make_pair(stone, num)] = res; return res; } @@ -71,7 +58,7 @@ uint64_t calc(uint64_t stone, int num, Cache &cache, GenCache &genCache) { derived *= 2024; } } - genCache[stone].push_back(std::make_pair(num, 1)); + genCache[std::make_pair(stone, num)] = 1; return 1; }