You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0KB

  1. module Main where
  2. main :: IO ()
  3. main = do
  4. raw <- readFile "day1.txt"
  5. let ls = map read $ lines raw :: [Integer]
  6. in do
  7. case (solveA ls) of
  8. Nothing -> error "no answer"
  9. (Just x) -> putStrLn $ "day1a: " ++ (show x)
  10. case (solveB ls) of
  11. Nothing -> error "no answer"
  12. (Just x) -> putStrLn $ "day1b: " ++ (show x)
  13. solveA :: [Integer] -> Maybe Integer
  14. solveA = solveX (\(x,y) -> x * y)
  15. (\xs -> [ (x,y) | x <- xs
  16. , y <- xs
  17. , x + y == 2020 ])
  18. solveB :: [Integer] -> Maybe Integer
  19. solveB = solveX (\(x,y,z) -> x * y * z)
  20. (\xs -> [ (x,y,z) | x <- xs
  21. , y <- xs
  22. , z <- xs
  23. , x + y + z == 2020 ])
  24. solveX :: Eq a => (a -> Integer) -> ([Integer] -> [a]) -> [Integer] -> Maybe Integer
  25. solveX _ _ [] = Nothing
  26. solveX f g l = if ls == []
  27. then Nothing
  28. else Just $ (f . head) ls
  29. where
  30. ls = g l