From 644c38de8b633dc5e03bff5216f68b2bfde4a645 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 9 Dec 2020 11:03:39 +0530 Subject: add initial solution for day09 --- execs/Day08.hs | 4 ++-- execs/Day09.hs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 execs/Day09.hs (limited to 'execs') diff --git a/execs/Day08.hs b/execs/Day08.hs index 7d3fce8..6546990 100644 --- a/execs/Day08.hs +++ b/execs/Day08.hs @@ -40,6 +40,6 @@ genAll (acc:rest) = map (acc:) $ genAll rest main :: IO () main = do n <- map (parseLine . words) . lines <$> readFile "input/08" - let solve1 = run 0 0 Set.empty . Map.fromList . zip [0..] + let solve1 = run 0 0 mempty . Map.fromList . zip [0..] print $ solve1 n - print $ solve1 $ head $ filter (doesEnd 0 0 Set.empty . Map.fromList . zip [0..]) $ genAll n + print $ solve1 $ head $ filter (doesEnd 0 0 mempty . Map.fromList . zip [0..]) $ genAll n diff --git a/execs/Day09.hs b/execs/Day09.hs new file mode 100644 index 0000000..e2e5fd4 --- /dev/null +++ b/execs/Day09.hs @@ -0,0 +1,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 -- cgit v1.2.3