-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
52 lines (41 loc) · 1.21 KB
/
run.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
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative ((<|>))
import Data.Char (isDigit)
import Data.List (isPrefixOf, tails)
import Data.Maybe (mapMaybe)
type N = Int
digits :: (String -> Maybe Char) -> String -> [Char]
digits f = mapMaybe f . tails
digit :: String -> Maybe Char
digit = \case
x:_ | isDigit x -> Just x
_ -> Nothing
wordDigit :: String -> Maybe Char
wordDigit = \case
x
| "one" `isPrefixOf` x -> Just '1'
| "two" `isPrefixOf` x -> Just '2'
| "three" `isPrefixOf` x -> Just '3'
| "four" `isPrefixOf` x -> Just '4'
| "five" `isPrefixOf` x -> Just '5'
| "six" `isPrefixOf` x -> Just '6'
| "seven" `isPrefixOf` x -> Just '7'
| "eight" `isPrefixOf` x -> Just '8'
| "nine" `isPrefixOf` x -> Just '9'
_ -> Nothing
extract :: [Char] -> N
extract xs = read [head xs, last xs]
solve :: (String -> Maybe Char) -> [String] -> N
solve f = sum . map extract . map (digits f)
part1 :: [String] -> N
part1 = solve digit
part2 :: [String] -> N
part2 = solve \x -> digit x <|> wordDigit x
main :: IO ()
main = main' "input.txt"
main' :: FilePath -> IO ()
main' file = do
input <- lines <$> readFile file
print (part1 input)
print (part2 input)