haskell: 2020 01 simplify

This commit is contained in:
Maciej Jur 2023-10-18 22:02:41 +02:00
parent ba4dc0e1f6
commit 1903ffad9a
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD

View file

@ -14,18 +14,15 @@ example =
, 1456 , 1456
] ]
scout :: Int -> [[Integer]] -> Maybe [[Integer]] scout :: Int -> [[a]] -> [[a]]
scout _ [] = Nothing scout _ [] = []
scout 0 _ = Just [] scout 0 _ = []
scout 1 (h:_) = Just $ map (:[]) h scout 1 (h:_) = map (:[]) h
scout n (h:t) = do scout n (h:t) = map (head h:) (scout (n-1) t) <> scout n t
a <- map (head h:) <$> scout (n-1) t
let b = fromMaybe [] $ scout n t
pure $ a <> b
solve :: Int -> [Integer] -> Integer 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 :: [Integer] -> Integer
solve1 = solve 2 solve1 = solve 2