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

40 lines
1.1 KiB
Haskell

module Puzzles.Day2 (puzzle) where
import Pre
import Data.Text qualified as T
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 2
, parser = const $ (<* newline) $ ((,) <$> (decimal <* char '-') <*> decimal) `sepBy` (char ',')
, parts =
( sum
. concatMap
(mapMaybe (\n -> guard (isRepetition2 n) $> n) . uncurry enumFromTo)
)
/\ ( sum
. concatMap
(mapMaybe (\n -> guard (isRepetitionN n) $> n) . uncurry enumFromTo)
)
/\ nil
, extraTests = mempty
}
newtype ID = ID Int
deriving newtype (Eq, Ord, Show, Num, Enum)
isRepetition2 :: ID -> Bool
isRepetition2 (T.show -> n) = case T.length n `divMod` 2 of
(d, 0) -> equalChunks n d
_ -> False
isRepetitionN :: ID -> Bool
isRepetitionN (T.show -> n) = flip any [1 .. T.length n `div` 2] $ equalChunks n
equalChunks :: Text -> Int -> Bool
equalChunks n i = case T.chunksOf i n of
[] -> True
x : xs -> all (== x) xs