Day 22, with stack build system
[advent-of-code-16.git] / adventofcode16 / app / advent16.hs
diff --git a/adventofcode16/app/advent16.hs b/adventofcode16/app/advent16.hs
new file mode 100644 (file)
index 0000000..88b8b59
--- /dev/null
@@ -0,0 +1,42 @@
+module Main(main) where
+
+import Data.List (nub)
+
+input = "11100010111110100"
+disk1length = 272
+disk2length = 35651584
+
+-- input = "10000"
+-- disk1length = 20
+
+main :: IO ()
+main = do 
+    part1 
+    part2
+
+part1 :: IO ()
+part1 = putStrLn $ checksum $ take disk1length $ expand disk1length input
+
+part2 :: IO ()
+part2 = putStrLn $ checksum $ take disk2length $ expand disk2length input
+
+
+expand :: Int -> String -> String
+expand len a
+    | length a >= len = a
+    | otherwise = expand len $ a ++ "0" ++ b
+        where b = map (invert) $ reverse a
+              invert '0' = '1'
+              invert '1' = '0'
+
+checksum :: String -> String
+checksum digits
+    | odd $ length digits = digits
+    | otherwise = checksum $ map (checksumPair) $ pairs digits
+        where checksumPair p = if (length $ nub p) == 1 then '1' else '0'
+
+
+pairs :: [a] -> [[a]]
+pairs [] = []
+pairs xs = [p] ++ (pairs ys)
+    where (p, ys) = splitAt 2 xs