From 1903ffad9a14bf505907aee64e8f7f6a6488be89 Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Wed, 18 Oct 2023 22:02:41 +0200 Subject: [PATCH] haskell: 2020 01 simplify --- 2020/Haskell/day01.hs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/2020/Haskell/day01.hs b/2020/Haskell/day01.hs index a3fe427..4749ec5 100644 --- a/2020/Haskell/day01.hs +++ b/2020/Haskell/day01.hs @@ -14,18 +14,15 @@ example = , 1456 ] -scout :: Int -> [[Integer]] -> Maybe [[Integer]] -scout _ [] = Nothing -scout 0 _ = Just [] -scout 1 (h:_) = Just $ map (:[]) h -scout n (h:t) = do - a <- map (head h:) <$> scout (n-1) t - let b = fromMaybe [] $ scout n t - pure $ a <> b +scout :: Int -> [[a]] -> [[a]] +scout _ [] = [] +scout 0 _ = [] +scout 1 (h:_) = map (:[]) h +scout n (h:t) = map (head h:) (scout (n-1) t) <> scout n t solve :: Int -> [Integer] -> Integer -solve n = product . head . filter ((2020==) . sum) . fromMaybe [] . scout n . tails +solve n = product . head . filter ((2020==) . sum) . scout n . tails solve1 :: [Integer] -> Integer solve1 = solve 2