3 import Prelude hiding (round)
5 import Control.Monad.ST
6 import Control.Monad.Loops
8 import qualified Data.Vector.Unboxed.Mutable as V
13 do let seed = [20, 0, 1, 11, 6, 3]
19 part1 seed = runGame seed 2020
20 part2 seed = runGame seed 30000000
22 runGame seed roundsNeeded =
24 do (round, word, history) <- seedGame seed roundsNeeded
25 gameLoop roundsNeeded round word history
28 -- gameLoop targetRound round word history =
29 -- do gameStep round word history
30 -- `untilM_` ((== targetRound) <$> readSTRef round)
33 -- gameLoop targetRound round word history =
34 -- do ( gameStep round word history
35 -- `untilM_` (do r <- readSTRef round
36 -- return $ r == targetRound)
40 -- gameLoop targetRound round word history =
41 -- do untilM_ (gameStep round word history )
42 -- (do r <- readSTRef round
43 -- return $ r == targetRound )
46 -- gameLoop targetRound round word history =
47 -- do whileM_ (do r <- readSTRef round
48 -- return $ r /= targetRound )
49 -- (gameStep round word history )
52 -- gameLoop targetRound round word history =
53 -- do whileM_ (do r <- readSTRef round
54 -- return $ r /= targetRound )
55 -- $ gameStep round word history
58 gameLoop targetRound round word history =
59 do whileM_ ((/= targetRound) <$> readSTRef round)
60 $ gameStep round word history
63 seedGame seed historySize =
64 do round <- newSTRef $ length seed
65 word <- newSTRef $ last seed
66 history <- V.replicate historySize 0
67 forM_ (zip (init seed) [1..]) $ \(t, s) -> V.write history t s
68 return (round, word, history)
70 gameStep :: STRef s Int -> STRef s Int -> V.MVector s Int -> ST s ()
71 gameStep round word history =
72 do roundVal <- readSTRef round
73 wordVal <- readSTRef word
74 wordH <- V.read history wordVal
75 let word' = speakWord wordH roundVal
76 V.write history wordVal roundVal
77 modifySTRef round (+1)
81 speakWord :: Int -> Int -> Int
83 speakWord prev now = now - prev