12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- const Moon = require("./Moon");
- const { step, parseInput } = require("./common");
- const input = parseInput([
- "<x=14, y=15, z=-2>",
- "<x=17, y=-3, z=4>",
- "<x=6, y=12, z=-13>",
- "<x=-2, y=10, z=-8>",
- ]);
- // const tests = parseInput([
- // "<x=-1, y=0, z=2>",
- // "<x=2, y=-10, z=-7>",
- // "<x=4, y=-8, z=8>",
- // "<x=3, y=5, z=-1>",
- // ]);
- const tests = parseInput([
- "<x=-8, y=-10, z=0>",
- "<x=5, y=5, z=10>",
- "<x=2, y=-7, z=3>",
- "<x=9, y=-8, z=-3>",
- ]);
- const moons = input;
- /**
- * The amount of steps it takes for a moon to reach its initial state
- *
- * Each index in this array corresponds to the index moon in the input.
- */
- const stepsToLoop = [0, 0, 0, 0];
- let loopsFound = 0;
- do {
- step(moons);
- if ((loopsFound & 1) == 0) {
- if (moons[0].hasReachedInitialState()) { loopsFound += 1; }
- stepsToLoop[0]++;
- }
- if ((loopsFound & 2) == 0) {
- if (moons[1].hasReachedInitialState()) { loopsFound += 2; }
- stepsToLoop[1]++;
- }
- if ((loopsFound & 4) == 0) {
- if (moons[2].hasReachedInitialState()) { loopsFound += 4; }
- stepsToLoop[2]++;
- }
- if ((loopsFound & 8) == 0) {
- if (moons[3].hasReachedInitialState()) { loopsFound += 8; }
- stepsToLoop[3]++;
- }
- } while (loopsFound != 15);
- console.log(stepsToLoop);
- // console.log(`Steps required to loop: ${steps}`);
|