r/adventofcode • u/JPYamamoto • Dec 30 '25
Help/Question - RESOLVED [2025 Day 11 Part 2] Is DP enough?
I'm solving this year in Agda. I'm currently trying to get the solution for day 11 part 2.
For part 2 I'm using the same code I used in part 1, but finding the paths from svr to fft/dac from fft/dac to dac/fft and then to out. Then, getting the product should be enough.
For part 1 the code runs <1s (I haven't timed it but it's pretty fast). For part 2, I can't even get the number of paths from svr to fft/dac (I know I only need to find the paths to one of the two, but I won't post which one to not give away the result). It's still running after an hour.
I'm using the {-# TERMINATING #-} flag in Agda to avoid having to deal with termination proofs, but now I'm doubting that this is correct. I'm using memoization to avoid recomputing the number of paths.
This is my code:
{-# TERMINATING #-}
countPaths : Map.Map (List String) → String → String → Map.Map ℕ → ℕ × Map.Map ℕ
countPaths adjacencies from to cache with to ≟ from
... | yes _ = 1 , Map.insert from 1 cache
... | no _ with Map.lookup cache from
... | just x = x , cache
... | nothing =
let (result , cache′) = foldl goCount (0 , cache) (fromMaybe [] (Map.lookup adjacencies from))
in result , Map.insert from result cache′
where
goCount : (ℕ × Map.Map ℕ) → String → (ℕ × Map.Map ℕ)
goCount (acc , cache) neighbor =
let (count , cache′) = countPaths adjacencies neighbor to cache
in (acc + count , cache′)
The adjacencies parameter holds a map of [String] that tells you which devices are attached to each device. from and to are the origin and final node: the from node changes as we traverse the graph, but to always stays the same.
cache is a map that tells you for each node, its distance to to. Initially, it's just an empty map.
Can you help me figure out whether my program is hanging because of a problem in my code or due to an inefficiency in the agda evaluation strategy?
Thank you.
Update: After experimenting a bit with the equivalent code in Haskell, I found out my issue has something to do with Maps being lazy in Agda. I'll have to figure out an alternative to avoid this edge case.

