Day 12 Part 1

This commit is contained in:
shu 2019-12-12 07:49:36 +01:00
parent eba2d41b29
commit 148c568850
2 changed files with 32 additions and 0 deletions

28
2019/day12/day12.hs Normal file
View File

@ -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)

4
2019/day12/input Normal file
View File

@ -0,0 +1,4 @@
<x=5, y=4, z=4>
<x=-11, y=-11, z=-3>
<x=0, y=7, z=0>
<x=-13, y=2, z=10>