diff options
author | Julien Dessaux | 2023-03-20 00:35:17 +0100 |
---|---|---|
committer | Julien Dessaux | 2023-03-20 00:35:17 +0100 |
commit | f5eb3e1a98d7ec0aea08bc640ec341178d4750e6 (patch) | |
tree | bc58bf14f1ac70fd14eccb2a5ff4f411bda9b92c /2020/10-Adapter_Array/first.hs | |
parent | small style fixes (diff) | |
download | advent-of-code-f5eb3e1a98d7ec0aea08bc640ec341178d4750e6.tar.gz advent-of-code-f5eb3e1a98d7ec0aea08bc640ec341178d4750e6.tar.bz2 advent-of-code-f5eb3e1a98d7ec0aea08bc640ec341178d4750e6.zip |
2020-10 in haskell
Diffstat (limited to '2020/10-Adapter_Array/first.hs')
-rw-r--r-- | 2020/10-Adapter_Array/first.hs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2020/10-Adapter_Array/first.hs b/2020/10-Adapter_Array/first.hs new file mode 100644 index 0000000..0157a3b --- /dev/null +++ b/2020/10-Adapter_Array/first.hs @@ -0,0 +1,31 @@ +module Main (main) where +import Control.Monad (when) +import Data.List (foldl', sort) +import System.Exit (die) + +exampleExpectedOutput = 35 +exampleExpectedOutput2 = 220 + +parseInput :: String -> IO [Int] +parseInput filename = do + input <- readFile filename + return $ map read $ lines input + +compute :: [Int] -> Int +compute jolts = result $ foldl' compute' (0, 0, 0) $ sort jolts + where + compute' (prev, ones, threes) jolt + | jolt - prev == 1 = (jolt, ones + 1, threes) + | jolt - prev == 3 = (jolt, ones, threes + 1) + result (_, ones, threes) = ones * (threes + 1) + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + example2 <- parseInput "example2" + let exampleOutput2 = compute example2 + when (exampleOutput2 /= exampleExpectedOutput2) (die $ "example2 failed: got " ++ show exampleOutput2 ++ " instead of " ++ show exampleExpectedOutput2) + input <- parseInput "input" + print $ compute input |