Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. #include "../utils.h"
  2. #include <algorithm>
  3. int lastoutput = -37;
  4. int eoutput = -37;
  5. vector<int> intComp(int prevOut, int amp, bool firstrun, vector<int> intcodes, int pos)
  6. {
  7. // getchar();
  8. int x = pos;
  9. int inputam = false;
  10. while (x < intcodes.size())
  11. {
  12. // getchar();
  13. cout << "RAW: " << intcodes.at(x) << " | ";
  14. string comp = to_string(intcodes.at(x));
  15. while (comp.size() < 5)
  16. {
  17. comp = '0' + comp;
  18. }
  19. bool par1im = comp.at(2) == '1';
  20. bool par2im = comp.at(1) == '1';
  21. bool par3im = comp.at(0) == '1';
  22. cout << "OPCODE: " << comp << " ";
  23. cout << "par1 : "
  24. << "imediate: " << par1im;
  25. cout << "| par2 : "
  26. << "imediate: " << par2im;
  27. cout << "| par3 : "
  28. << "imediate: " << par3im;
  29. cout << endl;
  30. switch (stoi(comp.substr(3)))
  31. {
  32. case 99:
  33. {
  34. cout << "STOPING" << endl;
  35. // x = intcodes.size();
  36. vector<int> l;
  37. l.push_back(x);
  38. l.push_back(99);
  39. // getchar();
  40. return l;
  41. break;
  42. }
  43. case 1:
  44. {
  45. cout << "add ";
  46. cout << endl;
  47. int operand1;
  48. if (par1im)
  49. {
  50. operand1 = intcodes.at(x + 1);
  51. }
  52. else
  53. {
  54. operand1 = intcodes.at(intcodes.at(x + 1));
  55. }
  56. int operand2;
  57. if (par2im)
  58. {
  59. operand2 = intcodes.at(x + 2);
  60. }
  61. else
  62. {
  63. operand2 = intcodes.at(intcodes.at(x + 2));
  64. }
  65. intcodes.at(intcodes.at(x + 3)) = operand1 + operand2;
  66. cout << "BECOMES : " << intcodes.at(intcodes.at(x + 3)) << endl;
  67. x += 4;
  68. break;
  69. }
  70. case 2:
  71. {
  72. cout << "mult ";
  73.  
  74. int operand1;
  75. if (par1im)
  76. {
  77. operand1 = intcodes.at(x + 1);
  78. }
  79. else
  80. {
  81. operand1 = intcodes.at(intcodes.at(x + 1));
  82. }
  83. int operand2;
  84. if (par2im)
  85. {
  86. operand2 = intcodes.at(x + 2);
  87. }
  88. else
  89. {
  90. operand2 = intcodes.at(intcodes.at(x + 2));
  91. }
  92. intcodes.at(intcodes.at(x + 3)) = operand1 * operand2;
  93. cout << "BECOMES : " << intcodes.at(intcodes.at(x + 3)) << endl;
  94. x += 4;
  95. break;
  96. }
  97. case 3:
  98. cout << "inputting " << to_string(amp) << " to " << intcodes.at(x + 1) << endl;
  99. if (firstrun)
  100. {
  101. if (!inputam)
  102. {
  103. intcodes.at(intcodes.at(x + 1)) = amp;
  104. inputam = true;
  105. }
  106. else
  107. {
  108. }
  109. }
  110. else
  111. {
  112. intcodes.at(intcodes.at(x + 1)) = prevOut;
  113. }
  114.  
  115. /**
  116. * Opcode 3 takes a single integer as input and saves it to the position
  117. * given by its only parameter. For example, the instruction 3,50 would
  118. * take an input value and store it at address 50.
  119. * */
  120. x += 2;
  121. break;
  122. case 4:
  123. {
  124. cout << "OUTPUT : ";
  125. cout << to_string((par1im ? intcodes.at(x + 1) : intcodes.at(intcodes.at(x + 1)))) << endl;
  126. eoutput = (par1im ? intcodes.at(x + 1) : intcodes.at(intcodes.at(x + 1)));
  127. x += 2;
  128. vector<int> l;
  129. l.push_back(x);
  130. l.push_back(eoutput);
  131. // getchar();
  132. return l;
  133. /**
  134. * Opcode 4 outputs the value of its only parameter.
  135. * For example, the instruction 4,50 would output the value at address 50.
  136. * */
  137. break;
  138. }
  139. case 5:
  140. {
  141. cout << "jump if true ";
  142.  
  143. int operand1;
  144. if (par1im)
  145. {
  146. operand1 = intcodes.at(x + 1);
  147. }
  148. else
  149. {
  150. operand1 = intcodes.at(intcodes.at(x + 1));
  151. }
  152. cout << operand1;
  153. int operand2;
  154. if (par2im)
  155. {
  156. operand2 = intcodes.at(x + 2);
  157. }
  158. else
  159. {
  160. operand2 = intcodes.at(intcodes.at(x + 2));
  161. }
  162. if (operand1 != 0)
  163. {
  164. cout << " jumped to " << operand2 << endl;
  165. x = operand2;
  166. }
  167. else
  168. {
  169. x += 3;
  170. }
  171. break;
  172. }
  173. case 6:
  174. {
  175. cout << "jump if false";
  176.  
  177. int operand1;
  178. if (par1im)
  179. {
  180. operand1 = intcodes.at(x + 1);
  181. }
  182. else
  183. {
  184. operand1 = intcodes.at(intcodes.at(x + 1));
  185. }
  186. cout << " " << operand1;
  187. int operand2;
  188. if (par2im)
  189. {
  190. operand2 = intcodes.at(x + 2);
  191. }
  192. else
  193. {
  194. operand2 = intcodes.at(intcodes.at(x + 2));
  195. }
  196. if (operand1 == 0)
  197. {
  198. x = operand2;
  199. cout << " jumped" << endl;
  200. }
  201. else
  202. {
  203. x += 3;
  204. }
  205. break;
  206. }
  207. case 7:
  208. {
  209. cout << "less than";
  210.  
  211. int operand1;
  212. if (par1im)
  213. {
  214. operand1 = intcodes.at(x + 1);
  215. }
  216. else
  217. {
  218. operand1 = intcodes.at(intcodes.at(x + 1));
  219. }
  220. int operand2;
  221. if (par2im)
  222. {
  223. operand2 = intcodes.at(x + 2);
  224. }
  225. else
  226. {
  227. operand2 = intcodes.at(intcodes.at(x + 2));
  228. }
  229. cout << " " << operand1 << " | " << operand2;
  230. if (operand1 < operand2)
  231. {
  232. intcodes.at(intcodes.at(x + 3)) = 1;
  233. cout << " :yes " << endl;
  234. }
  235. else
  236. {
  237. intcodes.at(intcodes.at(x + 3)) = 0;
  238. cout << " :no " << endl;
  239. }
  240. x += 4;
  241. break;
  242. }
  243. case 8:
  244. {
  245. cout << "equals";
  246.  
  247. int operand1;
  248. if (par1im)
  249. {
  250. operand1 = intcodes.at(x + 1);
  251. }
  252. else
  253. {
  254. operand1 = intcodes.at(intcodes.at(x + 1));
  255. }
  256. int operand2;
  257. if (par2im)
  258. {
  259. operand2 = intcodes.at(x + 2);
  260. }
  261. else
  262. {
  263. operand2 = intcodes.at(intcodes.at(x + 2));
  264. }
  265. cout << " " << operand1 << " | " << operand2;
  266. if (operand1 == operand2)
  267. {
  268. intcodes.at(intcodes.at(x + 3)) = 1;
  269. cout << " :yes" << endl;
  270. }
  271. else
  272. {
  273. intcodes.at(intcodes.at(x + 3)) = 0;
  274. cout << " :no" << endl;
  275. }
  276. x += 4;
  277. break;
  278. }
  279. cout << "ERROR FOUND " << endl;
  280. default:
  281. exit(1);
  282. }
  283. cout << endl;
  284. }
  285. }
  286. bool isUnique(int a, int b, int c, int d, int e)
  287. {
  288. vector<int> wow;
  289. wow.push_back(a);
  290. if (find(wow.begin(), wow.end(), b) != wow.end())
  291. {
  292. return false;
  293. }
  294. wow.push_back(b);
  295. if (find(wow.begin(), wow.end(), c) != wow.end())
  296. {
  297. return false;
  298. }
  299. wow.push_back(c);
  300. if (find(wow.begin(), wow.end(), d) != wow.end())
  301. {
  302. return false;
  303. }
  304. wow.push_back(d);
  305. if (find(wow.begin(), wow.end(), e) != wow.end())
  306. {
  307. return false;
  308. }
  309. return true;
  310. }
  311. vector<int> parseInput(vector<string> input)
  312. {
  313. vector<int> intcodes;
  314. string a = "";
  315. for (int x = 0; x < input.size(); x++)
  316. {
  317. for (int y = 0; y < input.at(x).size(); y++)
  318. {
  319.  
  320. if (input.at(x).at(y) == ' ' || input.at(x).at(y) == '\n')
  321. {
  322. continue;
  323. }
  324. if (input.at(x).at(y) == ',')
  325. {
  326. if (a != "")
  327. {
  328. intcodes.push_back(stoi(a));
  329. cout << a << endl;
  330. a = "";
  331. }
  332. }
  333. else
  334. {
  335. a += input.at(x).at(y);
  336. }
  337. }
  338. }
  339. if (a != "")
  340. {
  341. intcodes.push_back(stoi(a));
  342. cout << a << endl;
  343. }
  344. return intcodes;
  345. }
  346. int main()
  347. {
  348. vector<string> input;
  349. Utils utils;
  350. input = utils.loadFile("inputtest.txt");
  351. vector<int> intcodes;
  352. intcodes = parseInput(input);
  353. cout << "parsed input" << endl;
  354. vector<int> old = intcodes;
  355. int output = 0;
  356. for (int a = 5; a < 10; a++)
  357. {
  358. for (int b = 5; b < 10; b++)
  359. {
  360. for (int c = 5; c < 10; c++)
  361. {
  362. for (int d = 5; d < 10; d++)
  363. {
  364. for (int e = 5; e < 10; e++)
  365. {
  366. intcodes = old;
  367. vector<int> aint = old;
  368. int apos = 0;
  369. vector<int> bint = old;
  370. int bpos = 0;
  371. vector<int> cint = old;
  372. int cpos = 0;
  373. vector<int> dint = old;
  374. int dpos = 0;
  375. vector<int> eint = old;
  376. int epos = 0;
  377. vector<int> temp;
  378. if (!isUnique(a, b, c, d, e))
  379. {
  380. continue;
  381. }
  382. if (a != 9 && b != 7 && c != 8 && d != 5 && e != 5)
  383. {
  384. continue;
  385. }
  386. cout << a << " " << b << " " << c << " " << d << " " << e << endl;
  387. bool stop = false;
  388. bool firstrun = true;
  389. while (!stop)
  390. {
  391. int aout;
  392. int eout;
  393. if (firstrun)
  394. {
  395. eout = 0;
  396. }
  397.  
  398. temp = intComp(eout, a, firstrun, aint, epos);
  399. aout = temp.at(1);
  400. apos = temp.at(0);
  401.  
  402. temp = intComp(aout, b, firstrun, bint, bpos);
  403. int bout = temp.at(1);
  404. bpos = temp.at(0);
  405.  
  406. temp = intComp(bout, c, firstrun, cint, cpos);
  407. int ceout = temp.at(1);
  408. cpos = temp.at(0);
  409.  
  410. temp = intComp(ceout, d, firstrun, dint, dpos);
  411. int dout = temp.at(1);
  412. dpos = temp.at(0);
  413.  
  414. temp = intComp(dout, e, firstrun, eint, epos);
  415. eout = temp.at(1);
  416. epos = temp.at(0);
  417. if (eout == 99)
  418. {
  419. stop = true;
  420. break;
  421. }
  422. firstrun = false;
  423. }
  424. cout << eoutput << endl;
  425. getchar();
  426. }
  427. }
  428. }
  429. }
  430. }
  431. // intcodes.at(1) = 0;
  432. // intcodes.at(2) = 1;
  433. cout << endl
  434. << output << endl;
  435. return 0;
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement