summaryrefslogtreecommitdiff
path: root/2017
diff options
context:
space:
mode:
Diffstat (limited to '2017')
-rw-r--r--2017/17.hs37
-rw-r--r--2017/17.in1
2 files changed, 38 insertions, 0 deletions
diff --git a/2017/17.hs b/2017/17.hs
new file mode 100644
index 0000000..7dbe55b
--- /dev/null
+++ b/2017/17.hs
@@ -0,0 +1,37 @@
+import Control.Monad
+import Data.List hiding (insert)
+import Data.Maybe
+
+
+data State = State Int [Int]
+
+initState :: State
+initState = State 1 [0]
+
+step :: Int -> State -> State
+step stepsize (State cur arr) = State cur (take (length arr) $ drop stepsize $ cycle arr)
+
+insert :: State -> State
+insert (State cur (hd:tl)) = State (cur+1) (cur : tl ++ [hd])
+
+data State2 = State2 {s2Len :: Int, s2Cur :: Int, s2Value :: Int}
+
+initState2 :: State2
+initState2 = State2 1 0 0
+
+step2 :: Int -> State2 -> State2
+step2 stepsize (State2 len cur val) = State2 len ((cur + stepsize) `mod` len) val
+
+insert2 :: State2 -> State2
+insert2 (State2 len 0 val) = State2 (len+1) 1 len
+insert2 (State2 len cur val) = State2 (len+1) (cur+1) val
+
+main :: IO ()
+main = do
+ stepsize <- liftM read (readFile "17.in")
+
+ let State _ end = iterate (insert . step stepsize) initState !! 2017
+ print $ end !! 1
+
+ let end2 = iterate (insert2 . step2 stepsize) initState2 !! 50000000
+ print $ s2Value end2
diff --git a/2017/17.in b/2017/17.in
new file mode 100644
index 0000000..aef2e27
--- /dev/null
+++ b/2017/17.in
@@ -0,0 +1 @@
+349