Explorar el Código

Add day 1 part 1 solution

ApisNecros hace 3 meses
padre
commit
d4dddd6bce
Se han modificado 2 ficheros con 51 adiciones y 0 borrados
  1. 45 0
      1/1_1.js
  2. 6 0
      1/test.input

+ 45 - 0
1/1_1.js

@@ -0,0 +1,45 @@
+import { loadInput, numericAscSort } from "../common.js";
+
+const input = loadInput(1, parseInput);
+
+solve(input);
+
+/**
+ * @param {string} inputData 
+ * @returns {number[][]}
+ */
+function parseInput(inputData) {
+    // Split the input into lines, and then split each line into two numbers
+    const lines = inputData.split(/\n/);
+    let leftList = [];
+    let rightList = [];
+
+    // split each line into two numbers, then add those to the left and right lists
+    for(const line of lines) {
+        const [l, r] = line.split(/\s+/).map((num) => parseInt(num, 10));
+        leftList.push(l);
+        rightList.push(r);
+    }
+
+    leftList.sort(numericAscSort);
+    rightList.sort(numericAscSort);
+
+    return [leftList, rightList];
+}
+
+/**
+ * @param {number[][]} parsedInput 
+ */
+function solve(parsedInput) {
+    // separate out the already sorted left and right list
+    const [leftList, rightList] = parsedInput;
+    // Create a list to store the differences between the left and right list
+    const deltaList = [];
+
+    while(leftList.length) {
+        deltaList.push(Math.abs(rightList.shift() - leftList.shift()));
+    }
+
+    console.log("List Distance: ", eval(deltaList.join("+")));
+    
+}

+ 6 - 0
1/test.input

@@ -0,0 +1,6 @@
+3   4
+4   3
+2   5
+1   3
+3   9
+3   3