blob: 051ed9f929726a874b2bd311bbae013c41de5ca5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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 = getContents >>= print . result . map parseLine . lines
|