You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
moons = {}
|
|
for line in io.lines(arg[1]) do
|
|
x, y, z = line:match("<x=(-?%d+), +y=(-?%d+), +z=(-?%d+)>")
|
|
table.insert(moons, {pos={x=tonumber(x), y=tonumber(y), z=tonumber(z)}, vel={x=0, y=0, z=0}})
|
|
end
|
|
|
|
function update_vel(moon)
|
|
for _, omoon in ipairs(moons) do
|
|
for _, axis in ipairs({"x", "y", "z"}) do
|
|
if omoon.pos[axis] > moon.pos[axis] then
|
|
moon.vel[axis] = moon.vel[axis] + 1
|
|
end
|
|
if omoon.pos[axis] < moon.pos[axis] then
|
|
moon.vel[axis] = moon.vel[axis] - 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function update_pos(moon)
|
|
moon.pos.x = moon.pos.x + moon.vel.x
|
|
moon.pos.y = moon.pos.y + moon.vel.y
|
|
moon.pos.z = moon.pos.z + moon.vel.z
|
|
end
|
|
|
|
print("After 0 steps:")
|
|
for _, moon in ipairs(moons) do
|
|
print(string.format("pos=<x=%d, y=%d, z=%d>, vel=<x=%d, y=%d, z=%d>", moon.pos.x, moon.pos.y, moon.pos.z, moon.vel.x, moon.vel.y, moon.vel.z))
|
|
end
|
|
print()
|
|
for i = 1, 10 do
|
|
for _, moon in ipairs(moons) do
|
|
update_vel(moon)
|
|
end
|
|
|
|
for _, moon in ipairs(moons) do
|
|
update_pos(moon)
|
|
end
|
|
|
|
print(string.format("After %d steps:", i))
|
|
for _, moon in ipairs(moons) do
|
|
print(string.format("pos=<x=%d, y=%d, z=%d>, vel=<x=%d, y=%d, z=%d>", moon.pos.x, moon.pos.y, moon.pos.z, moon.vel.x, moon.vel.y, moon.vel.z))
|
|
end
|
|
print()
|
|
end
|
|
|
|
energy = 0
|
|
for _, moon in ipairs(moons) do
|
|
pot = math.abs(moon.pos.x) + math.abs(moon.pos.y) + math.abs(moon.pos.z)
|
|
kin = math.abs(moon.vel.x) + math.abs(moon.pos.y) + math.abs(moon.pos.z)
|
|
energy = energy + pot * kin
|
|
end
|
|
print("Energy:", energy)
|
|
|