Day12: Part 2 works
This commit is contained in:
parent
99b6f0085b
commit
3fe5944338
66
day12.hs
66
day12.hs
|
@ -5,10 +5,18 @@ import Linear.V3
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
moons <- map createMoon <$> map getList <$> map cleanString <$> lines <$> getContents
|
moons <- map createMoon <$> map getList <$> map cleanString <$> lines <$> getContents
|
||||||
let newMoons = stepMoons moons 1000
|
--let newMoons = calculateMoons moons 1000
|
||||||
let ernergy = sum $ map getErnergy newMoons
|
--let ernergy = sum $ map getErnergy newMoons
|
||||||
mapM putStrLn ( map show newMoons )
|
let xAxis = getXAxis moons
|
||||||
putStrLn ( show ernergy)
|
let loopx = findLoop xAxis xAxis 0
|
||||||
|
let yAxis = getYAxis moons
|
||||||
|
let loopy = findLoop yAxis yAxis 0
|
||||||
|
let zAxis = getZAxis moons
|
||||||
|
let loopz = findLoop zAxis zAxis 0
|
||||||
|
let result = foldl1 lcm [loopx, loopy, loopz]
|
||||||
|
--mapM putStrLn ( map show newMoons )
|
||||||
|
--putStrLn ( show ernergy)
|
||||||
|
putStrLn(show result)
|
||||||
|
|
||||||
data Moon = Moon{ position :: V3 Int
|
data Moon = Moon{ position :: V3 Int
|
||||||
,velocity :: V3 Int
|
,velocity :: V3 Int
|
||||||
|
@ -28,18 +36,54 @@ stepGravity xs = map (\(p,v) -> Moon p v) $ zip (newPos) newVel
|
||||||
where newVel = zipWith (+) (getVel xs) (gravity $ getPos xs)
|
where newVel = zipWith (+) (getVel xs) (gravity $ getPos xs)
|
||||||
newPos = zipWith (+) (getPos xs) (newVel)
|
newPos = zipWith (+) (getPos xs) (newVel)
|
||||||
|
|
||||||
|
gravity :: (Num a, Eq a)=> [a] -> [a]
|
||||||
|
gravity xs = [sum [signum $ x-y | x<-xs, y/=x] | y<-xs]
|
||||||
|
|
||||||
|
calculateMoons :: [Moon] -> Int -> [Moon]
|
||||||
|
calculateMoons xs 0 = xs
|
||||||
|
calculateMoons xs n = calculateMoons (stepGravity xs) (n-1)
|
||||||
|
|
||||||
|
findLoop :: ([Int],[Int]) -> ([Int],[Int]) -> Int -> Int
|
||||||
|
findLoop pos start c
|
||||||
|
| pos /= start || c == 0 = findLoop (stepAxis pos) start (c+1)
|
||||||
|
| pos == start = c
|
||||||
|
|
||||||
|
stepAxis :: ([Int],[Int]) -> ([Int],[Int])
|
||||||
|
stepAxis (pos,vel) = (newPos, newVel)
|
||||||
|
where newVel = zipWith (+) (vel) (gravity pos)
|
||||||
|
newPos = zipWith (+) (pos) (newVel)
|
||||||
|
|
||||||
getPos :: [Moon] -> [V3 Int]
|
getPos :: [Moon] -> [V3 Int]
|
||||||
getPos xs = map(\x -> position x) xs
|
getPos xs = map(\x -> position x) xs
|
||||||
|
|
||||||
getVel :: [Moon] -> [V3 Int]
|
getVel :: [Moon] -> [V3 Int]
|
||||||
getVel xs = map(\x -> velocity x) xs
|
getVel xs = map(\x -> velocity x) xs
|
||||||
|
|
||||||
gravity :: [V3 Int] -> [V3 Int]
|
|
||||||
gravity xs = [sum [signum $ x-y | x<-xs, y/=x] | y<-xs]
|
|
||||||
|
|
||||||
stepMoons :: [Moon] -> Int -> [Moon]
|
|
||||||
stepMoons xs 0 = xs
|
|
||||||
stepMoons xs n = stepMoons (stepGravity xs) (n-1)
|
|
||||||
|
|
||||||
getErnergy :: Moon -> Int
|
getErnergy :: Moon -> Int
|
||||||
getErnergy (Moon pos vel) = (sum $ abs pos) * (sum $ abs vel)
|
getErnergy (Moon pos vel) = (sum $ abs pos) * (sum $ abs vel)
|
||||||
|
|
||||||
|
getXAxis :: [Moon] -> ([Int],[Int])
|
||||||
|
getXAxis xs = (pos,vel)
|
||||||
|
where pos = map(\x -> getX $ position x) xs
|
||||||
|
vel = map(\x -> getX $ velocity x) xs
|
||||||
|
|
||||||
|
getZAxis :: [Moon] -> ([Int],[Int])
|
||||||
|
getZAxis xs = (pos,vel)
|
||||||
|
where pos = map(\x -> getZ $ position x) xs
|
||||||
|
vel = map(\x -> getZ $ velocity x) xs
|
||||||
|
|
||||||
|
getYAxis :: [Moon] -> ([Int],[Int])
|
||||||
|
getYAxis xs = (pos,vel)
|
||||||
|
where pos = map(\x -> getY $ position x) xs
|
||||||
|
vel = map(\x -> getY $ velocity x) xs
|
||||||
|
|
||||||
|
getX :: V3 Int -> Int
|
||||||
|
getX (V3 x y z) = x
|
||||||
|
|
||||||
|
getY :: V3 Int -> Int
|
||||||
|
getY (V3 x y z) = y
|
||||||
|
|
||||||
|
getZ :: V3 Int -> Int
|
||||||
|
getZ (V3 x y z) = z
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user