aboutsummaryrefslogtreecommitdiff
path: root/execs/Day09.hs
blob: 7bf4908b45182ec9e093b6764f72d3a5722c03a1 (plain)
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
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