Day 12: lcm is in Prelude :veryironicwow:
This commit is contained in:
parent
457a7e7d73
commit
b177b0daa5
|
@ -1,6 +1,7 @@
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import Linear.V3
|
import Linear.V3
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
moons <- parseContent <$> readFile "input"
|
moons <- parseContent <$> readFile "input"
|
||||||
|
@ -30,9 +31,8 @@ step (moons, vel) = (zipWith (+) moons newVel, newVel)
|
||||||
newVel = zipWith (+) vel dVel
|
newVel = zipWith (+) vel dVel
|
||||||
gravity xs = [sum [signum $ x - y | x <- xs, y /= x] | y <- xs]
|
gravity xs = [sum [signum $ x - y | x <- xs, y /= x] | y <- xs]
|
||||||
|
|
||||||
|
|
||||||
part2 :: (Num a, Eq a) => ([[a]], [[a]]) -> Int
|
part2 :: (Num a, Eq a) => ([[a]], [[a]]) -> Int
|
||||||
part2 (moons, vel) = lcm' periods
|
part2 (moons, vel) = foldr1 lcm periods
|
||||||
where
|
where
|
||||||
m = transpose moons
|
m = transpose moons
|
||||||
v = transpose vel
|
v = transpose vel
|
||||||
|
@ -46,30 +46,3 @@ findPeriod x a n =
|
||||||
else findPeriod x' a (n + 1)
|
else findPeriod x' a (n + 1)
|
||||||
where
|
where
|
||||||
x' = step x
|
x' = step x
|
||||||
|
|
||||||
lcm' :: [Int] -> Int
|
|
||||||
lcm' xs = product $ zipWith (^) nums maxElems
|
|
||||||
where
|
|
||||||
nums = nub $ concat decomp
|
|
||||||
decomp = map decomposition xs
|
|
||||||
maxElems = [maximum $ map (length . elemIndices x) decomp | x <- nums]
|
|
||||||
|
|
||||||
--hyper optimized prime decomposition
|
|
||||||
decomposition :: Int -> [Int]
|
|
||||||
decomposition 1 = []
|
|
||||||
decomposition x =
|
|
||||||
let n =
|
|
||||||
if x `mod` 2 == 0
|
|
||||||
then 2
|
|
||||||
else 3
|
|
||||||
maxiter = (floor . (sqrt :: Double -> Double) . fromIntegral) x
|
|
||||||
findprim candidate current
|
|
||||||
| (candidate <= maxiter) && (x `mod` candidate /= 0) =
|
|
||||||
findprim (next current) (current + 1)
|
|
||||||
| otherwise = candidate
|
|
||||||
m = findprim n 1
|
|
||||||
in if m <= maxiter
|
|
||||||
then m : decomposition (x `div` m)
|
|
||||||
else [x]
|
|
||||||
where
|
|
||||||
next k = k * 4 - k `div` 2 * 2 + 1
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user