Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <iterator>
  5.  
  6. void doOpcode(std::vector<int> &vector, int &start, int end);
  7.  
  8. void solvePart1(std::vector<int> &vector);
  9.  
  10. std::vector<int> parseOpcode(int opcode);
  11.  
  12. int getIndex(std::vector<int> &vector, int opcode, int index);
  13.  
  14. int main() {
  15.     const std::vector<int> integers = {3,225,1,225,6,6,1100,1,238,225,104,0,1101,65,73,225,1101,37,7,225,1101,42,58,225,1102,62,44,224,101,-2728,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1,69,126,224,101,-92,224,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1102,41,84,225,1001,22,92,224,101,-150,224,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1101,80,65,225,1101,32,13,224,101,-45,224,224,4,224,102,8,223,223,101,1,224,224,1,224,223,223,1101,21,18,225,1102,5,51,225,2,17,14,224,1001,224,-2701,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,101,68,95,224,101,-148,224,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1102,12,22,225,102,58,173,224,1001,224,-696,224,4,224,1002,223,8,223,1001,224,6,224,1,223,224,223,1002,121,62,224,1001,224,-1302,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,359,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,374,1001,223,1,223,108,677,677,224,102,2,223,223,1006,224,389,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,404,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,419,101,1,223,223,8,677,226,224,1002,223,2,223,1005,224,434,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,101,1,223,223,1107,226,226,224,102,2,223,223,1006,224,479,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,108,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,524,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,539,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,8,677,677,224,102,2,223,223,1005,224,569,101,1,223,223,107,226,677,224,102,2,223,223,1005,224,584,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,599,1001,223,1,223,1008,677,677,224,1002,223,2,223,1005,224,614,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,629,101,1,223,223,1108,677,226,224,1002,223,2,223,1005,224,644,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,659,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226};
  16.     std::vector<int> intCode = integers;
  17.     solvePart1(intCode);
  18.  
  19. }
  20.  
  21.  
  22.  
  23. std::vector<int> parseOpcode(int opcode) {
  24.     //1002,4,3,4
  25.     std::vector<int> opArr(4);
  26.     // parse the opcodes
  27.     int op = opcode;
  28.     int j = 3;
  29.     opArr[j--] = opcode % 100;
  30.     op /= 100;
  31.     while (op > 0) {
  32.         opArr[j--] = op % 10;
  33.         op /= 10;
  34.     }
  35.  
  36.     while(j > 0) {
  37.         opArr[j--] = 0;
  38.     }
  39.  
  40.     return opArr;
  41. }
  42.  
  43.  
  44. int getIndex(std::vector<int> &vector, int opcode, int index) {
  45.     if(opcode == 1) {
  46.         return index;
  47.     } else if (opcode == 0){
  48.         return vector[index];
  49.     }
  50. }
  51.  
  52.  
  53. void doOpcode(std::vector<int> &vector, int &start, int end) {
  54.     int add = 1;
  55.     int mult = 2;
  56.     int get_in = 3;
  57.     int show_out = 4;
  58.     int jmp_true = 5;
  59.     int jmp_false = 6;
  60.     int less = 7;
  61.     int eqls = 8;
  62.     int halt = 99;
  63.  
  64.     if (end - start == 1) {
  65.  
  66.         if (vector[start] == halt) {
  67.             std::cout << "gotta halt" << std::endl;
  68.             start = vector.size() + 100;
  69.         }
  70.  
  71.  
  72.         if (vector[start] == get_in) {
  73.             int id;
  74.             std::cout << "give id: " << std::endl;
  75.             std::cin >> id;
  76.             vector[vector[start + 1]] = id;
  77.         } else if (vector[start] == show_out) {
  78.             std::cout << "[output] out=" << vector[vector[start + 1]] << std::endl;
  79.         }
  80.     } else {
  81.         std::vector<int> opCode = parseOpcode(vector[start]);
  82.         int opcode = opCode[3];
  83.  
  84.         if (opcode == halt) {
  85.             std::cout << "gotta halt" << std::endl;
  86.             start = vector.size() + 100;
  87.         }
  88.  
  89.         if(opcode == add) {
  90.             int index1 = getIndex(vector, opCode[2], start + 1);
  91.             int index2 = getIndex(vector, opCode[1], start + 2);
  92.             int index3 = getIndex(vector, opCode[0], start + 3);
  93.             vector[index3] = vector[index1] + vector[index2];
  94.         } else if (opcode == mult) {
  95.             int index1 = getIndex(vector, opCode[2], start + 1);
  96.             int index2 = getIndex(vector, opCode[1], start + 2);
  97.             int index3 = getIndex(vector, opCode[0], start + 3);
  98.             vector[index3] = vector[index1] * vector[index2];
  99.         } else if (opcode == jmp_true) {
  100.             int index1 = getIndex(vector, opCode[2], start + 1);
  101.             int index2 = getIndex(vector, opCode[1], start + 2);
  102.             if (vector[index1] != 0) {
  103.                 start = vector[index2];
  104.             }
  105.         } else if (opcode == jmp_false) {
  106.             int index1 = getIndex(vector, opCode[2], start + 1);
  107.             int index2 = getIndex(vector, opCode[1], start + 2);
  108.             if (vector[index1] == 0) {
  109.                 start = vector[index2];
  110.             }
  111.         } else if (opcode == less) {
  112.             int index1 = getIndex(vector, opCode[2], start + 1);
  113.             int index2 = getIndex(vector, opCode[1], start + 2);
  114.             int index3 = getIndex(vector, opCode[0], start + 3);
  115.  
  116.             if (vector[index1] < vector[index2]) {
  117.                 vector[index3] = 1;
  118.             } else {
  119.                 vector[index3] = 0;
  120.             }
  121.  
  122.         } else if (opcode == eqls) {
  123.             int index1 = getIndex(vector, opCode[2], start + 1);
  124.             int index2 = getIndex(vector, opCode[1], start + 2);
  125.             int index3 = getIndex(vector, opCode[0], start + 3);
  126.  
  127.             if (vector[index1] == vector[index2]) {
  128.                 vector[index3] = 1;
  129.             } else {
  130.                 vector[index3] = 0;
  131.             }
  132.  
  133.         }
  134.  
  135.      }
  136.  
  137. }
  138.  
  139.  
  140. void solvePart1(std::vector<int> &vector) {
  141.     int i = 0;
  142.     while(i < vector.size()) {
  143.         if (vector[i] == 3 || vector[i] == 4) {
  144.             doOpcode(vector, i, i + 1);
  145.             i += 2;
  146.         } else {
  147.             doOpcode(vector, i, i + 3);
  148.             i += 4;
  149.  
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement