Estoy intentando hacer años anteriores
This commit is contained in:
2
2019/day_2/input
Normal file
2
2019/day_2/input
Normal file
@@ -0,0 +1,2 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,2,19,13,23,1,23,10,27,1,13,27,31,2,31,10,35,1,35,9,39,1,39,13,43,1,13,43,47,1,47,13,51,1,13,51,55,1,5,55,59,2,10,59,63,1,9,63,67,1,6,67,71,2,71,13,75,2,75,13,79,1,79,9,83,2,83,10,87,1,9,87,91,1,6,91,95,1,95,10,99,1,99,13,103,1,13,103,107,2,13,107,111,1,111,9,115,2,115,10,119,1,119,5,123,1,123,2,127,1,127,5,0,99,2,14,0,0
|
||||
|
||||
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
|
||||
47
2019/day_2/program_alarm.cc
Normal file
47
2019/day_2/program_alarm.cc
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include "intcode.hh"
|
||||
|
||||
std::vector<int> read_input_file() {
|
||||
std::ifstream file("input");
|
||||
std::vector<int> data;
|
||||
std::string line;
|
||||
|
||||
while(std::getline(file, line, ','))
|
||||
data.push_back(std::stoi(line));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void solve_a(std::vector<int> memory) {
|
||||
memory[1] = 12;
|
||||
memory[2] = 2;
|
||||
|
||||
auto vm = IntCode(memory);
|
||||
std::cout << vm.get(0) << std::endl;
|
||||
}
|
||||
|
||||
void solve_b(std::vector<int> memory) {
|
||||
for(int x = 0; x < 99; x++){
|
||||
for(int y = 0; y < 99; y++){
|
||||
memory[1] = x;
|
||||
memory[2] = y;
|
||||
|
||||
auto vm = IntCode(memory);
|
||||
if(vm.get(0) == 19690720)
|
||||
std::cout << x << ", " << y << " = " << (100 * x) + y << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto memory = read_input_file();
|
||||
|
||||
solve_a(memory);
|
||||
solve_b(memory);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user