Tidying, especially the parser
[advent-of-code-16.git] / adventofcode16 / app / advent19.hs
1 module Main(main) where
2
3 import Prelude hiding (length, take, drop)
4 import Data.Sequence
5
6 -- input = 5
7 input = 3012210
8
9 main :: IO ()
10 main = do
11 part1
12 part2
13
14 part1 :: IO ()
15 part1 = print $ 2 * (input - 2 ^ (toInteger (floor $ logBase 2 (fromIntegral input)))) + 1
16
17 part2 :: IO ()
18 part2 = print $ flip index 0 $ presentSteps initial
19
20 presentSteps :: Seq Int -> Seq Int
21 presentSteps elves
22 | isFinished elves = elves
23 | otherwise = presentSteps $ next elves
24
25 initial :: Seq Int
26 initial = fromList [1..input]
27
28 isFinished :: Seq Int -> Bool
29 isFinished elves = length elves == 1
30
31 next :: Seq Int -> Seq Int
32 next elves = prefix >< (midfix |> suffix)
33 where
34 target = length elves `quot` 2
35 prefix = drop 1 $ take target elves
36 midfix = drop (target+1) elves
37 suffix = index elves 0