blob: 5a615a9670e2429d259ffa490b7f94282a0c58a4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
|