[hs] Clean up parsing a bit
This commit is contained in:
parent
155118b5f2
commit
098214e56e
4 changed files with 41 additions and 30 deletions
|
|
@ -19,7 +19,7 @@ parser = manyLines $ Line
|
|||
<$> (decimal <* char '-')
|
||||
<*> (decimal <* space)
|
||||
<*> (anySingle <* char ':' <* space)
|
||||
<*> untilEol
|
||||
<*> line
|
||||
|
||||
validCount :: Line -> Bool
|
||||
validCount l = n >= lMin l && n <= lMax l
|
||||
|
|
|
|||
|
|
@ -72,10 +72,10 @@ mapFieldsExceptCid f p = [f $ byr p, f $ iyr p, f $ eyr p, f $ hgt p, f $ hcl p,
|
|||
|
||||
pField :: T.Text -> Parser a -> Parser (Field a)
|
||||
pField name p = do
|
||||
notFollowedBy oneSpace
|
||||
notFollowedBy spaceChar
|
||||
void $ string name
|
||||
void $ char ':'
|
||||
(Valid <$> try p) <|> (Invalid <$> untilSpace)
|
||||
(Valid <$> try p) <|> (Invalid <$> lineUntil isSpace)
|
||||
|
||||
nDigits :: Int -> Parser Int
|
||||
nDigits n = do
|
||||
|
|
@ -115,7 +115,7 @@ pHcl :: Parser Passport
|
|||
pHcl = do
|
||||
f <- pField "hcl" $ do
|
||||
void $ char '#'
|
||||
t <- untilSpace
|
||||
t <- lineUntil isSpace
|
||||
guard $ T.length t == 6 && T.all isHexDigit t
|
||||
pure t
|
||||
pure mempty{hcl = f}
|
||||
|
|
@ -138,7 +138,7 @@ pCid = do
|
|||
parser :: Parser [Passport]
|
||||
parser = passport `sepBy` newline
|
||||
where
|
||||
passport = mconcat <$> field `endBy1` oneSpace
|
||||
passport = mconcat <$> field `endBy1` spaceChar
|
||||
field = pByr <|> pIyr <|> pEyr <|> pHgt <|> pHcl <|> pEcl <|> pPid <|> pCid
|
||||
|
||||
hasRequiredKeys :: Passport -> Bool
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ module Aoc.Y2020.D16
|
|||
( day
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.Bifunctor
|
||||
import Data.List
|
||||
|
||||
|
|
@ -16,22 +15,17 @@ import Aoc.Parse
|
|||
data Input = Input [(T.Text, Int -> Bool)] [Int] [[Int]]
|
||||
|
||||
parser :: Parser Input
|
||||
parser = do
|
||||
fields <- many (fieldLine <* newline)
|
||||
void $ string "\nyour ticket:\n"
|
||||
myTicket <- ticket <* newline
|
||||
void $ string "\nnearby tickets:\n"
|
||||
nearbyTickets <- many (ticket <* newline)
|
||||
pure $ Input fields myTicket nearbyTickets
|
||||
parser = Input
|
||||
<$> many (field <* newline)
|
||||
<*> (string "\nyour ticket:\n" *> ticket)
|
||||
<*> (string "\nnearby tickets:\n" *> many ticket)
|
||||
where
|
||||
fieldLine = do
|
||||
name <- takeWhileP Nothing $ \c -> (c /= ':') && (c /= '\n')
|
||||
void $ string ": "
|
||||
(a, b) <- (,) <$> (decimal <* string "-") <*> decimal
|
||||
void $ string " or "
|
||||
(c, d) <- (,) <$> (decimal <* string "-") <*> decimal
|
||||
bound = around (string "-") decimal decimal
|
||||
field = do
|
||||
name <- lineUntil (==':') <* string ": "
|
||||
((a, b), (c, d)) <- around (string " or ") bound bound
|
||||
pure (name, \n -> (a <= n && n <= b) || (c <= n && n <= d))
|
||||
ticket = decimal `sepBy` string ","
|
||||
ticket = (decimal `sepBy` string ",") <* newline
|
||||
|
||||
anyValid :: [(T.Text, Int -> Bool)] -> Int -> Bool
|
||||
anyValid fields n = any (($ n) . snd) fields
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue