{-# LANGUAGE OverloadedStrings #-}
import Data.Char
import Control.Arrow
import qualified Data.Text as T


main :: IO ()
main = do
  let nums = [("one", "o1ne"), ("two", "t2wo"), ("three", "th3ree"), ("four", "fo4ur"), ("five", "fi5ve"), ("six", "si6x"), ("seven", "se7ven"), ("eight", "eig8ht"), ("nine", "ni9ne")]
  input <- init . T.splitOn "\n" . T.pack <$> readFile "input"
  print $ day1a input
  print $ day1a $ day1b input nums

day1a :: [T.Text] -> Int
day1a = sum . map ((T.head &&& T.last >>> (\(x, y) -> read [x,y])) . T.filter isDigit)

day1b :: [T.Text] -> [(T.Text,T.Text)] -> [T.Text]
day1b txt nums = map numfold txt
    where rep z (x,y) = T.replace x y z
          numfold x = foldl rep x nums
