From: Neil Smith Date: Fri, 16 Dec 2016 09:35:02 +0000 (+0000) Subject: Added a version using X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=906f9582f63850bd18387f7adf3e6322ecb4ffbf;p=advent-of-code-16.git Added a version using --- diff --git a/advent16i.hs b/advent16i.hs new file mode 100644 index 0000000..4cf13f6 --- /dev/null +++ b/advent16i.hs @@ -0,0 +1,43 @@ +input = "11100010111110100" +disk1length = 272 +disk2length = 35651584 + +-- input = "10000" +-- disk1length = 20 + +main :: IO () +main = do + part1 + part2 + +part1 :: IO () +part1 = print $ fill disk1length input + +part2 :: IO () +part2 = print $ fill disk2length input + +fill :: Int -> String -> String +fill len filler = deBool $ checksum $ take len $ expand len $ enBool filler + +enBool :: String -> [Bool] +enBool = map (== '1') + +deBool :: [Bool] -> String +deBool = map (\b -> if b then '1' else '0') + + +expand :: Int -> [Bool] -> [Bool] +expand len = head . dropWhile ((<= len) . length) . iterate expandStep + +expandStep :: [Bool] -> [Bool] +expandStep a = a ++ [False] ++ b + where b = map (not) $ reverse a + +checksum :: [Bool] -> [Bool] +checksum = head . dropWhile (even . length) . iterate checksumStep + +checksumStep :: [Bool] -> [Bool] +checksumStep [] = [] +checksumStep [x] = [x] +checksumStep (x:y:xs) = (x==y):(checksumStep xs) +