1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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],
- ];
- 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();
- }
- 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);
|