Explorar o código

Add function to parse the day's input

ApisNecros hai 4 meses
pai
achega
5581773c3f
Modificáronse 1 ficheiros con 19 adicións e 0 borrados
  1. 19 0
      12/12_1.js

+ 19 - 0
12/12_1.js

@@ -53,3 +53,22 @@ function calculateGravity(moonA, moonB) {
         moonB.z > moonA.z ? 1 : (moonB.z < moonA.z ? -1 : 0),
     );
 }
+
+/**
+ * @param {string[]} inputs The day's input
+ * @returns {Vector3[]} An array of Vector3's representing the positions of moons
+ */
+function parseInput(inputs) {
+    const moons = [];
+    const parseRegex = new RegExp(/x=(-?\d+), y=(-?\d+), z=(-?\d+)/);
+
+    for (const position of inputs) {
+        // eslint-disable-next-line no-shadow-restricted-names
+        const [undefined, x, y, z] = position.match(parseRegex);
+        moons.push(new Vector3(parseInt(x, 10), parseInt(y, 10), parseInt(z, 10)));
+    }
+
+    return moons;
+}
+
+console.log(util.inspect(parseInput(input)));