Day 5 - replaced Seq Int with IntMap Int for slight performance improvement...
This commit is contained in:
parent
f395778a9f
commit
e910558507
23
5.hs
23
5.hs
|
@ -1,24 +1,27 @@
|
||||||
import Data.Sequence
|
import Data.IntMap (IntMap, insert, size, fromList, findWithDefault)
|
||||||
|
|
||||||
|
type Length = Int
|
||||||
type Index = Int
|
type Index = Int
|
||||||
type Steps = Int
|
type Steps = Int
|
||||||
type State = (Steps, Index, Seq Int)
|
type State = (Steps, Index, IntMap Int)
|
||||||
type Update = Int -> Int
|
type Update = Int -> Int
|
||||||
|
|
||||||
next :: Update -> State -> State
|
next :: Update -> State -> State
|
||||||
next f (steps, i, jumps) =
|
next f (steps, i, jumps) =
|
||||||
let value = index jumps i
|
let value = findWithDefault undefined i jumps
|
||||||
nextI = i + value
|
nextI = i + value
|
||||||
nextJumps = update i (f value) jumps
|
nextJumps = insert i (f value) jumps
|
||||||
in (steps + 1, nextI, nextJumps)
|
in (steps + 1, nextI, nextJumps)
|
||||||
|
|
||||||
getExitSteps :: Update -> State -> Int
|
getExitSteps :: Length -> Update -> State -> Int
|
||||||
getExitSteps f (steps, i, jumps) =
|
getExitSteps len f (steps, i, jumps) =
|
||||||
if i >= Data.Sequence.length jumps then steps else getExitSteps 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
|
||||||
input <- readFile "5.txt"
|
input <- readFile "5.txt"
|
||||||
let jumps = fromList . map read $ lines input :: Seq Int
|
let jumpsList = map read $ lines input
|
||||||
print $ getExitSteps (+1) (0, 0, jumps)
|
jumpsMap = fromList $ zip [0..] jumpsList
|
||||||
print $ getExitSteps (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumps)
|
len = length jumpsList
|
||||||
|
print $ getExitSteps len (+1) (0, 0, jumpsMap)
|
||||||
|
print $ getExitSteps len (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsMap)
|
Loading…
Reference in New Issue