12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const util = require("util");
- const input = [
- "<x=14, y=15, z=-2>",
- "<x=17, y=-3, z=4>",
- "<x=6, y=12, z=-13>",
- "<x=-2, y=10, z=-8>",
- ];
- class Vector3 {
- /**
- * A 3D vector
- *
- * @param {number} x The X value of this vector
- * @param {number} y The Y value of this vector
- * @param {number} z The Z value of this vector
- */
- constructor(x, y, z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- /**
- * Add another vector to this one
- *
- * @param {Vector3} operand The vector to add to this one
- * @returns {void}
- */
- add(operand) {
- this.x += operand.x;
- this.y += operand.y;
- this.z += operand.z;
- }
- }
- /**
- * Find the gravitational influence two moons have on each other
- *
- * @param {Vector3} moonA The first moon to consider
- * @param {Vector3} moonB The second moon to consider
- *
- * @returns {void}
- */
- function calculateGravity(moonA, moonB) {
- // The gravitational pull on Moon A caused by Moon B
- const gravityA = new Vector3(
- // eslint-disable-next-line no-nested-ternary
- moonB.x > moonA.x ? 1 : (moonB.x < moonA.x ? -1 : 0),
- // eslint-disable-next-line no-nested-ternary
- moonB.y > moonA.y ? 1 : (moonB.y < moonA.y ? -1 : 0),
- // eslint-disable-next-line no-nested-ternary
- moonB.z > moonA.z ? 1 : (moonB.z < moonA.z ? -1 : 0),
- );
- }
|