-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.hs
106 lines (78 loc) · 2.61 KB
/
Day01.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
module Day01 where
import Data.Char
import Data.List
import Data.Maybe
import Text.ParserCombinators.ReadP
import Harness
import ParseHelper
-- this one uses a different parser for pt 1 & pt 2, so need to swap these lines
main :: IO ()
main =
-- getInputAndSolve (parseInput parseDigitsPt1) literalCalibrationValueSum (const "swap commented lines for pt2")
getInputAndSolve (parseInput parseDigitsPt2) (const "swap commented lines for pt1") realCalibrationValueSum
-- SOLVE
literalCalibrationValueSum :: [Digits] -> Int
literalCalibrationValueSum = sum . map toCalibrationValue
realCalibrationValueSum :: [Digits] -> Int
realCalibrationValueSum = sum . map toCalibrationValue
-- HELPERS
toCalibrationValue :: Digits -> Int
toCalibrationValue digits =
asInt (firstDigit digits) * 10 + asInt (lastDigit digits)
-- PARSE
data Digits = Digits
{ firstDigit :: Digit
, lastDigit :: Digit
}
deriving (Show)
data Digit
= Literal Int
| Spelt Int
deriving (Show)
onlyLiterals :: [Digit] -> [Int]
onlyLiterals = mapMaybe $ \case
Literal i -> Just i
Spelt _ -> Nothing
asInt :: Digit -> Int
asInt = \case
Literal i -> i
Spelt i -> i
parseDigitsPt1 :: ReadP Digits
parseDigitsPt1 = do
digitList <- map Literal . onlyLiterals <$> parseDigitList
return $ Digits (head digitList) (last digitList)
parseDigitsPt2 :: ReadP Digits
parseDigitsPt2 = do
-- parse the whole line
allChars <- many1 $ satisfy isAlphaNum
-- get first digit by parsing from left to right
let firstDigit = head . fromJust $ runParser parseDigitList allChars
-- get last digit by parsing from right to left
-- use longer and longer substrings until a parse succeeds
let ltrSubstrings = drop 1 $ reverse $ tails allChars
ltrResults = mapMaybe (runParser parseDigitList) ltrSubstrings
lastDigit = head $ head $ filter (not . null) ltrResults
return Digits {..}
parseDigitList :: ReadP [Digit]
parseDigitList = do
fmap catMaybes
. many1
$ (Just <$> parseDigit)
<++ (Nothing <$ satisfy isAlpha)
where
parseDigit :: ReadP Digit
parseDigit =
choice
[ Literal . read . (: []) <$> satisfy isNumber
, Spelt 1 <$ string "one"
, Spelt 2 <$ string "two"
, Spelt 3 <$ string "three"
, Spelt 4 <$ string "four"
, Spelt 5 <$ string "five"
, Spelt 6 <$ string "six"
, Spelt 7 <$ string "seven"
, Spelt 8 <$ string "eight"
, Spelt 9 <$ string "nine"
]