aboutsummaryrefslogtreecommitdiff
path: root/execs/Day09.hs
blob: f1c41f2bbf63076726d5bfd88022617bc7220e0b (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 Utils
import Data.List (inits, tails, find, sort)
import Data.Bifunctor

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