-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.hs
33 lines (28 loc) · 954 Bytes
/
Day5.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Javran.AdventOfCode.Y2017.Day5 (
) where
import Control.Monad.ST
import Data.Function
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Javran.AdventOfCode.Prelude
data Day5 deriving (Generic)
simulate :: (Int -> Int -> Int) -> [Int] -> ST s Int
simulate modifier xs = do
vec <- VU.unsafeThaw (VU.fromList xs)
fix
( \loop cur cnt -> do
offset <- VUM.unsafeRead vec cur
let next = cur + offset
if next < 0 || next >= VUM.length vec
then pure (cnt + 1)
else do
VUM.unsafeModify vec (modifier offset) cur
loop next (cnt + 1)
)
0
0
instance Solution Day5 where
solutionRun _ SolutionContext {getInputS, answerShow} = do
xs <- fmap (read @Int) . lines <$> getInputS
answerShow $ runST (simulate (const succ) xs)
answerShow $ runST (simulate (\offset -> if offset >= 3 then pred else succ) xs)