Day 5 - added some strict evaluation for performance

This commit is contained in:
Jonathan Chan 2017-12-17 16:10:39 -08:00
parent 065421a742
commit 0ce9ae7665
1 changed files with 5 additions and 4 deletions

9
05.hs
View File

@ -1,4 +1,5 @@
import Data.IntMap (IntMap, insert, fromList, findWithDefault) {-# LANGUAGE BangPatterns #-}
import Data.IntMap.Strict (IntMap, insert, fromList, findWithDefault)
type State = (Int, Int, IntMap Int) type State = (Int, Int, IntMap Int)
type Update = Int -> Int type Update = Int -> Int
@ -9,12 +10,12 @@ next f (steps, i, jumps) =
in (steps + 1, i + value, insert i (f value) jumps) in (steps + 1, i + value, insert i (f value) jumps)
getExitSteps :: Int -> Update -> State -> Int getExitSteps :: Int -> Update -> State -> Int
getExitSteps len f (steps, i, jumps) = getExitSteps len f (!steps, i, jumps) =
if i >= len then steps else getExitSteps len f $! next f (steps, i, jumps) if i >= len then steps else getExitSteps len f $ next f (steps, i, jumps)
main :: IO () main :: IO ()
main = do main = do
jumpsList <- fmap (map read . lines) $ readFile "5.txt" jumpsList <- fmap (map read . lines) $ readFile "05.txt"
let jumpsMap = fromList $ zip [0..] jumpsList let jumpsMap = fromList $ zip [0..] jumpsList
print $ getExitSteps (length jumpsList) (+1) (0, 0, jumpsMap) print $ getExitSteps (length jumpsList) (+1) (0, 0, jumpsMap)
print $ getExitSteps (length jumpsList) (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsMap) print $ getExitSteps (length jumpsList) (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsMap)