summary refs log tree commit diff
path: root/1/2.hs
blob: a5ba754ce49e42dff6decf7926df8e5843f3121e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import Prelude
import Text.Read
import Data.Maybe
import Data.List
import Data.Monoid
import System.Environment

digits = ["one","two","three","four","five","six","seven","eight","nine"]

parse [] = (mempty,mempty)

parse l@(x:xs)
  | Just i <- findIndex (`isPrefixOf` l) digits = f (Just $ i+1) <> parse xs
  | otherwise = f (readMaybe [x]) <> parse xs
  where
    f = (,) . First <*> Last

add (First x, Last y) = (+) . (*10) <$> x <*> y

main = head <$> getArgs >>= readFile >>= print . sum . catMaybes . map (add . parse) <$> lines