diff options
author | tzlil <tzlils@protonmail.com> | 2023-12-15 19:34:05 +0200 |
---|---|---|
committer | tzlil <tzlils@protonmail.com> | 2023-12-15 19:34:05 +0200 |
commit | 3dda0ec7cb4477259a1be52565fe27e0312b9d3b (patch) | |
tree | 99e9d7f9525369d68edf0d02224b5fb6bbd33a46 /4/1.hs | |
parent | 1fd224e40011ee0a63aa7955fe79b616a4dfa6dd (diff) |
wip 5
Diffstat (limited to '4/1.hs')
-rw-r--r-- | 4/1.hs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/4/1.hs b/4/1.hs new file mode 100644 index 0000000..275a04c --- /dev/null +++ b/4/1.hs @@ -0,0 +1,23 @@ +import Prelude +import Text.Parsec.String (Parser) +import Text.Parsec +import System.Environment +import Control.Monad +import qualified Data.Set as S + +type Card = (Int, S.Set Int, S.Set Int) + +numbers = S.fromList <$> ((many1 space) *> sepEndBy (read <$> many1 digit) (many1 space)) + +card :: Parser Card +card = (,,) <$> (string "Card" *> spaces *> (read <$> many1 digit) <* char ':') <*> numbers <* (char '|') <*> numbers + +cards = many1 card + +solution :: [Card] -> Int +solution xs = sum $ map f xs where + f (id,picked,winning) = case (length $ S.intersection picked winning) of + 0 -> 0 + n -> 2 ^ (pred n) + +main = liftM2 (>>=) readFile (((print . solution . either (error.show) id) .) . parse cards) =<< head <$> getArgs \ No newline at end of file |