adventofcode/2023/app/Day1.hs

34 lines
893 B
Haskell
Raw Normal View History

2023-12-02 21:19:43 +01:00
module Day1 where
import System.IO
import Data.List
import Data.Char
import Data.Maybe
import Data.List.Extra
getFirstInt :: String -> Int
getFirstInt xs = read $ singleton $ fromMaybe '0' $ find isDigit xs
restoreLine :: String -> Int
restoreLine l = 10 * getFirstInt l + getFirstInt (reverse l)
main :: IO ()
main = do
handle <- openFile "app/Day1-input.txt" ReadMode
input <- hGetContents handle
putStr "Part 1: "
print $ sum $ map restoreLine (words input)
putStr "Part 2: "
print $ sum $ map restoreLine $ words
$ replace "zero" "z0o"
$ replace "one" "o1e"
$ replace "two" "t2o"
$ replace "three" "t3e"
$ replace "four" "f4r"
$ replace "five" "f5e"
$ replace "six" "s6x"
$ replace "seven" "s7n"
$ replace "eight" "e8t"
$ replace "nine" "n9e" input
pure ()