blob: e2e5fd4d67e8b1a5f201072d26f25d9fb62b388d (
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
29
30
31
32
33
34
35
|
module Main where
import Utils
import Data.List (inits, tails, find)
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 preamble [x] = [(x, doCheck preamble x)]
checkAll preamble@(p:ps) (x:xs) = (x, doCheck preamble x) : checkAll (ps++[x]) xs
findWeakness :: [[Int]] -> Int -> Int
findWeakness subs target = mn + mx
where Just t = find ((== target) . sum) subs
mn = minimum t
mx = maximum t
main :: IO ()
main = do
n <- map parseLine . lines <$> readFile "input/09"
let preambleLen = 25
Just (target, _) = find (not . snd)
$ uncurry checkAll
$ bimap (take preambleLen) (drop preambleLen)
$ (,) n n
print target
print $ findWeakness (sublists n) target
|