diff options
| -rw-r--r-- | 7-2.hs | 25 | 
1 files changed, 25 insertions, 0 deletions
| @@ -0,0 +1,25 @@ +cat :: Int -> Int -> Int +cat x y = x * 10^(floor (logBase 10 (fromIntegral y)) + 1) + y + +combine :: [Int] -> [Int] +combine (x:xs) = foldl f [x] xs +  where +    f :: [Int] -> Int -> [Int] +    f ns n = [(+), (*), cat] <*> 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 |