advent-of-code/2017/17/day17.hs
2024-11-12 21:46:18 +01:00

26 lines
677 B
Haskell

#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
{-# LANGUAGE BangPatterns #-}
import Data.List
rotate :: Int -> [a] -> [a]
rotate n xs = take (length xs) (drop n (cycle xs))
spinlock :: (Num a, Enum a) => a -> Int -> [a] -> [a]
spinlock n steps buf = foldl' f buf [1..n]
where f !curbuf !i = let !newbuf = rotate (steps+1) curbuf in i:newbuf
valueAfter :: Eq t => t -> [t] -> t
valueAfter n buf = buf !! ((i + 1) `rem` length buf)
where Just i = elemIndex n buf
main :: IO ()
main = do
let input = 312
n = 2017
start = [0]
print $ valueAfter n $ spinlock n input start
-- let n = 50000000
-- print $ valueAfter 0 $ spinlock n input start