38 lines
1019 B
Haskell
Raw Normal View History

module Puzzles.Day2 (puzzle) where
2025-12-02 15:19:11 +00:00
2025-12-08 12:48:49 +00:00
import Pre
2025-12-02 15:19:11 +00:00
import Data.Text qualified as T
puzzle :: Puzzle
puzzle =
2025-12-02 15:19:11 +00:00
Puzzle
{ number = 2
, parser = const $ (<* newline) $ ((,) <$> (decimal <* char '-') <*> decimal) `sepBy` (char ',')
2025-12-02 15:19:11 +00:00
, parts =
[ sum
2025-12-02 15:19:11 +00:00
. concatMap
(mapMaybe (\n -> guard (isRepetition2 n) $> n) . uncurry enumFromTo)
, sum
2025-12-02 15:19:11 +00:00
. concatMap
(mapMaybe (\n -> guard (isRepetitionN n) $> n) . uncurry enumFromTo)
]
2025-12-04 21:16:46 +00:00
, extraTests = mempty
2025-12-02 15:19:11 +00:00
}
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