From f6bd85a982cd057cd9c7f573686e50c5d8680b0f Mon Sep 17 00:00:00 2001 From: Laura Orvokki Kursula Date: Sat, 7 Dec 2024 13:04:59 +0100 Subject: 7-1 --- 7-1.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 7-1.hs diff --git a/7-1.hs b/7-1.hs new file mode 100644 index 0000000..81a6ba5 --- /dev/null +++ b/7-1.hs @@ -0,0 +1,22 @@ +combine :: [Int] -> [Int] +combine (x:xs) = foldl f [x] xs + where + f :: [Int] -> Int -> [Int] + f ns n = [(+), (*)] <*> ns <*> [n] + +check :: Int -> [Int] -> Bool +check x xs = elem x (combine xs) + +parseLine :: String -> (Int,[Int]) +parseLine s = (read $ init x, map read xs) + where + (x:xs) = words s + +result :: [(Int,[Int])] -> Int +result xs = sum . map fst . filter snd + $ zip (map fst xs) (map (uncurry check) xs) + +main :: IO () +main = do + ls <- lines <$> getContents + print . result . map parseLine $ ls -- cgit v1.2.3