Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <map>
  6. #include <iostream>
  7.  
  8. int intcode(int, int, std::vector<int>);
  9.  
  10. std::map<int, int> CodeMoves;
  11. enum Modes {POSITION = 0, IMMEDIATE=1};
  12.  
  13. struct OpCode {
  14.     int DE; // Two-digit opcode
  15.     int C; // Mode of 1st parameter
  16.     int B; // Mode of 2nd parameter
  17.     int A; // Mode of 3rd parameter
  18. };
  19.  
  20. struct Computer {
  21.  
  22.     Modes mode = Modes::POSITION;
  23.     std::vector<int> opcodes;
  24.  
  25.     Computer(std::vector<int> data){
  26.         opcodes = data;
  27.     }
  28.  
  29.     void GetDiagnosticCode(){
  30.         int i = 0;
  31.         while (i < opcodes.size()){
  32.             OpCode code = PullCode(opcodes[i]);
  33.             bool stop = false;
  34.             switch(code.DE){
  35.                 case 1: opcodes[opcodes[i+3]] = AddCode(code, opcodes[i+1], opcodes[i+2], opcodes); break;
  36.                 case 2: opcodes[opcodes[i+3]] = MultCode(code, opcodes[i+1], opcodes[i+2], opcodes); break;
  37.                 case 3: printf("System ID: "); std::cin >> opcodes[opcodes[i+1]]; break;
  38.                 case 4: WriteCode(code, opcodes[i+1], opcodes); break;
  39.                 case 5: JumpCode(code, true, opcodes[i+1], opcodes[i+2], opcodes, &i); break;
  40.                 case 6: JumpCode(code, false, opcodes[i+1], opcodes[i+2], opcodes, &i); break;
  41.                 case 7: LTCode(code, opcodes[i+1], opcodes[i+2], opcodes[i+3], opcodes); break;
  42.                 case 8: EqualCode(code, opcodes[i+1], opcodes[i+2], opcodes[i+3], opcodes); break;
  43.                 case 99: stop = true; break;
  44.             }
  45.  
  46.             if(stop)
  47.                 break;
  48.             // Move forward by the amount of parameters
  49.             i += CodeMoves.find(code.DE)->second;
  50.         }        
  51.     }
  52.  
  53.     OpCode PullCode(int value){
  54.         OpCode code;
  55.         std::string val = std::to_string(value);
  56.         // make sure value is 5 digits
  57.         if(val.size() < 5){
  58.             std::string ph = "";
  59.             ph.resize(5-val.size(), '0'); // Get a string of zeros
  60.             val = ph + val;
  61.         }
  62.         // Pull out all the values
  63.         code.DE = std::stoi(val.substr(3,4));
  64.         code.C = (val[2] - '0');
  65.         code.B = (val[1] - '0');
  66.         code.A = (val[0] - '0');
  67.  
  68.         return code;
  69.     }
  70.  
  71.     void LTCode(OpCode code, int prm1, int prm2, int prm3, std::vector<int> &opcodes){
  72.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  73.         int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
  74.        
  75.         opcodes[prm3] = (val1 < val2) ? 1 : 0;
  76.     }
  77.  
  78.     void EqualCode(OpCode code, int prm1, int prm2, int prm3, std::vector<int> &opcodes){
  79.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  80.         int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
  81.  
  82.         opcodes[prm3] = (val1 == val2) ? 1 : 0;
  83.     }
  84.  
  85.     void JumpCode(OpCode code, bool match, int prm1, int loc, std::vector<int> opcodes, int *iter){
  86.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  87.         int val2 = (code.B == Modes::POSITION) ? opcodes[loc] : loc;
  88.  
  89.         if( (val1 != 0) == match)
  90.             *iter = val2;
  91.         else
  92.             *iter += 3;
  93.     }
  94.  
  95.     int AddCode(OpCode code, int prm1, int prm2, std::vector<int> opcodes){
  96.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  97.         int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
  98.         return (val1 + val2);
  99.     }
  100.  
  101.     int MultCode(OpCode code, int prm1, int prm2, std::vector<int> opcodes){
  102.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  103.         int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
  104.         return (val1 * val2);
  105.     }
  106.  
  107.     void WriteCode(OpCode code, int prm1, std::vector<int> opcodes){
  108.         int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
  109.         printf("%d\n", val1);
  110.     }
  111.  
  112. };
  113.  
  114. int main(){
  115.  
  116.     CodeMoves.insert(std::pair<int, int>(1, 4));
  117.     CodeMoves.insert(std::pair<int, int>(2, 4));
  118.     CodeMoves.insert(std::pair<int, int>(3, 2));
  119.     CodeMoves.insert(std::pair<int, int>(4, 2));
  120.     CodeMoves.insert(std::pair<int, int>(5, 0));
  121.     CodeMoves.insert(std::pair<int, int>(6, 0));
  122.     CodeMoves.insert(std::pair<int, int>(7, 4));
  123.     CodeMoves.insert(std::pair<int, int>(8, 4));
  124.  
  125.  
  126.     std::ifstream inFile;
  127.     inFile.open("input.txt");
  128.  
  129.     std::vector<int> opcodes;
  130.  
  131.     std::string code;
  132.     while(getline(inFile, code, ',')){
  133.         int c = std::stoi(code);
  134.         opcodes.push_back(c);
  135.     }
  136.  
  137.     Computer comp(opcodes);
  138.     comp.GetDiagnosticCode();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement