12_2.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const Moon = require("./Moon");
  2. const { step, parseInput } = require("./common");
  3. const input = parseInput([
  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. // const tests = parseInput([
  10. // "<x=-1, y=0, z=2>",
  11. // "<x=2, y=-10, z=-7>",
  12. // "<x=4, y=-8, z=8>",
  13. // "<x=3, y=5, z=-1>",
  14. // ]);
  15. const tests = parseInput([
  16. "<x=-8, y=-10, z=0>",
  17. "<x=5, y=5, z=10>",
  18. "<x=2, y=-7, z=3>",
  19. "<x=9, y=-8, z=-3>",
  20. ]);
  21. const moons = input;
  22. /**
  23. * The amount of steps it takes for a moon to reach its initial state
  24. *
  25. * Each index in this array corresponds to the index moon in the input.
  26. */
  27. const stepsToLoop = [0, 0, 0, 0];
  28. let loopsFound = 0;
  29. do {
  30. step(moons);
  31. if ((loopsFound & 1) == 0) {
  32. if (moons[0].hasReachedInitialState()) { loopsFound += 1; }
  33. stepsToLoop[0]++;
  34. }
  35. if ((loopsFound & 2) == 0) {
  36. if (moons[1].hasReachedInitialState()) { loopsFound += 2; }
  37. stepsToLoop[1]++;
  38. }
  39. if ((loopsFound & 4) == 0) {
  40. if (moons[2].hasReachedInitialState()) { loopsFound += 4; }
  41. stepsToLoop[2]++;
  42. }
  43. if ((loopsFound & 8) == 0) {
  44. if (moons[3].hasReachedInitialState()) { loopsFound += 8; }
  45. stepsToLoop[3]++;
  46. }
  47. } while (loopsFound != 15);
  48. console.log(stepsToLoop);
  49. // console.log(`Steps required to loop: ${steps}`);