35 lines
1.0 KiB
Haskell
35 lines
1.0 KiB
Haskell
module Main where
|
|
|
|
main :: IO ()
|
|
main = do
|
|
raw <- readFile "day1.txt"
|
|
let ls = map read $ lines raw :: [Integer]
|
|
in do
|
|
case (solveA ls) of
|
|
Nothing -> error "no answer"
|
|
(Just x) -> putStrLn $ "day1a: " ++ (show x)
|
|
case (solveB ls) of
|
|
Nothing -> error "no answer"
|
|
(Just x) -> putStrLn $ "day1b: " ++ (show x)
|
|
|
|
solveA :: [Integer] -> Maybe Integer
|
|
solveA = solveX (\(x,y) -> x * y)
|
|
(\xs -> [ (x,y) | x <- xs
|
|
, y <- xs
|
|
, x + y == 2020 ])
|
|
|
|
solveB :: [Integer] -> Maybe Integer
|
|
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
|
|
ls = g l
|