From 8097d2578d3d341ecfa0b0de50862235f42acb4c Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 12 Dec 2017 09:29:05 -0800 Subject: [PATCH] Day 5 - alternate solution using unboxed vectors, but unfortunately they're not any faster --- 5.hs | 2 +- 5_alt.hs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 5_alt.hs diff --git a/5.hs b/5.hs index 7bbc7e4..81c929e 100644 --- a/5.hs +++ b/5.hs @@ -1,4 +1,4 @@ -import Data.IntMap (IntMap, insert, size, fromList, findWithDefault) +import Data.IntMap (IntMap, insert, fromList, findWithDefault) type Length = Int type Index = Int diff --git a/5_alt.hs b/5_alt.hs new file mode 100644 index 0000000..1cced97 --- /dev/null +++ b/5_alt.hs @@ -0,0 +1,26 @@ +import Data.Vector.Unboxed (Vector, fromList, (!), (//)) +import qualified Data.Vector.Unboxed as V (length) + +type Length = Int +type Index = Int +type Steps = Int +type State = (Steps, Index, Vector Int) +type Update = Int -> Int + +next :: Update -> State -> State +next f (steps, i, jumps) = + let value = jumps ! i + nextI = i + value + nextJumps = jumps // [(i, f value)] + in (steps + 1, nextI, nextJumps) + +getExitSteps :: Update -> State -> Int +getExitSteps f (steps, i, jumps) = + if i >= V.length jumps then steps else getExitSteps f $! next f (steps, i, jumps) + +main :: IO () +main = do + input <- readFile "5.txt" + let jumpsList = fromList $ map read $ lines input + print $ getExitSteps (+1) (0, 0, jumpsList) + print $ getExitSteps (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsList) \ No newline at end of file