123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- const Computer = require("../IntComp/Computer");
- const InputModes = require("../IntComp/InputModes");
- const input = [3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 46, 55, 68, 89, 110, 191, 272, 353, 434, 99999, 3, 9, 1002, 9, 3, 9, 1001, 9, 3, 9, 102, 4, 9, 9, 101, 4, 9, 9, 1002, 9, 5, 9, 4, 9, 99, 3, 9, 102, 3, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 5, 9, 102, 4, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 5, 9, 1002, 9, 2, 9, 1001, 9, 5, 9, 1002, 9, 3, 9, 4, 9, 99, 3, 9, 101, 3, 9, 9, 102, 3, 9, 9, 101, 3, 9, 9, 1002, 9, 4, 9, 4, 9, 99, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 99, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 99];
- const demos = [
- [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0],
- [3, 26, 1001, 26, -4, 26, 3, 27, 1002, 27, 2, 27, 1, 27, 26, 27, 4, 27, 1001, 28, -1, 28, 1005, 28, 6, 99, 0, 0, 5],
- [3, 52, 1001, 52, -5, 52, 3, 53, 1, 52, 56, 54, 1007, 54, 5, 55, 1005, 55, 26, 1001, 54, -5, 54, 1105, 1, 12, 1, 53, 54, 53, 1008, 54, 0, 55, 1001, 55, 1, 55, 2, 53, 55, 53, 4, 53, 1001, 56, -1, 56, 1005, 56, 6, 99, 0, 0, 0, 0, 10],
- ];
- function main() {
- // Create our array of amplifiers
- const AMPLIFIER_COUNT = 5;
- /** @type {Computer[]} */
- const amplifierArray = [];
- for (let i = 0; i < AMPLIFIER_COUNT; i++) {
- amplifierArray.push(new Computer(input, {
- inputModeMap: [InputModes.INPUT_FROM_RUNTIME_STACK, InputModes.INPUT_FROM_RUNTIME_STACK],
- }));
- }
- // Set the amplifiers' output computers
- for (let i = 0; i < AMPLIFIER_COUNT; i++) {
- const outputIndex = i < AMPLIFIER_COUNT - 1 ? i + 1 : 0;
- amplifierArray[i].outputComputer = amplifierArray[outputIndex];
- }
- const amplifierConfigurations = permute([5, 6, 7, 8, 9]);
- let maxOutput = -999;
- for (const config of amplifierConfigurations) {
- amplifierArray.forEach((comp) => { comp.Reset(); });
- // Initial amplifier output to begin each cycle with
- let amplifierOutput = 0;
- // Set the phases for the amplifiers
- for (let i = 0; i < AMPLIFIER_COUNT; i++) {
- amplifierArray[i].Input(config[i]);
- }
- // Set the second input for the first amplifier to 0
- amplifierArray[0].Input(0);
- // Run all of the computers
- amplifierArray.forEach((comp) => comp.Run());
- // Pause until all computers halt
- while (amplifierArray[0].running || amplifierArray[1].running || amplifierArray[2].running || amplifierArray[3].running || amplifierArray[4].running)
- // eslint-disable-next-line no-empty, brace-style
- {}
- amplifierOutput = amplifierArray[0].runtimeInput.shift();
- if (amplifierOutput > maxOutput) { maxOutput = amplifierOutput; }
- }
- console.log(`Largest output: ${maxOutput}`);
- }
- main();
- /**
- * 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;
- }
|