|
@@ -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; }
|