diff --git a/hs/app/Main.hs b/hs/app/Main.hs index 1431398..cdfac72 100644 --- a/hs/app/Main.hs +++ b/hs/app/Main.hs @@ -3,6 +3,7 @@ module Main where import Options.Applicative import Aoc.Y2019 +import Aoc.Y2020 data Settings = Settings { function :: FilePath -> IO () @@ -16,6 +17,7 @@ solutions = subparser $ mconcat $ map (\(name, func) -> command name (info (pure , ("2019_03", solve201903) , ("2019_04", solve201904) , ("2019_05", solve201905) + , ("2020_01", solve202001) ] parser :: Parser Settings diff --git a/hs/src/Aoc/Y2020.hs b/hs/src/Aoc/Y2020.hs new file mode 100644 index 0000000..1438bc6 --- /dev/null +++ b/hs/src/Aoc/Y2020.hs @@ -0,0 +1,5 @@ +module Aoc.Y2020 + ( solve202001 + ) where + +import Aoc.Y2020.A01 diff --git a/hs/src/Aoc/Y2020/A01.hs b/hs/src/Aoc/Y2020/A01.hs new file mode 100644 index 0000000..f689ca3 --- /dev/null +++ b/hs/src/Aoc/Y2020/A01.hs @@ -0,0 +1,32 @@ +module Aoc.Y2020.A01 + ( solve202001 + ) where + +import Control.Monad + +findPair :: [Integer] -> (Integer, Integer) +findPair l = head $ do + a <- l + b <- l + guard $ a + b == 2020 + pure (a, b) + +findTriple :: [Integer] -> (Integer, Integer, Integer) +findTriple l = head $ do + a <- l + b <- l + c <- l + guard $ a + b + c == 2020 + pure (a, b, c) + +solve202001 :: FilePath -> IO () +solve202001 f = do + values <- map read . lines <$> readFile f + + putStrLn ">> Part 1" + let (x1, x2) = findPair values + putStrLn $ show x1 ++ " * " ++ show x2 ++ " = " ++ show (x1 * x2) + + let (y1, y2, y3) = findTriple values + putStrLn ">> Part 2" + putStrLn $ show y1 ++ " * " ++ show y2 ++ " * " ++ show y3 ++ " = " ++ show (y1 * y2 * y3)