-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-2.hs
81 lines (65 loc) · 1.49 KB
/
day-2.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
module Main where
import Control.Monad
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import AoC
import Data.List (find)
import Data.Maybe (isNothing)
type S = String
type Parser = Parsec Void S
type Game = (Int, [Draws])
type Draws = [Draw]
type Draw = (Color, Int)
type Color = S
solve :: String -> Int -> Int
solve inp = \case
1 -> part1
2 -> part2
where
gs :: [Game]
Just gs = parseMaybe (many parseGame) inp
part1 = let
vgs :: [Game]
vgs = filter viable gs
in map fst vgs |> sum
part2 = map score gs |> sum
-- Part 2
score :: Game -> Int
score (_, dds) = product mxs
where
ds :: Draws
ds = concat dds
mxs :: [Int]
mxs = map (fst .> (flt .> map snd .> maximum)) bounds
flt :: Color -> Draws
flt c = filter (fst .> (== c)) ds
-- Part 1
viable :: Game -> Bool
viable (n, dss) = concat dss |> find violator .> isNothing
violator :: Draw -> Bool
violator (c, n) = let (Just mx) = lookup c bounds in n > mx
bounds :: [(Color, Int)]
bounds =
[ ("red", 12)
, ("green", 13)
, ("blue", 14)
]
-- Parsers
parseGame :: Parser Game
parseGame = do
string "Game "
n <- L.decimal
string ": "
ds <- tok $ sepBy parseDraws (string "; ")
pure (n, ds)
parseDraws = sepBy parseDraw (string ", ")
parseDraw :: Parser Draw
parseDraw = do
n <- tok L.decimal
color <- tok $ some letterChar
pure (color, n)
tok = L.lexeme space
main :: IO ()
main = defaultMain solve