7_1.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99, 0, 0],
  7. [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],
  8. ];
  9. // Create our array of amplifiers
  10. const AMPLIFIER_COUNT = 5;
  11. const amplifierArray = [];
  12. for (let i = 0; i < AMPLIFIER_COUNT; i++) {
  13. amplifierArray.push(new Computer(input, { inputModeMap: [InputModes.INPUT_FROM_RUNTIME_STACK, InputModes.INPUT_FROM_RUNTIME_STACK] }));
  14. }
  15. const amplifierConfigurations = permute([...Array(5).keys()]);
  16. let maxOutput = -999;
  17. for (const configuration of amplifierConfigurations) {
  18. amplifierArray.forEach((comp) => comp.Reset());
  19. let amplifierOutput = 0;
  20. for (let i = 0; i < AMPLIFIER_COUNT; i++) {
  21. amplifierArray[i].RunWithInput([configuration[i], amplifierOutput]);
  22. amplifierOutput = amplifierArray[i].FetchOutputValue();
  23. }
  24. if (amplifierOutput > maxOutput) {
  25. maxOutput = amplifierOutput;
  26. }
  27. }
  28. console.log(`Largest output: ${maxOutput}`);
  29. /**
  30. * Create an array of all permutations of another array
  31. *
  32. * @param {unknown[]} permutation The array to create permutations of
  33. * @returns {Array[]} An array of all permutations of the input array
  34. */
  35. function permute(permutation) {
  36. const length = permutation.length;
  37. const result = [permutation.slice()];
  38. const c = new Array(length).fill(0);
  39. let i = 1;
  40. let k;
  41. let p;
  42. while (i < length) {
  43. if (c[i] < i) {
  44. k = i % 2 && c[i];
  45. p = permutation[i];
  46. permutation[i] = permutation[k];
  47. permutation[k] = p;
  48. ++c[i];
  49. i = 1;
  50. result.push(permutation.slice());
  51. }
  52. else {
  53. c[i] = 0;
  54. ++i;
  55. }
  56. }
  57. return result;
  58. }