diff options
author | tzlil <tzlils@protonmail.com> | 2023-12-02 19:25:43 +0200 |
---|---|---|
committer | tzlil <tzlils@protonmail.com> | 2023-12-02 19:25:43 +0200 |
commit | 261bfbaa66c0293585c0d8f7ccb6fe4423bcad4e (patch) | |
tree | 5691f5b9efec114fa62ddc7ec8e61569881cc821 /2/2.hs | |
parent | 1b9ea3c36d2f32bedc0416898764f6039320274e (diff) |
lol
Diffstat (limited to '2/2.hs')
-rw-r--r-- | 2/2.hs | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/2/2.hs b/2/2.hs index 4437594..3d26a35 100644 --- a/2/2.hs +++ b/2/2.hs @@ -12,22 +12,24 @@ type Handful = (Color, Int) type Set = M.Map Color Int type Game = (Int, [Set]) -red = try $ string "red" *> pure Red -green = try $ string "green" *> pure Green -blue = try $ string "blue" *> pure Blue +red = string "red" *> pure Red +green = string "green" *> pure Green +blue = string "blue" *> pure Blue color :: Parser Color color = red <|> green <|> blue handful :: Parser Handful -handful = space *> (flip (,) <$> read <$> many1 digit) <* space <*> color +handful = liftM2 (flip (,)) (space *> (read <$> many1 digit)) (space *> color) set = M.fromList <$> (sepBy handful $ string ",") -game = string "Game " *> ((,) <$> read <$> many1 digit) <* string ":" <*> sepBy set (string ";") +game = liftM2 (,) (string "Game " *> (read <$> many1 digit)) (string ":" *> sepBy set (string ";")) games = sepBy game newline solution :: Game -> Int solution = product . M.elems . M.unionsWith max . snd -main = head <$> getArgs >>= ((=<<) . ((print . sum . map solution . either (error . show) id) .) . parse games) <*> readFile \ No newline at end of file +main = head <$> getArgs >>= \a -> do + r <- readFile a + print $ sum $ map solution $ either (error.show) id $ parse games a r \ No newline at end of file |