This breaks less code than adding it to the solution functions, and is more elegant in a way.
34 lines
806 B
Haskell
34 lines
806 B
Haskell
module Puzzles.Day6 (puzzle) where
|
|
|
|
import Pre
|
|
|
|
import Data.Text.Lazy qualified as TL
|
|
|
|
puzzle :: Puzzle
|
|
puzzle =
|
|
Puzzle
|
|
{ number = 6
|
|
, parser = const do
|
|
ints <- (hspace *> (decimal `sepBy1` hspace1)) `sepEndBy1` newline
|
|
ops <- ((single '*' $> Multiply) <|> (single '+' $> Add)) `sepEndBy` hspace1
|
|
void newline
|
|
pure (ops, transpose ints)
|
|
, parts =
|
|
[ 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
|