Переглянути джерело

Add initial attempt at day 12 pt 2

ApisNecros 4 місяців тому
батько
коміт
7c807aa864
1 змінених файлів з 61 додано та 0 видалено
  1. 61 0
      12/12_2.js

+ 61 - 0
12/12_2.js

@@ -0,0 +1,61 @@
+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}`);