aboutsummaryrefslogtreecommitdiff
path: root/2020/1a.hs
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-01-02 21:58:56 +0000
committerAria <me@aria.rip>2023-01-02 21:58:56 +0000
commit5eb58ad076f2cd435b11b140820da224b60b73d5 (patch)
tree2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2020/1a.hs
initial commit
Diffstat (limited to '2020/1a.hs')
-rw-r--r--2020/1a.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/2020/1a.hs b/2020/1a.hs
new file mode 100644
index 0000000..adac6bc
--- /dev/null
+++ b/2020/1a.hs
@@ -0,0 +1,28 @@
+module Day1 where
+
+import Text.Read (readMaybe)
+import System.Environment (getArgs)
+
+-- Cartesian product of a set with itself where ordering doesn't matter
+cp :: [a] -> [(a, a)]
+cp [] = []
+cp (x:xs) = [(x, y) | y <- xs] ++ cp xs
+
+getSomes :: [Maybe a] -> [a]
+getSomes xs = [x | Just x <- xs]
+
+sumEq :: Int -> [Int] -> [(Int, Int)]
+sumEq target = filter (\(x,y) -> x + y == target) . cp
+
+main :: IO ()
+main = do
+ args <- getArgs;
+ content <- readFile $ head args;
+ let l = lines content;
+ let nums = getSomes $ map readMaybe l :: [Int]
+
+ let (a,b) = head $ sumEq 2020 nums;
+ let r = a * b;
+
+ putStrLn $ show a ++ " + " ++ show b ++ " = 2020";
+ putStrLn $ show a ++ " * " ++ show b ++ " = " ++ show r; \ No newline at end of file