7_1.js 1.8 KB

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