summaryrefslogtreecommitdiff
path: root/2017/17.hs
blob: 7dbe55bc2aabb78702279bd8284ee70424c3ff68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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