Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

26
2017/17/day17.hs Normal file
View file

@ -0,0 +1,26 @@
#!/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