diff --git a/2019/day12/day12.hs b/2019/day12/day12.hs new file mode 100644 index 0000000..76b56d9 --- /dev/null +++ b/2019/day12/day12.hs @@ -0,0 +1,28 @@ +{-# 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) diff --git a/2019/day12/input b/2019/day12/input new file mode 100644 index 0000000..49300d0 --- /dev/null +++ b/2019/day12/input @@ -0,0 +1,4 @@ + + + +