aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorJulien Dessaux2023-03-07 00:53:29 +0100
committerJulien Dessaux2023-03-07 00:53:29 +0100
commit96efc40e53c0e01e97beb0af27da54fee82aa1b6 (patch)
tree67624940b797cc6b6bb240f00d3fd54ff1d4c6a9 /2020
parent2020-01 part 1 in haskell (diff)
downloadadvent-of-code-96efc40e53c0e01e97beb0af27da54fee82aa1b6.tar.gz
advent-of-code-96efc40e53c0e01e97beb0af27da54fee82aa1b6.tar.bz2
advent-of-code-96efc40e53c0e01e97beb0af27da54fee82aa1b6.zip
2020-01 part 2 in haskell
Diffstat (limited to '')
-rw-r--r--2020/01/second.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/2020/01/second.hs b/2020/01/second.hs
new file mode 100644
index 0000000..9c7f3b3
--- /dev/null
+++ b/2020/01/second.hs
@@ -0,0 +1,32 @@
+module Main where
+
+import Control.Monad (when)
+import Data.Maybe (catMaybes, fromJust)
+import System.Exit (die)
+
+exampleExpectedOutput = 241861950
+
+compute2020 :: Int -> Int -> Int -> Maybe Int
+compute2020 a b c
+ | a + b + c == 2020 = Just (a * b * c)
+ | 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) = (processA $ compute2020 x) xs ++ processInput xs
+ processA :: (Int -> Int -> Maybe Int) -> [Int] -> [Maybe Int]
+ processA f (_:[]) = [Nothing]
+ processA f (x:xs) = map (f x) xs ++ processA f 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