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