From ac3191ae40b8df0873f2e04f5bc1017322941b5d Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 9 Dec 2020 11:43:55 +0530 Subject: be more <<>> --- execs/Day09.hs | 17 +++++------------ lib/Utils.hs | 4 ++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/execs/Day09.hs b/execs/Day09.hs index e2e5fd4..f1c41f2 100644 --- a/execs/Day09.hs +++ b/execs/Day09.hs @@ -2,7 +2,7 @@ module Main where import Utils -import Data.List (inits, tails, find) +import Data.List (inits, tails, find, sort) import Data.Bifunctor parseLine :: String -> Int @@ -12,24 +12,17 @@ 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 - +checkAll :: [[Int]] -> [Int] -> [(Int, Bool)] +checkAll = zipWith (\p t -> (t, doCheck p t)) findWeakness :: [[Int]] -> Int -> Int -findWeakness subs target = mn + mx +findWeakness subs target = minimum t + maximum t 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 + Just (target, _) = find (not . snd) $ checkAll (windows preambleLen n) (drop preambleLen n) print target print $ findWeakness (sublists n) target diff --git a/lib/Utils.hs b/lib/Utils.hs index 89b1bd3..1381f16 100644 --- a/lib/Utils.hs +++ b/lib/Utils.hs @@ -6,6 +6,7 @@ module Utils ( binaryToInt , (&+) , howMany , sublists + , windows ) where import Data.Char (digitToInt) @@ -39,3 +40,6 @@ howMany predicate = length . filter predicate sublists :: [a] -> [[a]] sublists = concatMap inits . tails + +windows :: Int -> [a] -> [[a]] +windows m = foldr (zipWith (:)) (repeat []) . take m . tails -- cgit v1.2.3