2025-12-02 15:22:21 +00:00
|
|
|
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
|
2025-12-04 21:24:49 +00:00
|
|
|
import Data.Text.Lazy qualified as TL
|
2025-12-02 15:19:11 +00:00
|
|
|
|
2025-12-02 15:22:21 +00:00
|
|
|
puzzle :: Puzzle
|
|
|
|
|
puzzle =
|
2025-12-02 15:19:11 +00:00
|
|
|
Puzzle
|
|
|
|
|
{ number = 2
|
2025-12-08 12:48:49 +00:00
|
|
|
, parser = (<* newline) $ flip sepBy (char ',') $ (,) <$> (decimal <* char '-') <*> decimal
|
2025-12-02 15:19:11 +00:00
|
|
|
, parts =
|
2025-12-04 21:24:49 +00:00
|
|
|
[ TL.show
|
2025-12-02 15:19:11 +00:00
|
|
|
. sum
|
|
|
|
|
. concatMap
|
|
|
|
|
(mapMaybe (\n -> guard (isRepetition2 n) $> n) . uncurry enumFromTo)
|
2025-12-04 21:24:49 +00:00
|
|
|
, TL.show
|
2025-12-02 15:19:11 +00:00
|
|
|
. sum
|
|
|
|
|
. 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
|