Day 12: Aesthetics
This commit is contained in:
parent
12d28015d4
commit
457a7e7d73
|
@ -1,7 +1,6 @@
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.List.Split
|
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"
|
||||||
|
@ -9,17 +8,20 @@ main = do
|
||||||
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
|
parseContent :: String -> [[Int]]
|
||||||
part1 x = energy $ iterate step x !! 1000
|
parseContent = map (map read . splitOn ",") . lines . removeJunk
|
||||||
|
where
|
||||||
|
removeJunk xs = [x | x <- xs, x `notElem` " <>xyz="]
|
||||||
|
|
||||||
--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]]
|
part1 :: ([V3 Int], [V3 Int]) -> Int
|
||||||
parseContent = map (map read . splitOn ",") . lines . removeJunk
|
part1 input = energy $ iterate step input !! 1000
|
||||||
where
|
where
|
||||||
removeJunk xs = [x | x <- xs, x `notElem` " <>xyz="]
|
energy (x, y) = sum $ zipWith (*) (geten x) (geten y)
|
||||||
|
geten = map (sum . abs)
|
||||||
|
|
||||||
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)
|
||||||
|
@ -28,18 +30,6 @@ step (moons, vel) = (zipWith (+) moons newVel, newVel)
|
||||||
newVel = zipWith (+) vel dVel
|
newVel = zipWith (+) vel dVel
|
||||||
gravity xs = [sum [signum $ x - y | x <- xs, y /= x] | y <- xs]
|
gravity xs = [sum [signum $ x - y | x <- xs, y /= x] | y <- xs]
|
||||||
|
|
||||||
energy :: ([V3 Int], [V3 Int]) -> Int
|
|
||||||
energy (x, y) = sum $ zipWith (*) (geten x) (geten y)
|
|
||||||
where
|
|
||||||
geten = map (sum . abs)
|
|
||||||
|
|
||||||
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)
|
|
||||||
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
|
||||||
|
@ -49,6 +39,14 @@ part2 (moons, vel) = lcm' periods
|
||||||
periods = zipWith (curry findPeriod') m v
|
periods = zipWith (curry findPeriod') m v
|
||||||
findPeriod' x = findPeriod x x 1
|
findPeriod' x = findPeriod x x 1
|
||||||
|
|
||||||
|
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)
|
||||||
|
where
|
||||||
|
x' = step x
|
||||||
|
|
||||||
lcm' :: [Int] -> Int
|
lcm' :: [Int] -> Int
|
||||||
lcm' xs = product $ zipWith (^) nums maxElems
|
lcm' xs = product $ zipWith (^) nums maxElems
|
||||||
where
|
where
|
||||||
|
|
Loading…
Reference in New Issue
Block a user