aoc

advent of code
git clone git://source.orangerot.dev:/aoc.git
Log | Files | Refs

Main_clean.hs (771B)


      1 import Data.List
      2 import Data.Char
      3 
      4 wordDict = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
      5 
      6 findFirst :: String -> [String] -> Int
      7 findFirst [] _ = 0
      8 findFirst s@(x:xs) wordDict
      9   | isDigit x = read [x]
     10   | Just y <- elemIndex (take 3 s) wordDict = y -- not needed for exercise 1
     11   | Just y <- elemIndex (take 4 s) wordDict = y -- not needed for exercise 1
     12   | Just y <- elemIndex (take 5 s) wordDict = y -- not needed for exercise 1
     13   | otherwise = findFirst xs wordDict
     14 
     15 findLast s wordDict = findFirst (reverse s) (map reverse wordDict)
     16 
     17 main :: IO ()
     18 main = do
     19   inputLines <- lines <$> getContents
     20 
     21   let intList = map (\x -> findFirst x wordDict * 10 + findLast x wordDict) inputLines
     22   let result = sum intList
     23 
     24   print result
     25