Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <sstream>
- #include <unordered_map>
- #include <functional>
- #include <algorithm>
- // Utilty functions
- std::vector<std::string> split(const std::string& s, char delimiter)
- {
- std::vector<std::string> tokens;
- std::string token;
- std::istringstream ts(s);
- while (std::getline(ts, token, delimiter)) tokens.push_back(token);
- return tokens;
- }
- std::string join(const std::vector<std::string>& v, const std::string& delimiter) {
- std::string s;
- for (auto i = v.begin(); i != v.end(); ++i) s += (*i) + (i == v.end() - 1 ? "" : delimiter);
- return s;
- }
- // Actual code
- std::vector<std::string> interpret_code(std::vector<std::string> tokens) {
- // Instruction pointer
- int ip = 0;
- // Opcodes
- std::unordered_map<std::string, std::function<void(const std::string&, const std::vector<std::string>&)>> opcodes {
- {"ADD", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
- int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
- int arg2 = std::stoi(modes[1] == '1' ? params[1] : tokens[std::stoi(params[1])]);
- int outp = std::stoi(params[2]);
- 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;
- tokens[outp] = std::to_string(arg1 + arg2);
- ip += 4;
- }},
- {"MUL", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
- int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
- int arg2 = std::stoi(modes[1] == '1' ? params[1] : tokens[std::stoi(params[1])]);
- int outp = std::stoi(params[2]);
- 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;
- tokens[outp] = std::to_string(arg1 * arg2);
- ip += 4;
- }},
- {"INP", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
- int outp = std::stoi(params[0]);
- std::cout << ">>>> INP | Output position = " << outp << std::endl;
- std::cin >> tokens[outp];
- ip += 2;
- }},
- {"OUT", [&tokens, &ip](const std::string& modes, const std::vector<std::string>& params) -> void {
- int arg1 = std::stoi(modes[0] == '1' ? params[0] : tokens[std::stoi(params[0])]);
- std::cout << ">>>> OUT | arg1 = " << arg1 << " (with mode " << modes[0] << ")" << (modes[0] != '1' ? " [" + params[0] + "]" : "") << std::endl;
- std::cout << arg1 << std::endl;
- ip += 2;
- }},
- };
- try {
- for (; ip < tokens.size();) {
- // Parse instruction
- std::string instruction = tokens[ip];
- std::string opcode = instruction.length() > 2 ? instruction.substr(instruction.length() - 2) : instruction;
- std::string parameter_modes = instruction.length() > 2 ? instruction.substr(0, instruction.length() - 2) : "";
- std::reverse(parameter_modes.begin(), parameter_modes.end());
- std::cout << ">>>> OPCODE: " << opcode << " | PARAM MODES: " << parameter_modes << std::endl;
- int token = std::stoi(opcode.length() == 2 ? opcode.substr(1) : opcode);
- switch (token) {
- case 1: opcodes["ADD"](parameter_modes, { tokens[ip + 1], tokens[ip + 2], tokens[ip + 3] }); break;
- case 2: opcodes["MUL"](parameter_modes, { tokens[ip + 1], tokens[ip + 2], tokens[ip + 3] }); break;
- case 3: opcodes["INP"](parameter_modes, { tokens[ip + 1] }); break;
- case 4: opcodes["OUT"](parameter_modes, { tokens[ip + 1] }); break;
- case 9: return tokens;
- }
- std::cout << std::endl << join(tokens, ",") << std::endl << std::endl;
- }
- std::cout << "Didn't find a 99 opcode. Returning from input end..." << std::endl;
- return tokens;
- } catch (const std::exception& e) {
- std::cerr << "ERROR! Failed to parse code: " << e.what() << std::endl;
- return {};
- }
- }
- int main() {
- 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", ',');
- interpret_code(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement