-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.hs
41 lines (34 loc) · 1.2 KB
/
day1.hs
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
32
33
34
35
36
37
38
39
40
41
import Data.Char
import Data.List
import Data.Maybe
getDigits :: String -> [Int]
getDigits [] = []
getDigits (x : xs)
| isDigit x = digitToInt x : getDigits xs
| otherwise = getDigits xs
getDigits2 :: String -> [Int]
getDigits2 [] = []
getDigits2 line
| "one" `isPrefixOf` line = 1 : getDigits2 (tail line)
| "two" `isPrefixOf` line = 2 : getDigits2 (tail line)
| "three" `isPrefixOf` line = 3 : getDigits2 (tail line)
| "four" `isPrefixOf` line = 4 : getDigits2 (tail line)
| "five" `isPrefixOf` line = 5 : getDigits2 (tail line)
| "six" `isPrefixOf` line = 6 : getDigits2 (tail line)
| "seven" `isPrefixOf` line = 7 : getDigits2 (tail line)
| "eight" `isPrefixOf` line = 8 : getDigits2 (tail line)
| "nine" `isPrefixOf` line = 9 : getDigits2 (tail line)
getDigits2 (x : xs)
| isDigit x = digitToInt x : getDigits2 xs
| otherwise = getDigits2 xs
getFirstLast :: [Int] -> Int
getFirstLast lst = head lst * 10 + last lst
main :: IO ()
main = do
input <- fmap lines (readFile "input")
let nums = map getDigits input
let numsWithWords = map getDigits2 input
let calibSum = sum (map getFirstLast nums)
let calibSum2 = sum (map getFirstLast numsWithWords)
print calibSum
print calibSum2