[hs] Solve 2020_01
This commit is contained in:
parent
a8a35f3289
commit
17379cc287
3 changed files with 39 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
5
hs/src/Aoc/Y2020.hs
Normal file
5
hs/src/Aoc/Y2020.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module Aoc.Y2020
|
||||
( solve202001
|
||||
) where
|
||||
|
||||
import Aoc.Y2020.A01
|
||||
32
hs/src/Aoc/Y2020/A01.hs
Normal file
32
hs/src/Aoc/Y2020/A01.hs
Normal file
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue