-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseHelper.hs
114 lines (94 loc) · 3.21 KB
/
ParseHelper.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
107
108
109
110
111
112
113
114
-- | Helper functions for parsing problem inputs.
module ParseHelper where
import Data.Char (isDigit)
import Data.Functor
import Data.List (sortOn)
import Data.Maybe (listToMaybe)
import Text.ParserCombinators.ReadP
import GHC.Arr qualified as A
-- | Run a parser on each line of the input file.
parseInput :: (Show x) => ReadP x -> String -> [x]
parseInput parser =
parseInputRaw $ sepBy parser newline <* end
where
end = many $ choice [void newline, eof]
-- | Run a parser on the full input file.
parseInputRaw :: (Show x) => ReadP x -> String -> x
parseInputRaw parser str =
let results = readP_to_S parser str
successes = filter ((== "") . snd) results
longestAttempts = sortOn (length . snd) results
in case listToMaybe successes of
Nothing ->
error $ "Parsing failure, longest attempt: " <> show (head longestAttempts)
Just success ->
fst success
-- | Run a parser
runParser :: ReadP x -> String -> Maybe x
runParser parser str =
let results = readP_to_S parser str
successes = filter ((== "") . snd) results
in fst <$> listToMaybe successes
-- | Parse a newline or a carriage-return & newline.
newline :: ReadP Char
newline =
choice
[ char '\r' *> char '\n'
, char '\n'
]
-- | Parse a positive or negative integer.
parseInt :: ReadP Int
parseInt = do
sign <- option 1 (char '-' $> (-1))
(sign *) . read <$> many1 (satisfy isDigit)
-- | Parse a comma-separated array of ints, with optional beginning & end
-- characters.
parseIntArray :: Maybe Char -> Maybe Char -> ReadP [Int]
parseIntArray maybeStart maybeEnd = do
mapM_ char maybeStart
ints <- sepBy parseInt (skipSpaces *> char ',' *> skipSpaces)
mapM_ char maybeEnd
return ints
-- | Parse a grid of digits with no column separators & rows separated by
-- newlines.
parseIntGrid :: ReadP (A.Array (Int, Int) Int)
parseIntGrid = do
ls <- sepBy (many1 $ satisfy isDigit) newline <* newline
let height = length ls
width = minimum $ map length ls
return $
A.array
((0, 0), (height - 1, width - 1))
[ ((w, h), c)
| h <- [0 .. height - 1]
, w <- [0 .. width - 1]
, let c = read [ls !! h !! w]
]
-- | Parse a grid of characters.
parseCharGrid :: (Char -> Bool) -> ReadP (A.Array (Int, Int) Char)
parseCharGrid validChar = do
ls <- sepBy (many1 $ satisfy validChar) newline
let height = length ls
width = minimum $ map length ls
return $
A.array
((0, 0), (width - 1, height - 1))
[ ((w, h), c)
| h <- [0 .. height - 1]
, w <- [0 .. width - 1]
, let c = ls !! h !! w
]
-- | Parse a grid, converting each character into a discrete type.
parseValueGrid :: ReadP a -> ReadP (A.Array (Int, Int) a)
parseValueGrid parseChar = do
ls <- sepBy (many1 parseChar) newline
let height = length ls
width = minimum $ map length ls
return $
A.array
((0, 0), (width - 1, height - 1))
[ ((w, h), c)
| h <- [0 .. height - 1]
, w <- [0 .. width - 1]
, let c = ls !! h !! w
]