Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. long long run(vector<long long> instructions, long long input)
  9. {
  10. long long output = 0;
  11.  
  12. for (long long i = 0; i + 1 < instructions.size(); )
  13. {
  14.  
  15. long long opcode = instructions[i] % 100;
  16. if (opcode == 99)
  17. {
  18. return output;
  19. }
  20. long long relativeBase = 0;
  21. long long firstParameterMode = instructions[i] / 100 % 10;
  22. long long secondParameterMode = instructions[i] / 1000 % 10;
  23. // IMPORTANT: Parameters that an instruction writes to will never be in immediate mode.
  24. // long long thirdParameterMode = instructions[i] / 10000 % 10; // WRONG
  25. long long thirdParameterMode = 0; // 0 -> position mode; 1 -> immediate mode;
  26.  
  27. long long A = firstParameterMode ? i + 1 : instructions[i + 1];
  28. long long B = secondParameterMode ? i + 2 : instructions[i + 2];
  29. long long C = thirdParameterMode ? i + 3 : instructions[i + 3];
  30.  
  31. if (firstParameterMode == 2)
  32. {
  33. A = instructions[i + 1] + relativeBase;
  34. }
  35. if (secondParameterMode == 2)
  36. {
  37. B += instructions[i + 2] + relativeBase;
  38. }
  39. if (thirdParameterMode == 2)
  40. {
  41. C += instructions[i + 3] + relativeBase;
  42. }
  43.  
  44. if (opcode == 1)
  45. {
  46.  
  47. instructions[C] = instructions[A] + instructions[B];
  48.  
  49. i += 4;
  50. }
  51. else if (opcode == 2)
  52. {
  53.  
  54. instructions[C] = instructions[A] * instructions[B];
  55.  
  56. i += 4;
  57. }
  58. else if (opcode == 3)
  59. {
  60. if (firstParameterMode == 2)
  61. {
  62. instructions[i+1+ relativeBase] = input;
  63. }
  64. else instructions[instructions[i + 1]] = input;
  65. i += 2;
  66. }
  67. else if (opcode == 4)
  68. {
  69. if (firstParameterMode == 2)
  70. {
  71. output = instructions[i+1+ relativeBase] ;
  72. }
  73. output = instructions[instructions[i + 1]];
  74. i += 2;
  75. }
  76. else if (opcode == 5)
  77. {
  78. if (instructions[A] != 0)
  79. {
  80. i = instructions[B];
  81. continue;
  82. }
  83.  
  84. i += 3;
  85. }
  86. else if (opcode == 6)
  87. {
  88. if (instructions[A] == 0)
  89. {
  90. i = instructions[B];
  91. continue;
  92. }
  93.  
  94. i += 3;
  95. }
  96. else if (opcode == 7)
  97. {
  98. if (instructions[A] < instructions[B])
  99. {
  100. instructions[C] = 1;
  101. }
  102. else
  103. {
  104. instructions[C] = 0;
  105. }
  106. i += 4;
  107. }
  108. else if (opcode == 8)
  109. {
  110. if (instructions[A] == instructions[B])
  111. {
  112. instructions[C] = 1;
  113. }
  114. else
  115. {
  116. instructions[C] = 0;
  117. }
  118. i += 4;
  119. }
  120. else if (opcode == 9)
  121. {
  122. relativeBase += instructions[A];
  123. i += 2;
  124. }
  125.  
  126. }
  127. }
  128.  
  129. int main()
  130. {
  131. freopen("in.txt", "r", stdin);
  132. freopen("out.txt", "w", stdout);
  133.  
  134. vector<long long> instructions;
  135. long long number;
  136. char delimiter;
  137.  
  138. while (cin >> number)
  139. {
  140. cin >> delimiter; // ',' character
  141. instructions.push_back(number);
  142. }
  143. instructions.push_back(99);
  144. instructions.push_back(99);
  145.  
  146. cout << "Part 1: " << run(instructions, 1) << endl;
  147. cout << "Part 2: " << run(instructions, 5) << endl;
  148.  
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement