Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <map>
- #include <iostream>
- int intcode(int, int, std::vector<int>);
- std::map<int, int> CodeMoves;
- enum Modes {POSITION = 0, IMMEDIATE=1};
- struct OpCode {
- int DE; // Two-digit opcode
- int C; // Mode of 1st parameter
- int B; // Mode of 2nd parameter
- int A; // Mode of 3rd parameter
- };
- struct Computer {
- Modes mode = Modes::POSITION;
- std::vector<int> opcodes;
- Computer(std::vector<int> data){
- opcodes = data;
- }
- void GetDiagnosticCode(){
- int i = 0;
- while (i < opcodes.size()){
- OpCode code = PullCode(opcodes[i]);
- bool stop = false;
- switch(code.DE){
- case 1: opcodes[opcodes[i+3]] = AddCode(code, opcodes[i+1], opcodes[i+2], opcodes); break;
- case 2: opcodes[opcodes[i+3]] = MultCode(code, opcodes[i+1], opcodes[i+2], opcodes); break;
- case 3: printf("System ID: "); std::cin >> opcodes[opcodes[i+1]]; break;
- case 4: WriteCode(code, opcodes[i+1], opcodes); break;
- case 5: JumpCode(code, true, opcodes[i+1], opcodes[i+2], opcodes, &i); break;
- case 6: JumpCode(code, false, opcodes[i+1], opcodes[i+2], opcodes, &i); break;
- case 7: LTCode(code, opcodes[i+1], opcodes[i+2], opcodes[i+3], opcodes); break;
- case 8: EqualCode(code, opcodes[i+1], opcodes[i+2], opcodes[i+3], opcodes); break;
- case 99: stop = true; break;
- }
- if(stop)
- break;
- // Move forward by the amount of parameters
- i += CodeMoves.find(code.DE)->second;
- }
- }
- OpCode PullCode(int value){
- OpCode code;
- std::string val = std::to_string(value);
- // make sure value is 5 digits
- if(val.size() < 5){
- std::string ph = "";
- ph.resize(5-val.size(), '0'); // Get a string of zeros
- val = ph + val;
- }
- // Pull out all the values
- code.DE = std::stoi(val.substr(3,4));
- code.C = (val[2] - '0');
- code.B = (val[1] - '0');
- code.A = (val[0] - '0');
- return code;
- }
- void LTCode(OpCode code, int prm1, int prm2, int prm3, std::vector<int> &opcodes){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
- opcodes[prm3] = (val1 < val2) ? 1 : 0;
- }
- void EqualCode(OpCode code, int prm1, int prm2, int prm3, std::vector<int> &opcodes){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
- opcodes[prm3] = (val1 == val2) ? 1 : 0;
- }
- void JumpCode(OpCode code, bool match, int prm1, int loc, std::vector<int> opcodes, int *iter){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- int val2 = (code.B == Modes::POSITION) ? opcodes[loc] : loc;
- if( (val1 != 0) == match)
- *iter = val2;
- else
- *iter += 3;
- }
- int AddCode(OpCode code, int prm1, int prm2, std::vector<int> opcodes){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
- return (val1 + val2);
- }
- int MultCode(OpCode code, int prm1, int prm2, std::vector<int> opcodes){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- int val2 = (code.B == Modes::POSITION) ? opcodes[prm2] : prm2;
- return (val1 * val2);
- }
- void WriteCode(OpCode code, int prm1, std::vector<int> opcodes){
- int val1 = (code.C == Modes::POSITION) ? opcodes[prm1] : prm1;
- printf("%d\n", val1);
- }
- };
- int main(){
- CodeMoves.insert(std::pair<int, int>(1, 4));
- CodeMoves.insert(std::pair<int, int>(2, 4));
- CodeMoves.insert(std::pair<int, int>(3, 2));
- CodeMoves.insert(std::pair<int, int>(4, 2));
- CodeMoves.insert(std::pair<int, int>(5, 0));
- CodeMoves.insert(std::pair<int, int>(6, 0));
- CodeMoves.insert(std::pair<int, int>(7, 4));
- CodeMoves.insert(std::pair<int, int>(8, 4));
- std::ifstream inFile;
- inFile.open("input.txt");
- std::vector<int> opcodes;
- std::string code;
- while(getline(inFile, code, ',')){
- int c = std::stoi(code);
- opcodes.push_back(c);
- }
- Computer comp(opcodes);
- comp.GetDiagnosticCode();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement