diff options
Diffstat (limited to '')
-rw-r--r-- | 2020/10-Adapter_Array/#second.hs# | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/2020/10-Adapter_Array/#second.hs# b/2020/10-Adapter_Array/#second.hs# new file mode 100644 index 0000000..8e581a0 --- /dev/null +++ b/2020/10-Adapter_Array/#second.hs# @@ -0,0 +1,36 @@ +module Main (main) where +import Control.Monad (when) +import Data.List (group, sort) +import System.Exit (die) + +exampleExpectedOutput = 8 +exampleExpectedOutput2 = 19208 + +parseInput :: String -> IO [Int] +parseInput filename = do + input <- readFile filename + return $ map read $ lines input + +compute :: [Int] -> Int +compute jolts = product $ map (([1, 1, 2, 4, 7, 13, 24] !!) . length) $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s) --snd . head $ foldl compute' [(0, 1)] $ sort jolts + where + s = sort jolts + --compute' (prev, n) x = + --compute' (x:xs) = (length $ takeWhile (\i -> i <= x + 3) xs) * compute' xs + +main :: IO () +main = do + example <- parseInput "example" + let s = sort example + print s + print $ zipWith (-) s (0 : s) + print $ group $ zipWith (-) s (0 : s) + print $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s) + print $ map length $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s) + 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 |