2017-12-07 04:38:38 +00:00
|
|
|
import Data.IntMap (IntMap, insert, size, fromList, findWithDefault)
|
2017-12-05 07:07:09 +00:00
|
|
|
|
2017-12-07 04:38:38 +00:00
|
|
|
type Length = Int
|
2017-12-05 07:07:09 +00:00
|
|
|
type Index = Int
|
|
|
|
type Steps = Int
|
2017-12-07 04:38:38 +00:00
|
|
|
type State = (Steps, Index, IntMap Int)
|
2017-12-05 07:07:09 +00:00
|
|
|
type Update = Int -> Int
|
|
|
|
|
|
|
|
next :: Update -> State -> State
|
|
|
|
next f (steps, i, jumps) =
|
2017-12-07 04:38:38 +00:00
|
|
|
let value = findWithDefault undefined i jumps
|
2017-12-05 07:07:09 +00:00
|
|
|
nextI = i + value
|
2017-12-07 04:38:38 +00:00
|
|
|
nextJumps = insert i (f value) jumps
|
2017-12-05 07:07:09 +00:00
|
|
|
in (steps + 1, nextI, nextJumps)
|
|
|
|
|
2017-12-07 04:38:38 +00:00
|
|
|
getExitSteps :: Length -> Update -> State -> Int
|
|
|
|
getExitSteps len f (steps, i, jumps) =
|
|
|
|
if i >= len then steps else getExitSteps len f $ next f (steps, i, jumps)
|
2017-12-05 07:07:09 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
input <- readFile "5.txt"
|
2017-12-07 04:38:38 +00:00
|
|
|
let jumpsList = map read $ lines input
|
|
|
|
jumpsMap = fromList $ zip [0..] jumpsList
|
|
|
|
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)
|