aboutsummaryrefslogtreecommitdiff
path: root/2020/01/first.hs
diff options
context:
space:
mode:
authorJulien Dessaux2023-03-06 23:48:00 +0100
committerJulien Dessaux2023-03-06 23:48:00 +0100
commitd74ab7641eda7a78dbf1f55d438bdb16bb27dd37 (patch)
tree443f0b8589096b3ad29af6f470e80388fdf6cf1c /2020/01/first.hs
parent2022-01 in haskell (diff)
downloadadvent-of-code-d74ab7641eda7a78dbf1f55d438bdb16bb27dd37.tar.gz
advent-of-code-d74ab7641eda7a78dbf1f55d438bdb16bb27dd37.tar.bz2
advent-of-code-d74ab7641eda7a78dbf1f55d438bdb16bb27dd37.zip
2020-01 part 1 in haskell
Diffstat (limited to '')
-rw-r--r--2020/01/first.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/2020/01/first.hs b/2020/01/first.hs
new file mode 100644
index 0000000..5a615a9
--- /dev/null
+++ b/2020/01/first.hs
@@ -0,0 +1,29 @@
+module Main where
+
+import Control.Monad (when)
+import Data.Maybe (catMaybes, fromJust)
+import System.Exit (die)
+
+exampleExpectedOutput = 514579
+
+compute2020 :: Int -> Int -> Maybe Int
+compute2020 a b
+ | a + b == 2020 = Just (a * b)
+ | otherwise = Nothing
+
+compute :: String -> Int
+compute input = head . catMaybes $ processInput inputList
+ where
+ inputList :: [Int]
+ inputList = map read $ lines input
+ processInput :: [Int] -> [Maybe Int]
+ processInput (_:[]) = [Nothing]
+ processInput (x:xs) = map (compute2020 x) xs ++ processInput xs
+
+main :: IO ()
+main = do
+ example <- readFile "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- readFile "input"
+ print $ compute input