7_2.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const Computer = require("../IntComp/Computer");
  2. const InputModes = require("../IntComp/InputModes");
  3. 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];
  4. const demos = [
  5. [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0],
  6. [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],
  7. [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],
  8. ];
  9. function main() {
  10. // Create our array of amplifiers
  11. const AMPLIFIER_COUNT = 5;
  12. /** @type {Computer[]} */
  13. const amplifierArray = [];
  14. for (let i = 0; i < AMPLIFIER_COUNT; i++) {
  15. amplifierArray.push(new Computer(input, {
  16. inputModeMap: [InputModes.INPUT_FROM_RUNTIME_STACK, InputModes.INPUT_FROM_RUNTIME_STACK],
  17. }));
  18. }
  19. // Set the amplifiers' output computers
  20. for (let i = 0; i < AMPLIFIER_COUNT; i++) {
  21. const outputIndex = i < AMPLIFIER_COUNT - 1 ? i + 1 : 0;
  22. amplifierArray[i].outputComputer = amplifierArray[outputIndex];
  23. }
  24. const amplifierConfigurations = permute([5, 6, 7, 8, 9]);
  25. let maxOutput = -999;
  26. for (const config of amplifierConfigurations) {
  27. amplifierArray.forEach((comp) => { comp.Reset(); });
  28. // Initial amplifier output to begin each cycle with
  29. let amplifierOutput = 0;
  30. // Set the phases for the amplifiers
  31. for (let i = 0; i < AMPLIFIER_COUNT; i++) {
  32. amplifierArray[i].Input(config[i]);
  33. }
  34. // Set the second input for the first amplifier to 0
  35. amplifierArray[0].Input(0);
  36. // Run all of the computers
  37. amplifierArray.forEach((comp) => comp.Run());
  38. // Pause until all computers halt
  39. while (amplifierArray[0].running || amplifierArray[1].running || amplifierArray[2].running || amplifierArray[3].running || amplifierArray[4].running)
  40. // eslint-disable-next-line no-empty, brace-style
  41. {}
  42. amplifierOutput = amplifierArray[0].runtimeInput.shift();
  43. if (amplifierOutput > maxOutput) { maxOutput = amplifierOutput; }
  44. }
  45. console.log(`Largest output: ${maxOutput}`);
  46. }
  47. main();
  48. /**
  49. * Create an array of all permutations of another array
  50. *
  51. * @param {unknown[]} permutation The array to create permutations of
  52. * @returns {Array[]} An array of all permutations of the input array
  53. */
  54. function permute(permutation) {
  55. const length = permutation.length;
  56. const result = [permutation.slice()];
  57. const c = new Array(length).fill(0);
  58. let i = 1;
  59. let k;
  60. let p;
  61. while (i < length) {
  62. if (c[i] < i) {
  63. k = i % 2 && c[i];
  64. p = permutation[i];
  65. permutation[i] = permutation[k];
  66. permutation[k] = p;
  67. ++c[i];
  68. i = 1;
  69. result.push(permutation.slice());
  70. }
  71. else {
  72. c[i] = 0;
  73. ++i;
  74. }
  75. }
  76. return result;
  77. }