SHARE
TWEET

Untitled

a guest Dec 11th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Import opCode Computer (Day 5) > codeInterpreter(software,input)
  2.     Adjusted to take multiple input parameters:
  3.       else if (dP[3] === "03") {
  4.       code.splice(code[i + 1], 1, inp[inputInstruction]);
  5.       inputInstruction += 1;
  6.       i += 2;}  */
  7.  
  8. let input = testInputLoop;
  9. let sequence = [1, 0, 4, 3, 2];
  10.  
  11. function factorial(x) {
  12.   if (x < 0) return;
  13.   if (x === 0) return 1;
  14.   return x * factorial(x - 1);
  15. }
  16.  
  17. function getAllPermutations(sequence) {
  18.   let seq = [...sequence];
  19.   let results = [];
  20.  
  21.   if (seq.length === 1) {
  22.     results.push(seq);
  23.     return results;
  24.   }
  25.  
  26.   for (let i = 0; i < seq.length; i++) {
  27.     let first = seq[i];
  28.     let leftOver = seq.filter(i => i != first);
  29.     let innerPermutations = getAllPermutations(leftOver);
  30.     for (let j = 0; j < innerPermutations.length; j++) {
  31.       results.push(first + innerPermutations[j]);
  32.     }
  33.   }
  34.   return results;
  35. }
  36.  
  37. let sequenceSet = getAllPermutations(sequence);
  38.  
  39. function getAmplifierOutput(input, first, second) {
  40.   return codeInterpreter(input, [first, second]);
  41. }
  42.  
  43. function getHighestOutput(input,seq , startValue) {
  44.   let firstAmplifier = getAmplifierOutput(input, seq[0], startValue);
  45.   let secondAmplifier = getAmplifierOutput(input, seq[1], firstAmplifier);
  46.   let thirdAmplifier = getAmplifierOutput(input, seq[2], secondAmplifier);
  47.   let fourthAmplifier = getAmplifierOutput(input, seq[3], thirdAmplifier);
  48.   let fifthAmplifier = getAmplifierOutput(input, seq[4], fourthAmplifier);
  49.   return fifthAmplifier;
  50. }
  51.  
  52. /*console.log(getHighestOutput(input,sequence, 0)) */
  53. /// Part II
  54.  
  55. let sequenceLoop = [9, 8, 7, 6, 5];
  56. let amp = [0, 0, 0, 0, 0];
  57. let lastOuput;
  58.  
  59. function getHighestLoop(input, seq, array, startValue) {
  60.   let amplifier = [...array];
  61.   if (
  62.     amplifier[0] == 99 ||
  63.     amplifier[1] == 99 ||
  64.     amplifier[2] == 99 ||
  65.     amplifier[3] == 99 ||
  66.     amplifier[4] == 99
  67.   ) {
  68.     return lastOutput;
  69.   } else {
  70.     amplifier[0] = getAmplifierOutput(input, seq[0], startValue);
  71.     amplifier[1] = getAmplifierOutput(input, seq[1], amplifier[0]);
  72.     amplifier[2] = getAmplifierOutput(input, seq[2], amplifier[1]);
  73.     amplifier[3] = getAmplifierOutput(input, seq[3], amplifier[2]);
  74.     amplifier[4] = getAmplifierOutput(input, seq[4], amplifier[3]);
  75.  
  76.     lastOutput = amplifier[4];
  77.     console.log(amplifier)
  78.     getHighestLoop(input, seq, amplifier, amplifier[4]);
  79.   }
  80. }
  81.  
  82. console.log(getHighestLoop(input, sequenceLoop, amp, 0));
  83.  
  84. /*function getAllOutputs(permutations, input) {
  85.   let outputSet = [];
  86.   for (let i = 0; i < permutations.length; i++) {
  87.     let output = getHighestOutput(input, permutations[i]);
  88.     outputSet.push(output);
  89.   }
  90.   return outputSet;
  91. }
  92.  
  93. let maxOutput = Math.max(...getAllOutputs(sequenceSet, input));
  94. console.log(maxOutput);
  95. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top