Day 12: Forgot to use the formatter
This commit is contained in:
parent
5e171b20c0
commit
12d28015d4
|
@ -1,51 +1,60 @@
|
||||||
import Data.List.Split
|
|
||||||
import Data.List
|
import Data.List
|
||||||
|
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"
|
||||||
let velocity = replicate 4 [0,0,0]
|
let velocity = replicate 4 [0, 0, 0]
|
||||||
print $ part1 (toV3 moons, toV3 velocity)
|
print $ part1 (toV3 moons, toV3 velocity)
|
||||||
print $ part2 (moons,velocity)
|
print $ part2 (moons, velocity)
|
||||||
|
|
||||||
part1 :: ([V3 Int],[V3 Int]) -> Int
|
part1 :: ([V3 Int], [V3 Int]) -> Int
|
||||||
part1 x = energy $ iterate step x !! 1000
|
part1 x = energy $ iterate step x !! 1000
|
||||||
|
|
||||||
--I’m sure there is a better way to do this
|
--I’m sure there is a better way to do this
|
||||||
toV3 :: [[Int]] -> [V3 Int]
|
toV3 :: [[Int]] -> [V3 Int]
|
||||||
toV3 = map (\[x,y,z]->V3 x y z)
|
toV3 = map (\[x, y, z] -> V3 x y z)
|
||||||
|
|
||||||
parseContent :: String -> [[Int]]
|
parseContent :: String -> [[Int]]
|
||||||
parseContent = map (map read . splitOn ",") . lines . removeJunk
|
parseContent = map (map read . splitOn ",") . lines . removeJunk
|
||||||
where removeJunk xs = [ x | x <- xs, x `notElem` " <>xyz=" ]
|
where
|
||||||
|
removeJunk xs = [x | x <- xs, x `notElem` " <>xyz="]
|
||||||
|
|
||||||
step :: (Num a, Eq a) => ([a],[a]) -> ([a],[a])
|
step :: (Num a, Eq a) => ([a], [a]) -> ([a], [a])
|
||||||
step (moons,vel) = (zipWith (+) moons newVel,newVel)
|
step (moons, vel) = (zipWith (+) moons newVel, newVel)
|
||||||
where dVel = gravity moons
|
where
|
||||||
newVel = zipWith (+) vel dVel
|
dVel = gravity moons
|
||||||
gravity xs = [sum [signum $ x-y | x<-xs, y/=x] | y<-xs]
|
newVel = zipWith (+) vel dVel
|
||||||
|
gravity xs = [sum [signum $ x - y | x <- xs, y /= x] | y <- xs]
|
||||||
|
|
||||||
energy :: ([V3 Int],[V3 Int]) -> Int
|
energy :: ([V3 Int], [V3 Int]) -> Int
|
||||||
energy (x,y) = sum $ zipWith (*) (geten x) (geten y) where
|
energy (x, y) = sum $ zipWith (*) (geten x) (geten y)
|
||||||
|
where
|
||||||
geten = map (sum . abs)
|
geten = map (sum . abs)
|
||||||
|
|
||||||
findPeriod :: (Num a, Eq a) => ([a], [a]) -> ([a], [a]) -> Int -> Int
|
findPeriod :: (Num a, Eq a) => ([a], [a]) -> ([a], [a]) -> Int -> Int
|
||||||
findPeriod x a n = if x'==a then n else findPeriod x' a (n+1)
|
findPeriod x a n =
|
||||||
where x' = step x
|
if x' == a
|
||||||
|
then n
|
||||||
|
else findPeriod x' a (n + 1)
|
||||||
|
where
|
||||||
|
x' = step x
|
||||||
|
|
||||||
part2 :: (Num a, Eq a) => ([[a]], [[a]]) -> Int
|
part2 :: (Num a, Eq a) => ([[a]], [[a]]) -> Int
|
||||||
part2 (moons,vel) = lcm' periods
|
part2 (moons, vel) = lcm' periods
|
||||||
where m = transpose moons
|
where
|
||||||
v = transpose vel
|
m = transpose moons
|
||||||
periods = zipWith (curry findPeriod') m v
|
v = transpose vel
|
||||||
findPeriod' x = findPeriod x x 1
|
periods = zipWith (curry findPeriod') m v
|
||||||
|
findPeriod' x = findPeriod x x 1
|
||||||
|
|
||||||
lcm' :: [Int] -> Int
|
lcm' :: [Int] -> Int
|
||||||
lcm' xs = product $ zipWith (^) nums maxElems
|
lcm' xs = product $ zipWith (^) nums maxElems
|
||||||
where nums = nub $ concat decomp
|
where
|
||||||
decomp = map decomposition xs
|
nums = nub $ concat decomp
|
||||||
maxElems = [maximum $ map (length . elemIndices x) decomp | x<-nums]
|
decomp = map decomposition xs
|
||||||
|
maxElems = [maximum $ map (length . elemIndices x) decomp | x <- nums]
|
||||||
|
|
||||||
--hyper optimized prime decomposition
|
--hyper optimized prime decomposition
|
||||||
decomposition :: Int -> [Int]
|
decomposition :: Int -> [Int]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user