<html><head></head><body><div class="yahoo-style-wrap" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" data-setdir="false">Hi Haskellers - I'm learning Haskell and attempting to solve the Advent of Code 2020 puzzles using Haskell. I'm stuck on part 2 of Day 15 and have been for a while now, so I'm reaching out.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">The puzzle asks you to find the nth element in a list of integers. Here's how the list is constructed:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div dir="ltr" data-setdir="false">Start with a seed list of integers, like [0,3,6]. Then, referring to the last element (6), the next element is given by these rules:</div><div><br></div><div><ul><li>If the last element was the first time the element has appeared in the list, then the next element is 0.</li><li>Otherwise, the next element is the age, or distance in the number of index positions, between the last element and when it last appeared before that.</li></ul></div></div><div><br></div><div dir="ltr" data-setdir="false">For example, starting with [0,3,6], the next elements are 0, 3, 3, 1, 0, 4, 0, etc.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Part 1 of the puzzle asks you to find the 2020th element in the list.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">You can do this by constructing increasingly longer lists like this (using Data.List):</div></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div><b>nextNum :: [Int] -> [Int]</b></div><div><b>nextNum l@(x:xs) = if not (x `elem` xs) then 0 : l else age l : l</b></div><div><b> where</b></div><div><b> age (x:xs) = let Just i = elemIndex x xs</b></div><div><b> in i+1</b></div></div><div dir="ltr" data-setdir="false"><br></div>Then:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"> <span><b>head $ (iterate nextNum [6,3,0]) !! 2017</b></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>will give you the 2020th element of 436.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false">Note that you provide the starting list in reverse order and iterate so that it will keep adding new elements to the head of the list, which is more efficient than adding to the end.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">You can also use unfoldr to generate the list element by element like this:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"> <div><div><b>nextNum' :: [Int] -> Int</b></div><div><b>nextNum' (x:xs) = if not (x `elem` xs) then 0 else age x xs</b></div><div><b> where</b></div><div><b> age x xs = let Just i = elemIndex x xs</b></div><div><b> in i+1</b></div></div><br></div><div dir="ltr" data-setdir="false">Then:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><span><b>(unfoldr (\l -> Just (nextNum' l, nextNum' l : l)) slist) !! 2016</b></span><br></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>will give you the 2020th element of 436.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false">---</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Part 2 of the puzzle asks you to find the <b>30000000th</b> element given starting list [9,3,1,0,8,4].</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">I cannot find a way to do this without stack overflow and performance issues (I've run my attempts overnight with no answer generated). I've tried using Data.Map and Data.Sequence because my Stack Overflow searching suggested these might be more efficient data structures for this sort of task. Here are my attempts:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">-- Uses Data.Map to avoid duplicate numbers thereby shortening the list. The dictionary entry (k, v) gives the element and the last position of that element.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"> <div><div><b>nextNum'' :: (IntMap Int, (Int, Int)) -> (IntMap Int, (Int, Int))</b></div><div><b>nextNum'' (mp, (k, v)) = case IntMap.lookup k mp of</b></div><div><b> Nothing -> (IntMap.insert k v mp, (0, v+1))</b></div><div><b> Just pos -> (IntMap.insert k v mp, (v-pos, v+1))</b></div></div><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Then:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><span><b>snd $ (iterate nextNum'' (IntMap.fromList [(0,1),(3,2)],(6,3))) !! 2017</b></span><br></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>provides the answer for the 2020th element but either stack overflows or runs for hours (if I use a strict version of iterate) trying to figure out the 30000000th element.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false">Similarly, using Data.Sequence, I tried:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"> <div><div><b>nextNum''' :: Seq Int -> Int</b></div><div><b>nextNum'''' (xs :|> x) = if not (x `elem` xs) then 0 else age x xs</b></div><div><b> where</b></div><div><b> age x xs = let Just i = Seq.elemIndexR x xs</b></div><div><b> in Seq.length xs - i</b></div></div></div><div dir="ltr" data-setdir="false"><b><br></b></div><div dir="ltr" data-setdir="false"> <div><div><b>aoc15b' :: Seq Int -> Int -> Int</b></div><div><b>aoc15b' slist tnum = (\(xs :> x) -> x) $ Seq.viewr (Seq.unfoldr (\l -> if Seq.length l == tnum then Nothing else let nnum = force (nextNum'''' l) in Just (nnum, force (l |> nnum))) slist)</b></div></div><br></div><div dir="ltr" data-setdir="false">I found that I needed to fix stack overflow problems by using "force" from Control.DeepSeq. Despite seemingly fixing stack overflow issues though, the calculation just takes too long, and in fact, I have never been able to actually output a solution.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">I thought that using Data.Map or Data.Sequence would speed things up based on my Stack Overflow searching, but I'm unable to come up with a Haskell solution that runs in reasonable time.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">I'm at a loss for different strategies at this point and would appreciate any ideas from the community.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Thanks, Julian</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"></div></div></body></html>