aoc2020/day1.hs
2020-12-01 20:32:06 +13:00

28 lines
821 B
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 [] = 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 ]
solveB :: [Integer] -> Maybe Integer
solveB [] = Nothing
solveB xs = Just $ (\(x,y,z) -> x * y * z) lsb
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 ]