Browse Source

Add start of day 7 pt 1

ApisNecros 1 year ago
parent
commit
a749496341
1 changed files with 65 additions and 0 deletions
  1. 65 0
      7/7_1.js

+ 65 - 0
7/7_1.js

@@ -0,0 +1,65 @@
+const Computer = require("../IntComp/Computer");
+
+const input = [];
+
+const demos = [
+    [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0],
+    [3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99, 0, 0],
+    [3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33, 1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0],
+];
+
+// Create our array of amplifiers
+const AMPLIFIER_COUNT = 5;
+const amplifierArray = [];
+for (let i = 0; i < AMPLIFIER_COUNT; i++) {
+    amplifierArray.push(new Computer(demos[0]));
+}
+
+const amplifierConfigurations = permute([...Array(5).keys()]);
+
+let maxOutput = -999;
+for (const configuration of amplifierConfigurations) {
+    amplifierArray.forEach((comp) => comp.Reset());
+
+    for (let i = 0; i < AMPLIFIER_COUNT - 1; i++) {
+        amplifierArray[configuration[i]].outputComputer = amplifierArray[configuration[i - 1]];
+    }
+
+    amplifierArray[configuration[0]].RunWithInput();
+
+    const finalOutput = amplifierArray[AMPLIFIER_COUNT - 1].FetchOutputValue();
+}
+
+/**
+ * Create an array of all permutations of another array
+ *
+ * @param {unknown[]} permutation The array to create permutations of
+ * @returns {Array[]} An array of all permutations of the input array
+ */
+function permute(permutation) {
+    const length = permutation.length;
+    const result = [permutation.slice()];
+    const c = new Array(length).fill(0);
+    let i = 1;
+    let k;
+    let p;
+
+    while (i < length) {
+        if (c[i] < i) {
+            k = i % 2 && c[i];
+            p = permutation[i];
+            permutation[i] = permutation[k];
+            permutation[k] = p;
+            ++c[i];
+            i = 1;
+            result.push(permutation.slice());
+        }
+        else {
+            c[i] = 0;
+            ++i;
+        }
+    }
+    return result;
+}
+
+console.log(amplifierConfigurations);