SHARE
TWEET

Untitled

a guest Dec 11th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Memory - initial puzzle input, a list of integers
  2. const input = [1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,608,1029,1102,1,29,1006,1101,39,0,1016,1101,1,0,1021,1101,37,0,1008,1101,0,25,1003,1102,32,1,1002,1101,0,35,1007,1102,1,28,1009,1101,0,31,1012,1101,22,0,1010,1101,319,0,1026,1102,1,23,1019,1102,423,1,1024,1101,27,0,1017,1101,0,36,1005,1101,0,0,1020,1101,681,0,1022,1102,1,30,1015,1101,0,24,1004,1102,312,1,1027,1102,1,21,1000,1102,1,34,1018,1101,0,678,1023,1101,0,38,1011,1102,1,418,1025,1102,1,20,1014,1101,33,0,1001,1101,0,26,1013,1102,1,613,1028,109,3,1202,5,1,63,1008,63,36,63,1005,63,205,1001,64,1,64,1105,1,207,4,187,1002,64,2,64,109,11,21108,40,40,0,1005,1014,229,4,213,1001,64,1,64,1105,1,229,1002,64,2,64,109,-19,1202,6,1,63,1008,63,33,63,1005,63,255,4,235,1001,64,1,64,1105,1,255,1002,64,2,64,109,3,1201,8,0,63,1008,63,29,63,1005,63,277,4,261,1106,0,281,1001,64,1,64,1002,64,2,64,109,10,21107,41,42,3,1005,1011,299,4,287,1106,0,303,1001,64,1,64,1002,64,2,64,109,19,2106,0,0,1001,64,1,64,1105,1,321,4,309,1002,64,2,64,109,-15,21107,42,41,-2,1005,1010,341,1001,64,1,64,1106,0,343,4,327,1002,64,2,64,109,6,2101,0,-9,63,1008,63,30,63,1005,63,363,1106,0,369,4,349,1001,64,1,64,1002,64,2,64,109,-11,1208,-5,29,63,1005,63,389,1001,64,1,64,1106,0,391,4,375,1002,64,2,64,109,15,1206,-2,409,4,397,1001,64,1,64,1105,1,409,1002,64,2,64,109,-3,2105,1,5,4,415,1105,1,427,1001,64,1,64,1002,64,2,64,109,-18,21101,43,0,10,1008,1011,42,63,1005,63,447,1106,0,453,4,433,1001,64,1,64,1002,64,2,64,109,19,1205,1,467,4,459,1105,1,471,1001,64,1,64,1002,64,2,64,109,-5,2107,34,-8,63,1005,63,489,4,477,1106,0,493,1001,64,1,64,1002,64,2,64,109,-11,2102,1,-1,63,1008,63,28,63,1005,63,517,1001,64,1,64,1105,1,519,4,499,1002,64,2,64,109,8,2108,37,-5,63,1005,63,539,1001,64,1,64,1106,0,541,4,525,1002,64,2,64,109,17,1206,-8,557,1001,64,1,64,1105,1,559,4,547,1002,64,2,64,109,-11,1205,2,571,1105,1,577,4,565,1001,64,1,64,1002,64,2,64,109,-14,1207,0,25,63,1005,63,599,4,583,1001,64,1,64,1105,1,599,1002,64,2,64,109,32,2106,0,-8,4,605,1105,1,617,1001,64,1,64,1002,64,2,64,109,-27,2102,1,-5,63,1008,63,24,63,1005,63,639,4,623,1105,1,643,1001,64,1,64,1002,64,2,64,109,-16,2101,0,10,63,1008,63,25,63,1005,63,669,4,649,1001,64,1,64,1105,1,669,1002,64,2,64,109,22,2105,1,8,1106,0,687,4,675,1001,64,1,64,1002,64,2,64,109,-21,1208,8,32,63,1005,63,705,4,693,1105,1,709,1001,64,1,64,1002,64,2,64,109,19,1207,-5,36,63,1005,63,729,1001,64,1,64,1105,1,731,4,715,1002,64,2,64,109,9,21101,44,0,-5,1008,1017,44,63,1005,63,753,4,737,1105,1,757,1001,64,1,64,1002,64,2,64,109,-12,21108,45,46,5,1005,1015,773,1105,1,779,4,763,1001,64,1,64,1002,64,2,64,109,-8,2108,25,1,63,1005,63,801,4,785,1001,64,1,64,1105,1,801,1002,64,2,64,109,-12,2107,22,10,63,1005,63,817,1106,0,823,4,807,1001,64,1,64,1002,64,2,64,109,23,1201,-8,0,63,1008,63,38,63,1005,63,847,1001,64,1,64,1106,0,849,4,829,1002,64,2,64,109,-3,21102,46,1,4,1008,1014,46,63,1005,63,871,4,855,1106,0,875,1001,64,1,64,1002,64,2,64,109,5,21102,47,1,2,1008,1017,46,63,1005,63,899,1001,64,1,64,1105,1,901,4,881,4,64,99,21101,0,27,1,21101,0,915,0,1105,1,922,21201,1,42136,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,0,942,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2105,1,0];
  3.  
  4. // copy of initial input, so we can reset properly
  5. let inputCopy = [...input];
  6.  
  7. // relative base
  8. let relBase = 0;
  9.  
  10. // opcode 1 - get values at position 1&2 right after code, add together, store in position 3
  11. function opcode1 (a, b, c, p) {
  12.   let valA = ptest(p[0], a);
  13.   let valB = ptest(p[1], b);
  14.   inputCopy[c] = valA + valB;
  15.   console.log(`op1: ${valA} + ${valB} = ${valA + valB}`)
  16. }
  17.  
  18. // opcode 2 - get values at position 1&2 right after code, multiply, store in position 3
  19. function opcode2 (a, b, c, p) {
  20.   let valA = ptest(p[0], a);
  21.   let valB = ptest(p[1], b);
  22.   inputCopy[c] = valA * valB;
  23.   console.log(`op2: ${valA} * ${valB} = ${valA * valB}`)
  24. }
  25.  
  26.  
  27. // opcode 3 - takes an input and stores in position 1
  28. function opcode3 (iv, s, p) {
  29.   let pos = ptest(p[0], s);
  30.   inputCopy[pos] = iv;
  31.   console.log(`op3: putting ${iv} into spot ${pos}`)
  32. }
  33.  
  34. // opcode 4 - outputs value at position 1
  35. function opcode4 (s, p) {
  36.   let val = ptest(p[0], s);
  37.   console.log(`op4: outputting ${val}`)
  38.   return val;
  39. }
  40.  
  41. // opcode 5 - if position 1 != 0, changes i to position 2; otherwise, does nothing
  42. function opcode5 (a, b, inp, p) {
  43.   let valA = ptest(p[0], a);
  44.   let valB = ptest(p[1], b);
  45.  
  46.   if (valA !== 0) {
  47.     inp = valB;
  48.   }
  49.   console.log(`op5: ${valA} and ${valB}, inst. pointer is now ${inp}`);
  50.   return inp;
  51. }
  52.  
  53. // opcode 6 - if position 1 == 0, changes i to position 2; otherwise, does nothing
  54. function opcode6 (a, b, inp, p) {
  55.   let valA = ptest(p[0], a);
  56.   let valB = ptest(p[1], b);
  57.  
  58.   if (valA === 0) {
  59.     inp = valB;
  60.   }
  61.   console.log(`op6: ${valA} and ${valB}, inst. pointer is now ${inp}`);
  62.  
  63.   return inp;
  64. }
  65.  
  66. // opcode 7 - if position 1 < position 2, position 3 is set to 1; otherwise, it's set to 0
  67. function opcode7 (a, b, c, p) {
  68.   let valA = ptest(p[0], a);
  69.   let valB = ptest(p[1], b);
  70.  
  71.   if (valA < valB) {
  72.     inputCopy[c] = 1;
  73.   } else {
  74.     inputCopy[c] = 0;
  75.   }
  76.   console.log(`op7: comparing if ${valA} is < ${valB}`);
  77. }
  78.  
  79. // opcode 8 - if position 1 == position 2, position 3 is set to 1; otherwise, it's set to 0
  80. function opcode8 (a, b, c, p) {
  81.   let valA = ptest(p[0], a);
  82.   let valB = ptest(p[1], b);
  83.  
  84.   if (valA == valB) {
  85.     inputCopy[c] = 1;
  86.   } else {
  87.     inputCopy[c] = 0;
  88.   }
  89.   console.log(`op8: comparing if ${valA} equals ${valB}`);
  90. }
  91.  
  92. function opcode9 (a, p) {
  93.   let valA = ptest(p[0], a);
  94.   relBase += valA;
  95.   console.log(`op9: new relative base is ${relBase}`);
  96. }
  97.  
  98. // allows parameter modes - 0 for position mode (value stored at position given); 1 for immediate mode (actual value listed); 2 for relative mode (value stored at position (actual value listed + the relative base))
  99. function ptest(param, checkval) {
  100.   let returnVal;
  101.  
  102.   if (param == 0 || !param) {
  103.     returnVal = inputCopy[checkval];
  104.   } else if (param == 1) {
  105.     returnVal = checkval;
  106.   } else if (param == 2) {
  107.     let baseVal = relBase + checkval;
  108.     returnVal = inputCopy[baseVal];
  109.   }
  110.  
  111.   if (returnVal === undefined) {
  112.     returnVal = 0;
  113.   }
  114.  
  115.   return returnVal;
  116. }
  117.  
  118. // opcode 99 - stop program
  119.  
  120. // run through memory input, following instructions until 99 is hit
  121. function runProgram() {
  122.   for (let i = 0; i < inputCopy.length; i++) {
  123.     if (inputCopy[i] === 99) {
  124.       break;
  125.     }
  126.    
  127.     let instruct = inputCopy[i].toString();
  128.     let opval = parseInt(instruct.slice(-2), 10);
  129.     let params = instruct.slice(0, -2).split('').reverse();
  130.    
  131.  
  132.     let ione = inputCopy[i+1];
  133.     let itwo = inputCopy[i+2];
  134.     let ithree = inputCopy[i+3];
  135.  
  136.     switch (opval) {
  137.       case 01:
  138.         opcode1(ione, itwo, ithree, params);
  139.         i += 3;
  140.         break;
  141.       case 02:
  142.         opcode2(ione, itwo, ithree, params);
  143.         i += 3;
  144.         break;
  145.       case 03:
  146.         opcode3(inputval, ione, params);
  147.         i++;
  148.         break;
  149.       case 04:
  150.         let res = opcode4(ione, params);
  151.         console.log(res);
  152.         i++;
  153.         break;
  154.       case 05:
  155.         let checkt = opcode5(ione, itwo, i, params);
  156.         if (i != checkt) {
  157.           i = checkt - 1;
  158.         } else {
  159.           i += 2;
  160.         }
  161.         break;
  162.       case 06:
  163.         let checkf = opcode6(ione, itwo, i, params);
  164.         if (i != checkf) {
  165.           i = checkf - 1;
  166.         } else {
  167.           i += 2;
  168.         }
  169.         break;
  170.       case 07:
  171.         opcode7(ione, itwo, ithree, params);
  172.         i += 3;
  173.         break;
  174.       case 08:
  175.         opcode8(ione, itwo, ithree, params);
  176.         i += 3;
  177.         break;
  178.       case 09:
  179.         opcode9(ione, params);
  180.         i++;
  181.         break;
  182.     }
  183.   }
  184. }
  185.  
  186.  
  187. let inputval = 1;
  188. runProgram();
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
 
Top