-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16.hs
44 lines (28 loc) · 1.07 KB
/
Day16.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE ViewPatterns #-}
import qualified Data.Sequence as Seq
import Data.Monoid
import Data.Foldable
input = readCurve "11100010111110100"
{-inputLen = 272-}
inputLen = 35651584
step c = c <> [False] <> (reverse . map not $ c)
showCurve :: [Bool] -> String
showCurve = fmap (\c -> if c then '1' else '0')
readCurve :: String -> [Bool]
readCurve = map (/= '0')
curves = iterate step input
curveLen l = take l <$> find ((>= l) . length) curves
checksum :: [Bool] -> [Bool]
checksum s = if even (length staging) then checksum staging else staging
where go (a:b:rst) | a == b = True : go rst
| otherwise = False : go rst
go [] = []
staging = go s
main = do
let input = readCurve "11100010111110100"
len1 = 272
len2 = 35651584
(Just result1) = showCurve . checksum <$> curveLen len1
(Just result2) = showCurve . checksum <$> curveLen len2
putStrLn $ "result part one: " ++ show result1
putStrLn $ "result part two: " ++ show result2