From d7e41f1e93eac045329b6ea91ae150d517e443a3 Mon Sep 17 00:00:00 2001 From: tzlil Date: Sat, 2 Dec 2023 14:09:07 +0200 Subject: a little nicer --- 2/1.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to '2/1.hs') diff --git a/2/1.hs b/2/1.hs index 9151408..4f3a5ae 100644 --- a/2/1.hs +++ b/2/1.hs @@ -7,7 +7,7 @@ import Data.Maybe import qualified Data.Map as M import Control.Monad -data Color = Red | Green | Blue deriving (Eq,Ord) +data Color = Red | Green | Blue deriving (Show,Eq,Ord) type Handful = (Color, Int) type Set = M.Map Color Int type Game = (Int, [Set]) @@ -25,6 +25,8 @@ handful = space *> (flip (,) <$> read <$> many1 digit) <* space <*> color set = M.fromList <$> (sepBy handful $ string ",") game = string "Game " *> ((,) <$> read <$> many1 digit) <* string ":" <*> sepBy set (string ";") +games = sepBy game newline + solution :: Game -> Maybe Int solution (i,m) = do let m' = M.unionsWith max m @@ -33,4 +35,4 @@ solution (i,m) = do guard $ M.findWithDefault 0 Blue m' <= 14 return i -main = head <$> getArgs >>= readFile >>= print . sum . catMaybes . map solution . catMaybes . map (either (const Nothing) Just . parse game "") <$> lines \ No newline at end of file +main = head <$> getArgs >>= readFile >>= print . sum . catMaybes . map solution . either (error.show) id . parse games "" \ No newline at end of file -- cgit 1.4.1