From 5539a4bd05bf8b6b95ab8680d515bd4c31d72eb2 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Mon, 8 Dec 2025 15:52:20 +0000 Subject: [PATCH] Solve day 6 part 1 --- haskell/Pre.hs | 2 +- haskell/Puzzles/Day6.hs | 24 +++++++++++++++++++++--- outputs/real/6/1 | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 outputs/real/6/1 diff --git a/haskell/Pre.hs b/haskell/Pre.hs index b2c44d4..4993d13 100644 --- a/haskell/Pre.hs +++ b/haskell/Pre.hs @@ -56,7 +56,7 @@ import Data.Char import Data.Foldable hiding (foldl1, foldr1, maximum, maximumBy, minimum, minimumBy) import Data.Foldable1 import Data.Functor -import Data.List (sortOn) +import Data.List (sortOn, transpose) import Data.List.Extra (dropEnd, enumerate) import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty, some1) import Data.Maybe diff --git a/haskell/Puzzles/Day6.hs b/haskell/Puzzles/Day6.hs index 599d617..31ab853 100644 --- a/haskell/Puzzles/Day6.hs +++ b/haskell/Puzzles/Day6.hs @@ -2,14 +2,32 @@ module Puzzles.Day6 (puzzle) where import Pre +import Data.Text.Lazy qualified as TL + puzzle :: Puzzle puzzle = Puzzle { number = 6 - , parser = pure () + , parser = do + ints <- (hspace *> (decimal `sepBy1` hspace1)) `sepEndBy1` newline + ops <- ((single '*' $> Multiply) <|> (single '+' $> Add)) `sepEndBy` hspace1 + void newline + pure (ops, transpose ints) , parts = - [ \() -> - "TODO" + [ TL.show + . sum + . uncurry (zipWith \op -> foldl' (apply op) (unit op)) ] , extraTests = mempty } + +data Op = Add | Multiply + deriving (Eq, Ord, Show, Enum, Bounded) +apply :: Op -> Int -> Int -> Int +apply = \case + Add -> (+) + Multiply -> (*) +unit :: Op -> Int +unit = \case + Add -> 0 + Multiply -> 1 diff --git a/outputs/real/6/1 b/outputs/real/6/1 new file mode 100644 index 0000000..d210b1e --- /dev/null +++ b/outputs/real/6/1 @@ -0,0 +1 @@ +8108520669952 \ No newline at end of file