Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. let relativeBase = 0;
  2. let instrus = [];
  3.  
  4. const intcode = (program, inputs, index) => {
  5. const s = program.split`,`.map(n => parseInt(n));
  6. let stop = false;
  7. instrus = [];
  8. while (index < s.length && !stop) {
  9. const instructions = s.slice(index, index + 4);
  10. const opcode = instructions[0] % 100;
  11. const modes = instructions[0].toString().padStart(5, "0");
  12.  
  13. const getParameter = position => {
  14. const positionMode = modes[3 - position];
  15. if (positionMode == "0") {
  16. // console.log(i, position, modes, positionMode, instructions, instructions[position], s[instructions[position]]);
  17. return s[instructions[position]];
  18. } else if (positionMode == "1") {
  19. // console.log(i, position, modes, positionMode, instructions, instructions[position]);
  20. return instructions[position];
  21. } else {
  22. return s[relativeBase + instructions[position]];
  23. }
  24. };
  25.  
  26. const getPosition = position => {
  27. const positionMode = modes[3 - position];
  28. if (positionMode === "0") {
  29. return instructions[position];
  30. } else if (positionMode == "1") {
  31. console.log("ok");
  32. } else {
  33. return relativeBase + instructions[position];
  34. }
  35. };
  36. // console.log(i, opcode, instructions);
  37. switch (opcode) {
  38. case 1:
  39. s[getPosition(3)] = getParameter(1) + getParameter(2);
  40. // console.log('opcode 1', instructions[3], s[instructions[3]]);
  41. index += 4;
  42. break;
  43. case 2:
  44. s[getPosition(3)] = getParameter(1) * getParameter(2);
  45. // console.log('opcode 2', instructions[3], s[instructions[3]]);
  46. index += 4;
  47. break;
  48. case 3:
  49. s[getPosition(1)] = inputs.shift();
  50. // console.log('opcode 3', instructions[1], s[instructions[1]]);
  51. index += 2;
  52. break;
  53. case 4:
  54. output = getParameter(1);
  55. instrus.push(output);
  56. index += 2;
  57.  
  58. if (instrus.length === 2) {
  59. return [s.join(","), index, false];
  60. }
  61.  
  62. // console.log('opcode 4');
  63. break;
  64. case 5:
  65. if (getParameter(1) !== 0) {
  66. index = getParameter(2);
  67. } else {
  68. index += 3;
  69. }
  70. break;
  71. case 6:
  72. if (getParameter(1) === 0) {
  73. index = getParameter(2);
  74. } else {
  75. index += 3;
  76. }
  77. break;
  78. case 7:
  79. if (getParameter(1) < getParameter(2)) {
  80. s[getPosition(3)] = 1;
  81. } else {
  82. s[getPosition(3)] = 0;
  83. }
  84. index += 4;
  85. break;
  86. case 8:
  87. if (getParameter(1) === getParameter(2)) {
  88. s[getPosition(3)] = 1;
  89. } else {
  90. s[getPosition(3)] = 0;
  91. }
  92. index += 4;
  93. break;
  94. case 9:
  95. param = getParameter(1);
  96. relativeBase += param;
  97. index += 2;
  98. break;
  99. case 99:
  100. stop = true;
  101. return [s.join(","), index, true];
  102. break;
  103. default:
  104. console.log(
  105. "FAILURE WRONG OPCODE AT POSITION",
  106. index,
  107. ":",
  108. instructions[0]
  109. );
  110. stop = true;
  111. break;
  112. }
  113. // console.log(s.slice(0,50).join`,`);
  114. }
  115. return s[0];
  116. };
  117.  
  118. let program =
  119. "3,8,1005,8,339,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,29,2,1108,11,10,1,1,20,10,2,107,6,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,62,1006,0,29,1006,0,12,1,1101,5,10,1,2,20,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,99,1006,0,30,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,124,1006,0,60,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,149,2,1007,2,10,1,1105,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,178,1,1108,15,10,1,1101,5,10,1,109,8,10,1006,0,20,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,215,1006,0,61,1006,0,16,2,1105,15,10,1006,0,50,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,250,1,1003,10,10,1,9,19,10,2,1004,6,10,2,1106,2,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,289,1,1103,13,10,2,105,17,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,318,101,1,9,9,1007,9,1086,10,1005,10,15,99,109,661,104,0,104,1,21101,0,825599304340,1,21101,356,0,0,1106,0,460,21101,0,937108545948,1,21102,1,367,0,1106,0,460,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,21628980315,1,21101,0,414,0,1105,1,460,21101,0,3316673539,1,21101,425,0,0,1106,0,460,3,10,104,0,104,0,3,10,104,0,104,0,21102,988753428840,1,1,21102,1,448,0,1106,0,460,21102,825544569700,1,1,21102,459,1,0,1106,0,460,99,109,2,21202,-1,1,1,21102,1,40,2,21102,491,1,3,21102,481,1,0,1105,1,524,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,486,487,502,4,0,1001,486,1,486,108,4,486,10,1006,10,518,1101,0,0,486,109,-2,2105,1,0,0,109,4,2102,1,-1,523,1207,-3,0,10,1006,10,541,21102,0,1,-3,21201,-3,0,1,22102,1,-2,2,21102,1,1,3,21102,560,1,0,1106,0,565,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,588,2207,-4,-2,10,1006,10,588,22101,0,-4,-4,1105,1,656,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,607,0,1106,0,565,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,626,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,648,21202,-1,1,1,21101,0,648,0,105,1,523,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0";
  120.  
  121. let visited = [];
  122. let white = [];
  123. let black = [];
  124. let index = 0;
  125. let halt = false;
  126.  
  127. let direction = 0; // 0 = up, 1 = left, 2 = down, 3 = right
  128. let y = 0;
  129. let x = 0;
  130. let count = 0;
  131.  
  132. while (!halt) {
  133. if (whiteCheck([x, y])) {
  134. [program, index, halt] = intcode(program, [1], index);
  135. } else {
  136. [program, index, halt] = intcode(program, [0], index);
  137. }
  138. console.log(instrus + " : " + index + " => " + x + ", " + y);
  139.  
  140. if (instrus[1] == 0) {
  141. direction = (direction + 1) % 4;
  142. } else if (instrus[1] == 1) {
  143. if (direction == 0) {
  144. direction += 4;
  145. }
  146. direction = direction - 1;
  147. } else {
  148. break;
  149. }
  150. paint([x, y], instrus[0]);
  151.  
  152. switch (Math.abs(direction)) {
  153. case 0:
  154. y++;
  155. break;
  156. case 1:
  157. x--;
  158. break;
  159. case 2:
  160. y--;
  161. break;
  162. case 3:
  163. x++;
  164. break;
  165. default:
  166. console.log("error");
  167. }
  168. }
  169. console.log(visited.length);
  170. console.log(count);
  171.  
  172. function paint(coord, color) {
  173. count++;
  174. if (!visitedcheck(coord)) {
  175. visited.push(coord);
  176. }
  177. if (color == 0) {
  178. if (!blackCheck(coord)) {
  179. black.push(coord);
  180. }
  181. if (whiteCheck(coord)) {
  182. white.splice(white.indexOf(coord), 1);
  183. }
  184. } else if (color == 1) {
  185. if (!whiteCheck(coord)) {
  186. white.push(coord);
  187. }
  188. if (blackCheck(coord)) {
  189. black.splice(black.indexOf(coord), 1);
  190. }
  191. } else {
  192. console.log("errorr");
  193. }
  194. }
  195.  
  196. function whiteCheck(blockCheck) {
  197. var bol = false;
  198. white.forEach(element => {
  199. if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
  200. bol = true;
  201. }
  202. });
  203. return bol;
  204. }
  205.  
  206. function blackCheck(blockCheck) {
  207. var bol = false;
  208. black.forEach(element => {
  209. if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
  210. bol = true;
  211. }
  212. });
  213. return bol;
  214. }
  215.  
  216. function visitedcheck(blockCheck) {
  217. var bol = false;
  218. visited.forEach(element => {
  219. if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
  220. bol = true;
  221. }
  222. });
  223. return bol;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement