From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/1b.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2020/1b.hs (limited to '2020/1b.hs') 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 -- cgit v1.2.3