Solve day 2 part 2

This is pretty naive but runs in less than a second when compiled.
This commit is contained in:
George Thomas 2025-12-02 12:33:41 +00:00
parent 6f3855ce4b
commit fe3493aa86
3 changed files with 14 additions and 3 deletions

View File

@ -104,12 +104,21 @@ puzzle2 =
[ T.show [ T.show
. sum . sum
. concatMap . concatMap
(mapMaybe (\n -> guard (isRepetition n) $> n) . uncurry enumFromTo) (mapMaybe (\n -> guard (isRepetition2 n) $> n) . uncurry enumFromTo)
, T.show
. sum
. concatMap
(mapMaybe (\n -> guard (isRepetitionN n) $> n) . uncurry enumFromTo)
] ]
} }
newtype ID = ID Int newtype ID = ID Int
deriving newtype (Eq, Ord, Show, Num, Enum) deriving newtype (Eq, Ord, Show, Num, Enum)
isRepetition :: ID -> Bool isRepetition2 :: ID -> Bool
isRepetition (T.show -> n) = uncurry (==) $ T.splitAt (T.length n `div` 2) n isRepetition2 (T.show -> n) = uncurry (==) $ T.splitAt (T.length n `div` 2) n
isRepetitionN :: ID -> Bool
isRepetitionN (T.show -> n) = flip any [1 .. T.length n `div` 2] \i -> case T.chunksOf i n of
[] -> False
x : xs -> all (== x) xs

1
outputs/examples/2/2 Normal file
View File

@ -0,0 +1 @@
4174379265

1
outputs/real/2/2 Normal file
View File

@ -0,0 +1 @@
41823587546