-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.hs
72 lines (60 loc) · 1.86 KB
/
Day2.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
{-# LANGUAGE TemplateHaskell #-}
module Day2
( part1
, part2
) where
import Data.Bifunctor (second)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (unpack)
import Data.Text (Text, pack)
import FlatParse.Basic (anyAsciiDecimalInt, char, optional_,
runParser, some, string, switch,
(<|>))
import Helpers.Parsers.FlatParse (Parser, extract)
import Helpers.Parsers.Text (characters, signedInts)
type Game = (Int, Round)
type Round = [(Int, Colour)]
data Colour
= Blue
| Red
| Green
deriving (Eq)
parseInput :: Parser [Game]
parseInput = some parseLine
parseLine :: Parser Game
parseLine = do
$(string "Game ")
iD <- anyAsciiDecimalInt
$(string ": ")
pairs <- some parsePair
$(char '\n')
pure (iD, pairs)
parsePair :: Parser (Int, Colour)
parsePair = do
count <- anyAsciiDecimalInt
$(char ' ')
colour <-
$(switch
[|case _ of
"blue" -> pure Blue
"red" -> pure Red
"green" -> pure Green|])
optional_ (($(char ',') <|> $(char ';')) >> $(char ' '))
pure (count, colour)
countPossible :: [Game] -> Int
countPossible = sum . map fst . filter (all validPair . snd)
where
validPair (b, Blue) = b <= 14
validPair (g, Green) = g <= 13
validPair (r, Red) = r <= 12
power :: Game -> Int
power (_, round) = red * green * blue
where
red = only Red
green = only Green
blue = only Blue
only c = maximum . map fst . filter (\p -> snd p == c) $ round
part1 :: Bool -> ByteString -> String
part1 _ = show . countPossible . extract . runParser parseInput
part2 :: Bool -> ByteString -> String
part2 _ = show . sum . map power . extract . runParser parseInput