12_1.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const util = require("util");
  2. const Vector3 = require("./Vector3");
  3. const Moon = require("./Moon");
  4. const input = [
  5. "<x=14, y=15, z=-2>",
  6. "<x=17, y=-3, z=4>",
  7. "<x=6, y=12, z=-13>",
  8. "<x=-2, y=10, z=-8>",
  9. ];
  10. /**
  11. * Find the gravitational influence two moons have on each other
  12. *
  13. * @param {Vector3} moonA The first moon to consider
  14. * @param {Vector3} moonB The second moon to consider
  15. *
  16. * @returns {void}
  17. */
  18. function calculateGravity(moonA, moonB) {
  19. // The gravitational pull on Moon A caused by Moon B
  20. const gravityA = new Vector3(
  21. // eslint-disable-next-line no-nested-ternary
  22. moonB.x > moonA.x ? 1 : (moonB.x < moonA.x ? -1 : 0),
  23. // eslint-disable-next-line no-nested-ternary
  24. moonB.y > moonA.y ? 1 : (moonB.y < moonA.y ? -1 : 0),
  25. // eslint-disable-next-line no-nested-ternary
  26. moonB.z > moonA.z ? 1 : (moonB.z < moonA.z ? -1 : 0),
  27. );
  28. }
  29. /**
  30. * @param {string[]} inputs The day's input
  31. * @returns {Moon[]} An array of Moon objects
  32. */
  33. function parseInput(inputs) {
  34. const moons = [];
  35. const parseRegex = new RegExp(/x=(-?\d+), y=(-?\d+), z=(-?\d+)/);
  36. for (const position of inputs) {
  37. // eslint-disable-next-line no-shadow-restricted-names
  38. const [undefined, x, y, z] = position.match(parseRegex);
  39. moons.push(new Moon(new Vector3(parseInt(x, 10), parseInt(y, 10), parseInt(z, 10))));
  40. }
  41. return moons;
  42. }
  43. // console.log(util.inspect(parseInput(input)));