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)
solveA :: [Integer] -> Maybe Integer
solveA [] = Nothing
solveA xs = Just $ (\(x,y) -> x * y) lsa
where
lsa = if lsa' == [] then error "no answer" else head lsa'
lsa' = [ (x,y) | x <- xs, y <- xs, x + y == 2020 ]
solveA = solveX (\(x,y) -> x * y)
(\xs -> [ (x,y) | x <- xs
, y <- xs
, x + y == 2020 ])
solveB :: [Integer] -> Maybe Integer
solveB [] = Nothing
solveB xs = Just $ (\(x,y,z) -> x * y * z) lsb
solveB = solveX (\(x,y,z) -> x * y * z)
(\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
lsb = if lsb' == [] then error "no answer" else head lsb'
lsb' = [ (x,y,z) | x <- xs, y <- xs, z <- xs, x + y + z == 2020 ]
ls = g l