Day 21 done
[advent-of-code-16.git] / advent16i.hs
1 input = "11100010111110100"
2 disk1length = 272
3 disk2length = 35651584
4
5 -- input = "10000"
6 -- disk1length = 20
7
8 main :: IO ()
9 main = do
10 part1
11 part2
12
13 part1 :: IO ()
14 part1 = print $ fill disk1length input
15
16 part2 :: IO ()
17 part2 = print $ fill disk2length input
18
19 fill :: Int -> String -> String
20 fill len filler = deBool $ checksum $ take len $ expand len $ enBool filler
21
22 enBool :: String -> [Bool]
23 enBool = map (== '1')
24
25 deBool :: [Bool] -> String
26 deBool = map (\b -> if b then '1' else '0')
27
28
29 expand :: Int -> [Bool] -> [Bool]
30 expand len = head . dropWhile ((<= len) . length) . iterate expandStep
31
32 expandStep :: [Bool] -> [Bool]
33 expandStep a = a ++ [False] ++ b
34 where b = map (not) $ reverse a
35
36 checksum :: [Bool] -> [Bool]
37 checksum = head . dropWhile (even . length) . iterate checksumStep
38
39 checksumStep :: [Bool] -> [Bool]
40 checksumStep [] = []
41 checksumStep [x] = [x]
42 checksumStep (x:y:xs) = (x==y):(checksumStep xs)
43