Main.hs (936B)
1 import Data.List 2 import Data.Char 3 4 -- isDigit :: Char -> Bool 5 -- isDigit c = c >= '0' && c <= '9' 6 7 wordDict = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] 8 9 findFirst :: String -> [String] -> Int 10 findFirst [] _ = 0 11 findFirst s@(x:xs) wordDict 12 | isDigit x = read [x] 13 | Just y <- elemIndex (take 3 s) wordDict = y 14 | Just y <- elemIndex (take 4 s) wordDict = y 15 | Just y <- elemIndex (take 5 s) wordDict = y 16 | otherwise = findFirst xs wordDict 17 18 rev [] = [] 19 rev (x:xs) = rev xs ++ [x] 20 21 main :: IO () 22 main = do 23 inputLines <- lines <$> getContents 24 25 let filterLines = map (filter isDigit) inputLines 26 let outerChars = map (\x -> [head x, last x]) filterLines 27 mapM_ putStrLn outerChars 28 29 let intList = map read outerChars :: [Int] 30 -- let intList = map (\x -> findFirst x wordDict * 10 + findFirst (rev x) (map rev wordDict)) inputLines 31 let result = sum intList 32 33 putStrLn $ show result 34