2020-12-01 02:27:50 -05:00
|
|
|
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
|
2020-12-01 02:32:06 -05:00
|
|
|
lsa = if lsa' == [] then error "no answer" else head lsa'
|
|
|
|
lsa' = [ (x,y) | x <- xs, y <- xs, x + y == 2020 ]
|
2020-12-01 02:27:50 -05:00
|
|
|
|
|
|
|
solveB :: [Integer] -> Maybe Integer
|
|
|
|
solveB [] = Nothing
|
|
|
|
solveB xs = Just $ (\(x,y,z) -> x * y * z) lsb
|
|
|
|
where
|
2020-12-01 02:32:06 -05:00
|
|
|
lsb = if lsb' == [] then error "no answer" else head lsb'
|
|
|
|
lsb' = [ (x,y,z) | x <- xs, y <- xs, z <- xs, x + y + z == 2020 ]
|