Day 22, with stack build system
[advent-of-code-16.git] / adventofcode16 / app / advent19.hs
diff --git a/adventofcode16/app/advent19.hs b/adventofcode16/app/advent19.hs
new file mode 100644 (file)
index 0000000..009a07c
--- /dev/null
@@ -0,0 +1,37 @@
+module Main(main) where
+
+import Prelude hiding (length, take, drop)
+import Data.Sequence
+
+-- input = 5 
+input = 3012210 
+
+main :: IO ()
+main = do 
+    part1 
+    part2
+
+part1 :: IO ()
+part1 = print $ 2 * (input - 2 ^ (toInteger (floor $ logBase 2 (fromIntegral input)))) + 1
+
+part2 :: IO ()
+part2 = print $ flip index 0 $ presentSteps initial
+
+presentSteps :: Seq Int -> Seq Int
+presentSteps elves 
+    | isFinished elves = elves
+    | otherwise = presentSteps $ next elves
+
+initial :: Seq Int
+initial = fromList [1..input] 
+
+isFinished :: Seq Int -> Bool
+isFinished elves = length elves == 1
+
+next :: Seq Int -> Seq Int
+next elves = prefix >< (midfix |> suffix)
+    where 
+        target = length elves `quot` 2
+        prefix = drop 1 $ take target elves
+        midfix = drop (target+1) elves
+        suffix = index elves 0