From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/10a.hs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2020/10a.hs (limited to '2020/10a.hs') diff --git a/2020/10a.hs b/2020/10a.hs new file mode 100644 index 0000000..5df6941 --- /dev/null +++ b/2020/10a.hs @@ -0,0 +1,29 @@ +module Day10A where + +import System.Environment (getArgs) +import Data.List (sort) + +-- Get the longest chain of adapters possible +getJoltsChain :: [Int] -> [Int] +getJoltsChain ss = xs ++ [last xs + 3] + where xs = sort ss + +-- Read a line-seperated file of numbers +numsFromFile :: String -> IO [Int] +numsFromFile p = do + c <- readFile p; + return $ map read $ lines c; + +main :: IO () +main = do + args <- getArgs; + xs <- numsFromFile $ head args; + + let chain = 0 : getJoltsChain xs; + + let diffs = zipWith (-) chain (drop 1 chain); + let diff3s = length $ filter (== -3) diffs; + let diff1s = length $ filter (== -1) diffs; + + print $ diff1s * diff3s; + return (); -- cgit v1.2.3