George Thomas 415055dcc2 Allow output types to vary for different parts of same day
For now this applies to Haskell only, and it may turn out to be tricky for the Rust implementation.

In practice, the limitation hasn't turned out to be important, and we could even go the other way and use `Integer` everywhere. This does however at least help with debugging, as well as just being conceptually right.

The `nil` and `(/\)` functions are intended to be overloaded to work for other list-like things in a later commit, and from there we will investigate using `OverloadedLists` and `RebindableSyntax` to recover standard list syntax, although there are probably limitations due to `(:)` being special.
2025-12-16 16:15:11 +00:00

44 lines
1.3 KiB
Haskell

module Puzzles.Day6 (puzzle) where
import Pre
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 6
, parser = const do
ints <- some ((Just <$> digit) <|> (single ' ' $> Nothing)) `sepEndBy1` newline
ops <- ((single '*' $> Multiply) <|> (single '+' $> Add)) `sepEndBy` hspace1
void newline
pure (ops, ints)
, parts =
( sum
. uncurry (zipWith applyToList)
. second (transpose . map (map (digitsToInt @Int . catMaybes) . filter notNull . splitOn [Nothing]))
)
/\ ( 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
, 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
applyToList :: Op -> [Int] -> Int
applyToList op = foldl' (apply op) (unit op)