X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent07%2FMain.hs;h=8f3a42445d74b6ffa4596856b47b815a645d7fb6;hb=14a708ee545a9bd6d81205177ab9baabd29d1894;hp=6d326a4f545c08603210a8d9bc73de441fc2a7db;hpb=c7364d4e2de33c97a720e1654e296d3f4c304756;p=advent-of-code-23.git diff --git a/advent07/Main.hs b/advent07/Main.hs index 6d326a4..8f3a424 100644 --- a/advent07/Main.hs +++ b/advent07/Main.hs @@ -33,17 +33,14 @@ part1 hands = sum $ fmap score rankedHands rankedHands = zip [1..] sortedHands score (r, CHand _ _ bid) = r * bid -part2 hands = part1 $ fmap enJoker hands +part2 = part1 . fmap enJoker enJoker :: Hand -> Hand enJoker (Hand cards bid) = Hand jCards bid where jCards = replace Jack Joker cards replace :: Eq a => a -> a -> [a] -> [a] -replace _ _ [] = [] -replace f t (x:xs) - | x == f = t : replace f t xs - | otherwise = x : replace f t xs +replace f t = fmap (\x -> if x == f then t else x) classify :: Hand -> ClassifiedHand classify (Hand cards bid) @@ -98,7 +95,7 @@ handP :: Parser Hand cardP :: Parser Card handsP = handP `sepBy` endOfLine -handP = Hand <$> ((many1 cardP) <* space) <*> decimal +handP = Hand <$> many1 cardP <* space <*> decimal cardP = (Two <$ "2") <|> (Three <$ "3") <|> (Four <$ "4") <|> (Five <$ "5") <|> (Six <$ "6") <|> (Seven <$ "7") <|>