Estoy intentando hacer años anteriores
This commit is contained in:
50
2019/day_2/intcode.hh
Normal file
50
2019/day_2/intcode.hh
Normal file
@@ -0,0 +1,50 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user