I see I am trying not to crash today. I’ve been given some pretty simple
instructions on how to handle tracking how gravity applies to the moons, so
first I’ll go about implementing that. A moon can be a map that contains its
position and its velocity: %{p: {0, 0, 0}, v: {0, 0, 0}}
defp apply_velocity(%{p: {x1, y1, z1}, v: {x2, y2, z2} = v}) do
%{p: {x1 + x2, y1 + y2, z1 + z2}, v: v}
end
defp set_velocity(%{p: p1, v: v1}, %{p: p2}) do
%{p: p1, v: apply_gravity(v1, gravity_diff(p1, p2))}
end
defp apply_gravity({x1, y1, z1}, {x2, y2, z2}) do
{x1 + x2, y1 + y2, z1 + z2}
end
defp gravity_diff({x1, y1, z1}, {x2, y2, z2}) do
{gravity_diff(x1, x2), gravity_diff(y1, y2), gravity_diff(z1, z2)}
end
defp gravity_diff(p1, p2) when p1 > p2, do: -1
defp gravity_diff(p1, p2) when p1 == p2, do: 0
defp gravity_diff(p1, p2) when p1 < p2, do: 1
apply_gravity/2
takes the positions of 2 moons, and computes how gravity
affects the velocity of the first moon. gravity_diff/2
takes two positions and
calculates how gravity affects the first moon by returning a differential, such
as {-1, 1, 0}
. set_velocity/2
uses the above two functions to apply gravity
to the first moon, as computed above, and apply_velocity/1
alters the given
moon’s position by its velocity. All that’s left is to chunk the moons through
it for 1000 steps and calculate the final energy output.