X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent15%2Fsrc%2Fadvent15slow.hs;fp=advent15%2Fsrc%2Fadvent15slow.hs;h=508fedd077cc4472b1262bcdcf6a209d8f1e50c9;hp=0000000000000000000000000000000000000000;hb=4ecf3b3b78c6ae46bb41526085d69572ba1e9204;hpb=60f781a177b4c92193f0fca095816c8ca883c1c4 diff --git a/advent15/src/advent15slow.hs b/advent15/src/advent15slow.hs new file mode 100644 index 0000000..508fedd --- /dev/null +++ b/advent15/src/advent15slow.hs @@ -0,0 +1,47 @@ +-- import Debug.Trace + +import Prelude hiding (round) +import qualified Data.IntMap.Strict as M + + +data Game = Game { round :: Int + , word :: Int + , history :: M.IntMap Int + } deriving (Show, Eq) + +main :: IO () +main = + do let seed = [20, 0, 1, 11, 6, 3] + -- print seed + print $ part1 seed + print $ part2 seed + +part1 = word . (gameRound 2020) . seedGame +-- part2 = word . (gameRound 30000000) . seedGame +part2 g0 = (word gf, maximum $ M.keys $ history gf) + where gf = (gameRound 30000000) $ seedGame g0 + + +seedGame seed = Game {..} + where round = length seed + word = last seed + history = M.fromList $ zip (init seed) [1..] + +infiniteGame g = iterate gameStep g + +gameRound r game0 = head $ dropWhile notYet $ infiniteGame game0 + where notYet game = round game < r + +gameStep Game{..} = + Game { round = round + 1 + , word = word' + , history = history' + } + where + word' = speakWord (M.lookup word history) round + history' = M.insert word round history + +speakWord Nothing _ = 0 +speakWord (Just prev) now = now - prev + +