瀏覽代碼

Add function to parse the day's input

ApisNecros 4 月之前
父節點
當前提交
5581773c3f
共有 1 個文件被更改,包括 19 次插入0 次删除
  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)));