From 53c0e9c38422f3cd5cf567bd8a7d2a8cdae66be8 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 17 Dec 2017 08:43:53 +0100 Subject: 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. --- 2017/17.hs | 37 +++++++++++++++++++++++++++++++++++++ 2017/17.in | 1 + 2 files changed, 38 insertions(+) create mode 100644 2017/17.hs create mode 100644 2017/17.in 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 -- cgit v1.2.3-70-g09d2