Estoy intentando hacer años anteriores

This commit is contained in:
Daniel Cortés
2020-12-03 00:13:20 -03:00
parent f8a17e3fa0
commit 42edc83f90
5 changed files with 239 additions and 0 deletions

50
2019/day_2/intcode.hh Normal file
View 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