From 07912fbb826eca723c1a6937e23ed96fe829d69f Mon Sep 17 00:00:00 2001 From: Javran Cheng Date: Sat, 6 Jan 2024 00:35:00 -0800 Subject: [PATCH] a bit less unpacking. sorta overkill. I like the idea that you just express the intention through Monoid and let foldMap have at it. --- src/Javran/AdventOfCode/Y2023/Day1.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Javran/AdventOfCode/Y2023/Day1.hs b/src/Javran/AdventOfCode/Y2023/Day1.hs index ada011e9..fc6f2de5 100644 --- a/src/Javran/AdventOfCode/Y2023/Day1.hs +++ b/src/Javran/AdventOfCode/Y2023/Day1.hs @@ -1,6 +1,7 @@ module Javran.AdventOfCode.Y2023.Day1 () where import Data.Char +import Data.Coerce (coerce) import Data.List import Data.Semigroup import Javran.AdventOfCode.Prelude @@ -46,12 +47,12 @@ getDigits2 = mapMaybe parse . tails parse = consumeAllWithReadP (oneDigitP <* munch (\_ -> True)) solve :: (String -> [Int]) -> [String] -> Int -solve df = sum . fmap (combine . foldMap collector . df) +solve df = getSum . foldMap (coerce combine . foldMap collector . df) where -- just to collect both first and last value with a single traverse. collector = (,) <$> Just . First <*> Just . Last combine = \case - (Just (First a), Just (Last b)) -> a * 10 + b + (Just a, Just b) -> a * 10 + b :: Int _ -> {- Note that this branch is usually unreachable as login input will always