cleaned up solution

This commit is contained in:
Thorn Avery 2020-12-01 20:41:14 +13:00
parent f7781d2f65
commit e4f146f68f

25
day1.hs
View File

@ -13,15 +13,22 @@ main = do
(Just x) -> putStrLn $ "day1b: " ++ (show x) (Just x) -> putStrLn $ "day1b: " ++ (show x)
solveA :: [Integer] -> Maybe Integer solveA :: [Integer] -> Maybe Integer
solveA [] = Nothing solveA = solveX (\(x,y) -> x * y)
solveA xs = Just $ (\(x,y) -> x * y) lsa (\xs -> [ (x,y) | x <- xs
where , y <- xs
lsa = if lsa' == [] then error "no answer" else head lsa' , x + y == 2020 ])
lsa' = [ (x,y) | x <- xs, y <- xs, x + y == 2020 ]
solveB :: [Integer] -> Maybe Integer solveB :: [Integer] -> Maybe Integer
solveB [] = Nothing solveB = solveX (\(x,y,z) -> x * y * z)
solveB xs = Just $ (\(x,y,z) -> x * y * z) lsb (\xs -> [ (x,y,z) | x <- xs
, y <- xs
, z <- xs
, x + y + z == 2020 ])
solveX :: Eq a => (a -> Integer) -> ([Integer] -> [a]) -> [Integer] -> Maybe Integer
solveX _ _ [] = Nothing
solveX f g l = if ls == []
then Nothing
else Just $ (f . head) ls
where where
lsb = if lsb' == [] then error "no answer" else head lsb' ls = g l
lsb' = [ (x,y,z) | x <- xs, y <- xs, z <- xs, x + y + z == 2020 ]