1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- const Moon = require("./Moon");
- const { step, parseInput } = require("./common");
- const input = parseInput([
- "<x=14, y=15, z=-2>",
- "<x=17, y=-3, z=4>",
- "<x=6, y=12, z=-13>",
- "<x=-2, y=10, z=-8>",
- ]);
- const tests = parseInput([
- "<x=-1, y=0, z=2>",
- "<x=2, y=-10, z=-7>",
- "<x=4, y=-8, z=8>",
- "<x=3, y=5, z=-1>",
- ]);
- 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)));
|