aboutsummaryrefslogtreecommitdiff
path: root/2020/10a.hs
diff options
context:
space:
mode:
Diffstat (limited to '2020/10a.hs')
-rw-r--r--2020/10a.hs29
1 files changed, 29 insertions, 0 deletions
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 ();