123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const util = require("util");
- const Vector3 = require("./Vector3");
- 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>",
- ];
- /**
- * 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),
- );
- }
- /**
- * @param {string[]} inputs The day's input
- * @returns {Vector3[]} An array of Vector3's representing the positions of moons
- */
- function parseInput(inputs) {
- const moons = [];
- const parseRegex = new RegExp(/x=(-?\d+), y=(-?\d+), z=(-?\d+)/);
- for (const position of inputs) {
- // eslint-disable-next-line no-shadow-restricted-names
- const [undefined, x, y, z] = position.match(parseRegex);
- moons.push(new Vector3(parseInt(x, 10), parseInt(y, 10), parseInt(z, 10)));
- }
- return moons;
- }
- console.log(util.inspect(parseInput(input)));
|