Parcourir la source

Add day 3 part 2 solution

ApisNecros il y a 2 semaines
Parent
commit
d5d224a8e5
2 fichiers modifiés avec 33 ajouts et 0 suppressions
  1. 32 0
      3/3_2.js
  2. 1 0
      3/test_2.input

+ 32 - 0
3/3_2.js

@@ -0,0 +1,32 @@
+import { loadInput } from "../common.js";
+
+// const input = loadInput("./3/test_2.input", parseInput, true);
+const input = loadInput(3, parseInput);
+
+solve(input);
+
+/**
+ * @param {string} memDump
+ */
+function parseInput(memDump) {
+    return Array.from(memDump.matchAll(/(?:(do(?:n't)?\(\))|(mul\(\d{1,3},\d{1,3}\)))/g));
+}
+
+function solve(validInstructions) {
+    let sum = 0;
+    let enabled = true;
+    for(const match of validInstructions) {
+        const instruction = match[0];
+
+        if (instruction == "do()") { enabled = true; continue; }
+        else if (instruction == "don't()") { enabled = false; continue; }
+
+        if (enabled) {
+            sum += eval(instruction);
+        }
+    }
+
+    console.log("Sum of result of valid instructions:", sum);
+}
+
+function mul(a,b) { return a*b; }

+ 1 - 0
3/test_2.input

@@ -0,0 +1 @@
+xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))