2025-12-08 13:09:28 +00:00
|
|
|
module Puzzles.Day6 (puzzle) where
|
|
|
|
|
|
|
|
|
|
import Pre
|
|
|
|
|
|
|
|
|
|
puzzle :: Puzzle
|
|
|
|
|
puzzle =
|
|
|
|
|
Puzzle
|
|
|
|
|
{ number = 6
|
2025-12-08 22:42:29 +00:00
|
|
|
, parser = const do
|
2025-12-08 23:38:48 +00:00
|
|
|
ints <- some ((Just <$> digit) <|> (single ' ' $> Nothing)) `sepEndBy1` newline
|
2025-12-08 15:52:20 +00:00
|
|
|
ops <- ((single '*' $> Multiply) <|> (single '+' $> Add)) `sepEndBy` hspace1
|
|
|
|
|
void newline
|
2025-12-08 23:38:48 +00:00
|
|
|
pure (ops, ints)
|
2025-12-08 13:09:28 +00:00
|
|
|
, parts =
|
2025-12-16 16:15:11 +00:00
|
|
|
( sum
|
2025-12-08 23:38:48 +00:00
|
|
|
. uncurry (zipWith applyToList)
|
|
|
|
|
. second (transpose . map (map (digitsToInt @Int . catMaybes) . filter notNull . splitOn [Nothing]))
|
2025-12-16 16:15:11 +00:00
|
|
|
)
|
|
|
|
|
/\ ( sum
|
|
|
|
|
. uncurry (zipWith applyToList)
|
|
|
|
|
. second
|
|
|
|
|
( map catMaybes
|
|
|
|
|
. splitOn [Nothing]
|
|
|
|
|
. map (\l -> if all isNothing l then Nothing else Just $ digitsToInt @Int $ catMaybes l)
|
|
|
|
|
. transpose
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
/\ nil
|
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
|
2025-12-08 23:38:48 +00:00
|
|
|
applyToList :: Op -> [Int] -> Int
|
|
|
|
|
applyToList op = foldl' (apply op) (unit op)
|