aboutsummaryrefslogtreecommitdiff
path: root/2020/1b.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/1b.hs
initial commit
Diffstat (limited to '2020/1b.hs')
-rw-r--r--2020/1b.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/2020/1b.hs b/2020/1b.hs
new file mode 100644
index 0000000..6cebb47
--- /dev/null
+++ b/2020/1b.hs
@@ -0,0 +1,28 @@
+module Day1 where
+
+import Text.Read (readMaybe)
+import System.Environment (getArgs)
+
+-- All 3-tuples you can take from a list (where ordering doesn't matter)
+cp3 :: [a] -> [(a, a, a)]
+cp3 [] = []
+cp3 (x:xs) = [(x, y, z) | y <- xs, z <- xs] ++ cp3 xs
+
+getSomes :: [Maybe a] -> [a]
+getSomes xs = [x | Just x <- xs]
+
+sumEq :: Int -> [Int] -> [(Int, Int, Int)]
+sumEq target = filter (\(x,y,z) -> x + y + z == target) . cp3
+
+main :: IO ()
+main = do
+ args <- getArgs;
+ content <- readFile $ head args;
+ let l = lines content;
+ let nums = getSomes $ map readMaybe l :: [Int]
+
+ let (a,b,c) = head $ sumEq 2020 nums;
+ let r = a * b * c;
+
+ putStrLn $ show a ++ " + " ++ show b ++ " + " ++ show c ++ " = 2020";
+ putStrLn $ show a ++ " * " ++ show b ++ " * " ++ show c ++ " = " ++ show r; \ No newline at end of file