Skip to content

Commit

Permalink
a bit less unpacking.
Browse files Browse the repository at this point in the history
sorta overkill. I like the idea that you just express the
intention through Monoid and let foldMap have at it.
  • Loading branch information
Javran committed Jan 6, 2024
1 parent 9fdc464 commit 07912fb
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Javran/AdventOfCode/Y2023/Day1.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 07912fb

Please sign in to comment.