12_1.js 1.4 KB

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