Advertisement
Wolfrost

Test Advent of Code interpreter

Dec 5th, 2019
1,105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <unordered_map>
  5. #include <functional>
  6. #include <algorithm>
  7.  
  8. // Utilty functions
  9.  
  10. std::vector<std::string> split(const std::string& s, char delimiter)
  11. {
  12.     std::vector<std::string> tokens;
  13.     std::string token;
  14.     std::istringstream ts(s);
  15.     while (std::getline(ts, token, delimiter)) tokens.push_back(token);
  16.     return tokens;
  17. }
  18. std::string join(const std::vector<std::string>& v, const std::string& delimiter) {
  19.     std::string s;
  20.     for (auto i = v.begin(); i != v.end(); ++i) s += (*i) + (i == v.end() - 1 ? "" : delimiter);
  21.     return s;
  22. }
  23.  
  24. // Actual code
  25.  
  26. std::vector<std::string> interpret_code(std::vector<std::string> tokens) {
  27.     // Instruction pointer
  28.     int ip = 0;
  29.  
  30.     // Opcodes
  31.     std::unordered_map<std::string, std::function<void(const std::string&, const std::vector<std::string>&)>> opcodes {
  32.         {"ADD", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
  33.             int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
  34.             int arg2 = std::stoi(modes[1] == '1' ? params[1] : tokens[std::stoi(params[1])]);
  35.             int outp = std::stoi(params[2]);
  36.  
  37.             std::cout << ">>>> ADD | arg1 = " << arg1 << " (with mode " << modes[0] << ")" << (modes[0] != '1' ? " [" + params[0] + "]" : "") << ", arg2 = " << arg2 << " (with mode " << modes[1] << ")" << (modes[1] != '1' ? " [" + params[1] + "]" : "") << ", output position = " << outp << ", output value = " << arg1 + arg2 << std::endl;
  38.             tokens[outp] = std::to_string(arg1 + arg2);
  39.             ip += 4;
  40.         }},
  41.         {"MUL", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
  42.             int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
  43.             int arg2 = std::stoi(modes[1] == '1' ? params[1] : tokens[std::stoi(params[1])]);
  44.             int outp = std::stoi(params[2]);
  45.  
  46.             std::cout << ">>>> MUL | arg1 = " << arg1 << " (with mode " << modes[0] << ")" << (modes[0] != '1' ? " [" + params[0] + "]" : "") << ", arg2 = " << arg2 << " (with mode " << modes[1] << ")" << (modes[1] != '1' ? " [" + params[1] + "]" : "") << ", output position = " << outp << ", output value = " << arg1 * arg2 << std::endl;
  47.             tokens[outp] = std::to_string(arg1 * arg2);
  48.             ip += 4;
  49.         }},
  50.         {"INP", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
  51.             int outp = std::stoi(params[0]);
  52.  
  53.             std::cout << ">>>> INP | Output position = " << outp << std::endl;
  54.             std::cin >> tokens[outp];
  55.             ip += 2;
  56.         }},
  57.         {"OUT", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
  58.             int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
  59.  
  60.             std::cout << ">>>> OUT | arg1 = " << arg1 << " (with mode " << modes[0] << ")" << (modes[0] != '1' ? " [" + params[0] + "]" : "") << std::endl;
  61.             std::cout << arg1 << std::endl;
  62.             ip += 2;
  63.         }},
  64.     };
  65.  
  66.     try {
  67.         for (; ip < tokens.size();) {
  68.             // Parse instruction
  69.             std::string instruction = tokens[ip];
  70.             std::string opcode = instruction.length() > 2 ? instruction.substr(instruction.length() - 2) : instruction;
  71.             std::string parameter_modes = instruction.length() > 2 ? instruction.substr(0, instruction.length() - 2) : "";
  72.             std::reverse(parameter_modes.begin(), parameter_modes.end());
  73.  
  74.             std::cout << ">>>> OPCODE: " << opcode << " | PARAM MODES: " << parameter_modes << std::endl;
  75.             int token = std::stoi(opcode.length() == 2 ? opcode.substr(1) : opcode);
  76.             switch (token) {
  77.                 case 1: opcodes["ADD"](parameter_modes, { tokens[ip + 1], tokens[ip + 2], tokens[ip + 3] }); break;
  78.                 case 2: opcodes["MUL"](parameter_modes, { tokens[ip + 1], tokens[ip + 2], tokens[ip + 3] }); break;
  79.                 case 3: opcodes["INP"](parameter_modes, { tokens[ip + 1] }); break;
  80.                 case 4: opcodes["OUT"](parameter_modes, { tokens[ip + 1] }); break;
  81.                 case 9: return tokens;
  82.             }
  83.  
  84.             std::cout << std::endl << join(tokens, ",") << std::endl << std::endl;
  85.         }
  86.  
  87.         std::cout << "Didn't find a 99 opcode. Returning from input end..." << std::endl;
  88.         return tokens;
  89.     } catch (const std::exception& e) {
  90.         std::cerr << "ERROR! Failed to parse code: " << e.what() << std::endl;
  91.         return {};
  92.     }
  93. }
  94.  
  95. int main() {
  96.     std::vector<std::string> input = split("3,225,1,225,6,6,1100,1,238,225,104,0,2,218,57,224,101,-3828,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1102,26,25,224,1001,224,-650,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1102,44,37,225,1102,51,26,225,1102,70,94,225,1002,188,7,224,1001,224,-70,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,1101,86,70,225,1101,80,25,224,101,-105,224,224,4,224,102,8,223,223,101,1,224,224,1,224,223,223,101,6,91,224,1001,224,-92,224,4,224,102,8,223,223,101,6,224,224,1,224,223,223,1102,61,60,225,1001,139,81,224,101,-142,224,224,4,224,102,8,223,223,101,1,224,224,1,223,224,223,102,40,65,224,1001,224,-2800,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,1102,72,10,225,1101,71,21,225,1,62,192,224,1001,224,-47,224,4,224,1002,223,8,223,101,7,224,224,1,224,223,223,1101,76,87,225,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,108,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1107,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,7,226,677,224,1002,223,2,223,1005,224,359,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,374,101,1,223,223,108,677,677,224,102,2,223,223,1006,224,389,1001,223,1,223,107,677,226,224,102,2,223,223,1006,224,404,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,419,1001,223,1,223,1107,677,677,224,1002,223,2,223,1006,224,434,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,449,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,464,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,479,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,1008,226,677,224,1002,223,2,223,1005,224,509,1001,223,1,223,1007,677,226,224,102,2,223,223,1005,224,524,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,539,101,1,223,223,1108,226,226,224,1002,223,2,223,1006,224,554,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,569,1001,223,1,223,7,226,226,224,102,2,223,223,1005,224,584,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,599,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,614,1001,223,1,223,108,226,226,224,1002,223,2,223,1006,224,629,101,1,223,223,107,677,677,224,102,2,223,223,1005,224,644,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,659,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,674,1001,223,1,223,4,223,99,226", ',');
  97.     interpret_code(input);
  98.     return 0;
  99. }
Advertisement
RAW Paste Data Copied
Advertisement