module Main where import Data.Bifunctor import Data.List (find, inits, sort, tails) import Utils parseLine :: String -> Int parseLine = read doCheck :: [Int] -> Int -> Bool doCheck preamble target = target `elem` p where p = [x + y | x <- preamble, y <- preamble, x /= y] checkAll :: [[Int]] -> [Int] -> [(Int, Bool)] checkAll = zipWith (\p t -> (t, doCheck p t)) findWeakness :: [[Int]] -> Int -> Int findWeakness subs target = minimum t + maximum t where Just t = find ((== target) . sum) subs main :: IO () main = do n <- map parseLine . lines <$> readFile "input/09" let preambleLen = 25 Just (target, _) = find (not . snd) $ checkAll (windows preambleLen n) (drop preambleLen n) print target print $ findWeakness (sublists n) target