Removed language extension
[advent-of-code-16.git] / advent16.hs
1 import Data.List (nub)
2
3 input = "11100010111110100"
4 disk1length = 272
5 disk2length = 35651584
6
7 -- input = "10000"
8 -- disk1length = 20
9
10 main :: IO ()
11 main = do
12 part1
13 part2
14
15
16 part1 :: IO ()
17 part1 = print $ checksum $ take disk1length $ expand disk1length input
18
19 part2 :: IO ()
20 part2 = print $ checksum $ take disk2length $ expand disk2length input
21
22
23 expand :: Int -> String -> String
24 expand len a
25 | length a >= len = a
26 | otherwise = expand len $ a ++ "0" ++ b
27 where b = map (invert) $ reverse a
28 invert '0' = '1'
29 invert '1' = '0'
30
31 checksum :: String -> String
32 checksum digits
33 | odd $ length digits = digits
34 | otherwise = checksum $ map (checksumPair) $ pairs digits
35 where checksumPair p = if (length $ nub p) == 1 then '1' else '0'
36
37
38 pairs :: [a] -> [[a]]
39 pairs [] = []
40 pairs xs = [p] ++ (pairs ys)
41 where (p, ys) = splitAt 2 xs