1
0
Fork 0
advent-of-code/2020/09/part1.hs
Sloane Perrault 8b6a1e0ce8 dnf: day 9
2022-09-21 09:19:52 -04:00

22 lines
616 B
Haskell
Executable file

#!/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