summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-12-17 08:43:53 +0100
committertomsmeding <tom.smeding@gmail.com>2017-12-17 08:44:25 +0100
commit53c0e9c38422f3cd5cf567bd8a7d2a8cdae66be8 (patch)
tree988f1aa89405388cfa87fc1edfc394ff39fd1a61
parent5f6c480073be6f69e0e0f1241ac80e26245dd7a7 (diff)
Day 17
You want to compile this for part 2. Inefficient, but works. Reusing part 1 was so inefficient that it blew up my memory.
-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