From c0504964e3b1f6c781f455e57dc2d8384f7c7b3f Mon Sep 17 00:00:00 2001 From: George Thomas Date: Tue, 2 Dec 2025 12:53:31 +0000 Subject: [PATCH] Refactor to unify day 2 parts 1 and 2 This doesn't noticeably affect the run time. --- app/Main.hs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a359dd4..9a83835 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -116,9 +116,14 @@ newtype ID = ID Int deriving newtype (Eq, Ord, Show, Num, Enum) isRepetition2 :: ID -> Bool -isRepetition2 (T.show -> n) = uncurry (==) $ T.splitAt (T.length n `div` 2) n +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] \i -> case T.chunksOf i n of - [] -> False +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