X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent15%2Fsrc%2Fadvent15.hs;h=ac0f2b450129f73b1a45396dd13e3aa3fa70cfff;hp=87cbf3de8c38a443ed195335c403913e6af1edec;hb=de640d46282134bb95db645a2bd8de687f3251b1;hpb=4ecf3b3b78c6ae46bb41526085d69572ba1e9204 diff --git a/advent15/src/advent15.hs b/advent15/src/advent15.hs index 87cbf3d..ac0f2b4 100644 --- a/advent15/src/advent15.hs +++ b/advent15/src/advent15.hs @@ -7,6 +7,9 @@ import Data.STRef import qualified Data.Vector.Unboxed.Mutable as V +type STInt s = STRef s Int +type VInt s = V.MVector s Int + main :: IO () main = do let seed = [20, 0, 1, 11, 6, 3] @@ -18,24 +21,25 @@ main = part1 seed = runGame seed 2020 part2 seed = runGame seed 30000000 -zeroInt :: Int -zeroInt = 0 +runGame :: [Int] -> Int -> Int +runGame seed roundsNeeded = + runST $ + do (round, word, history) <- seedGame seed roundsNeeded + gameSteps roundsNeeded round word history + readSTRef word +seedGame :: [Int] -> Int -> ST s (STInt s, STInt s, VInt s) seedGame seed historySize = do round <- newSTRef $ length seed word <- newSTRef $ last seed - history <- V.replicate historySize zeroInt + history <- V.replicate historySize 0 forM_ (zip (init seed) [1..]) $ \(t, s) -> V.write history t s return (round, word, history) -runGame seed roundsNeeded = - runST $ - do (round, word, history) <- seedGame seed roundsNeeded - gameStep roundsNeeded round word history - readSTRef word -gameStep :: Int -> STRef s Int -> STRef s Int -> V.MVector s Int -> ST s () -gameStep targetRound round word history = +-- gameSteps :: Int -> STRef s Int -> STRef s Int -> V.MVector s Int -> ST s () +gameSteps :: Int -> STInt s -> STInt s -> VInt s -> ST s () +gameSteps targetRound round word history = do roundVal <- readSTRef round if roundVal == targetRound then return () @@ -46,7 +50,7 @@ gameStep targetRound round word history = V.write history wordVal roundVal modifySTRef round (+1) writeSTRef word word' - gameStep targetRound round word history + gameSteps targetRound round word history speakWord :: Int -> Int -> Int