blob: a99fabf637f75eec4612d62c5a7a3bc336219468 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import Data.Char
import Data.List
import System.Environment
import System.IO
fixInput str
| "one" `isPrefixOf` str = '1'
| "two" `isPrefixOf` str = '2'
| "three" `isPrefixOf` str = '3'
| "four" `isPrefixOf` str = '4'
| "five" `isPrefixOf` str = '5'
| "six" `isPrefixOf` str = '6'
| "seven" `isPrefixOf` str = '7'
| "eight" `isPrefixOf` str = '8'
| "nine" `isPrefixOf` str = '9'
| otherwise = head str
main :: IO ()
main = do
args <- getArgs
case args of
[filename] -> do
fh <- openFile filename ReadMode
lines <- lines <$> readFile filename
let fixed = [ [ fixInput part | part <- tails line, part /= "" ] | line <- lines ]
let digits = map (\line -> [c | c <- line, isDigit c ]) fixed
let numbers = map (\line -> head line : [last line]) digits
let ints = map (\num -> read num :: Int ) numbers
let part2 = sum ints
print part2
_ -> putStrLn "Missing input filename."
|