-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.hs
66 lines (55 loc) · 1.85 KB
/
Day16.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
{-# LANGUAGE TemplateHaskell #-}
module Day16
( part1
, part2
) where
import Control.Monad.Trans.Class (lift)
import Data.ByteString (ByteString)
import Data.Void (Void)
import FlatParse.Stateful (Result (OK), anyAsciiDecimalInt,
ask, char, eof, isLatinLetter,
optional_, runParser, satisfy,
skipSatisfy, some, string, switch)
import qualified FlatParse.Stateful as F (Parser)
type Parser = F.Parser Bool Void Int
type PropParser = F.Parser Bool Void Bool
eol :: F.Parser Bool Void ()
eol = $(char '\n')
propParser :: PropParser
propParser = do
prop <- some $ satisfy isLatinLetter
$(string ": ")
val <- anyAsciiDecimalInt
optional_ $ satisfy (== ',') >> satisfy (== ' ')
isPart2 <- ask
return . test isPart2 prop $ val
parseLine :: Parser
parseLine = do
$(string "Sue ")
sue <- anyAsciiDecimalInt
$(string ": ")
props <- some propParser
if and props
then return sue
else eol >> parseLine
test :: Bool -> String -> Int -> Bool
test _ "children" = (== 3)
test True "cats" = (> 7)
test _ "cats" = (== 7)
test _ "samoyeds" = (== 2)
test True "pomeranians" = (< 3)
test _ "pomeranians" = (== 3)
test _ "akitas" = (== 0)
test _ "vizslas" = (== 0)
test True "goldfish" = (< 5)
test _ "goldfish" = (== 5)
test True "trees" = (> 3)
test _ "trees" = (== 3)
test _ "cars" = (== 2)
test _ "perfumes" = (== 1)
extract :: Result Void Int -> Int
extract (OK result _ _) = result
part1 :: Bool -> ByteString -> String
part1 _ = show . extract . runParser parseLine False 0
part2 :: Bool -> ByteString -> String
part2 _ = show . extract . runParser parseLine True 0