897559d5d810e7da1e8c721d3fafd2335ae6fbf3
[advent-of-code-16.git] / adventofcode16 / app / advent16i.hs
1 module Main(main) where
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 part1 :: IO ()
16 part1 = print $ fill disk1length input
17
18 part2 :: IO ()
19 part2 = print $ fill disk2length input
20
21 fill :: Int -> String -> String
22 fill len filler = deBool $ checksum $ take len $ expand len $ enBool filler
23
24 enBool :: String -> [Bool]
25 enBool = map (== '1')
26
27 deBool :: [Bool] -> String
28 deBool = map (\b -> if b then '1' else '0')
29
30
31 expand :: Int -> [Bool] -> [Bool]
32 expand len = head . dropWhile ((<= len) . length) . iterate expandStep
33
34 expandStep :: [Bool] -> [Bool]
35 expandStep a = a ++ [False] ++ b
36 where b = map (not) $ reverse a
37
38 checksum :: [Bool] -> [Bool]
39 checksum = head . dropWhile (even . length) . iterate checksumStep
40
41 checksumStep :: [Bool] -> [Bool]
42 checksumStep [] = []
43 checksumStep [x] = [x]
44 checksumStep (x:y:xs) = (x==y):(checksumStep xs)
45