29 lines
875 B
Haskell
29 lines
875 B
Haskell
|
{-# LANGUAGE LambdaCase #-}
|
||
|
|
||
|
import Data.List.Split
|
||
|
import Linear.V3
|
||
|
|
||
|
main = do
|
||
|
moons <- parseContent <$> readFile "testinput"
|
||
|
let velocity = replicate 4 $ V3 0 0 0
|
||
|
print $ energy $ step (moons,velocity) 1000
|
||
|
|
||
|
removeJunk :: String -> String
|
||
|
removeJunk xs = [ x | x <- xs, x `notElem` " <>xyz=" ]
|
||
|
|
||
|
parseContent :: String -> [V3 Int]
|
||
|
parseContent = map ((\[x,y,z]->V3 x y z) . map read . splitOn ",") . lines . removeJunk
|
||
|
|
||
|
gravity :: [V3 Int] -> [V3 Int]
|
||
|
gravity xs = [sum [signum $ x-y | x<-xs, y/=x] | y<-xs]
|
||
|
|
||
|
step :: ([V3 Int],[V3 Int]) -> Int -> ([V3 Int],[V3 Int])
|
||
|
step (moons,vel) 0 = (moons,vel)
|
||
|
step (moons,vel) n = step (zipWith (+) moons newVel,newVel) (n-1)
|
||
|
where dVel = gravity moons
|
||
|
newVel = zipWith (+) vel dVel
|
||
|
|
||
|
energy :: ([V3 Int],[V3 Int]) -> Int
|
||
|
energy (x,y) = sum $ zipWith (*) (geten x) (geten y) where
|
||
|
geten = map (sum . abs)
|