Solve day 2 part 2
This is pretty naive but runs in less than a second when compiled.
This commit is contained in:
parent
6f3855ce4b
commit
fe3493aa86
15
app/Main.hs
15
app/Main.hs
@ -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
1
outputs/examples/2/2
Normal file
@ -0,0 +1 @@
|
|||||||
|
4174379265
|
||||||
1
outputs/real/2/2
Normal file
1
outputs/real/2/2
Normal file
@ -0,0 +1 @@
|
|||||||
|
41823587546
|
||||||
Loading…
x
Reference in New Issue
Block a user