34 lines
806 B
Haskell
Raw Normal View History

2025-12-08 13:09:28 +00:00
module Puzzles.Day6 (puzzle) where
import Pre
2025-12-08 15:52:20 +00:00
import Data.Text.Lazy qualified as TL
2025-12-08 13:09:28 +00:00
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 6
, parser = const do
2025-12-08 15:52:20 +00:00
ints <- (hspace *> (decimal `sepBy1` hspace1)) `sepEndBy1` newline
ops <- ((single '*' $> Multiply) <|> (single '+' $> Add)) `sepEndBy` hspace1
void newline
pure (ops, transpose ints)
2025-12-08 13:09:28 +00:00
, parts =
2025-12-08 15:52:20 +00:00
[ TL.show
. sum
. uncurry (zipWith \op -> foldl' (apply op) (unit op))
2025-12-08 13:09:28 +00:00
]
, extraTests = mempty
}
2025-12-08 15:52:20 +00:00
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