Browse Source

cleaned up solution

master
Thorn Avery 3 years ago
parent
commit
e4f146f68f
1 changed files with 16 additions and 9 deletions
  1. +16
    -9
      day1.hs

+ 16
- 9
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

Loading…
Cancel
Save