1
0
Fork 0
advent-of-code/2020/09/part1.hs

23 lines
616 B
Haskell
Raw Normal View History

2022-09-21 09:19:52 -04:00
#!/usr/bin/env runghc
import qualified Data.Set as S
import Data.List (subsequences)
import Data.Either (fromLeft, lefts)
import Day9
main = interact (show . solve . parse)
solve = head . lefts . verify 25
verify :: Int -> [Int] -> [Either Int Int]
verify len xs
| length xs < len + 1 = []
| otherwise = verified:(verify len $ tail xs)
where
(preamble, [x]) = splitAt len $ take (len + 1) xs
pairs = combinations 2 preamble
sums = S.fromList $ map sum pairs
verified = if S.member x sums then (Right x) else (Left x)
combinations n xs = filter ((n==) . length) $ subsequences xs