const Moon = require("./Moon"); const { step, parseInput } = require("./common"); const input = parseInput([ "", "", "", "", ]); const tests = parseInput([ "", "", "", "", ]); const STEPS = 1000; for (let i = 0; i < STEPS; i++) { step(input); } console.log(`Total energy in system is: ${calculateTotalEnergy(input)}`); /** * Calculate the total energy in the system * * The total energy in the system is the sum of the sum of the positional * values multiplied by the sum of the velocity values. * * @param {Moon[]} moons The moons in the system * @returns {number} The total energy in the system */ function calculateTotalEnergy(moons) { let energy = 0; for (const moon of moons) { const potentialEnergy = Math.abs(moon.position.x) + Math.abs(moon.position.y) + Math.abs(moon.position.z); const kineticEnergy = Math.abs(moon.velocity.x) + Math.abs(moon.velocity.y) + Math.abs(moon.velocity.z); energy += potentialEnergy * kineticEnergy; } return energy; } // console.log(util.inspect(parseInput(input)));