51 lines
851 B
C++
51 lines
851 B
C++
#ifndef INTCODE_H
|
|
#define INTCODE_H
|
|
|
|
#include<vector>
|
|
#include<iostream>
|
|
|
|
class IntCode {
|
|
std::vector<int> memory;
|
|
bool halt = false;
|
|
int ip = 0; //instruction pointer
|
|
|
|
void op_add() {
|
|
int param_1 = memory[memory[ip+1]];
|
|
int param_2 = memory[memory[ip+2]];
|
|
|
|
memory[memory[ip+3]] = param_1 + param_2;
|
|
ip += 4;
|
|
}
|
|
|
|
void op_mult() {
|
|
int param_1 = memory[memory[ip+1]];
|
|
int param_2 = memory[memory[ip+2]];
|
|
|
|
memory[memory[ip+3]] = param_1 * param_2;
|
|
ip += 4;
|
|
}
|
|
|
|
void run() {
|
|
while(!halt){
|
|
switch(memory[ip]) {
|
|
case 1: op_add(); break;
|
|
case 2: op_mult(); break;
|
|
case 99: halt = true; break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public:
|
|
IntCode(std::vector<int> memory) {
|
|
this->memory = memory;
|
|
run();
|
|
}
|
|
|
|
|
|
int get(int address) {
|
|
return memory[address];
|
|
}
|
|
};
|
|
|
|
#endif
|