To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 4365d4e8 authored by amiessen's avatar amiessen
Browse files

structured intCode.hpp into class and cleaned up

parent 15784af1
......@@ -8,50 +8,84 @@
#include <vector>
void setParaMode(std::vector<int *> & params,
std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
class intCode {
private:
size_t pos_; // where in int code am I
std::vector<int> code_;
std::vector<int> paraMode_;
int lastOut_;
// Private Member
void getParameterMode();
void setParaMode(std::vector<int *> &);
void modify1();
void modify2();
void modify3();
void modify4();
void modify5();
void modify6();
void modify7();
void modify8();
public:
// Ctor
intCode(std::vector<int> code, int in, size_t pos = 0) {
code_ = code;
lastOut_ = in;
pos_ = pos;
}
// Getters
size_t getPosition() { return pos_; }
std::vector<int> getCode() { return code_; }
int getOutput() { return lastOut_; }
// Public Member
void runIntCode();
};
void intCode::setParaMode(std::vector<int *> & params)
{
// set pointers to integers depending on parameter mode (1 or 0, position
// or immediate, respectively)
for (size_t i = 0; i < params.size(); i++) {
if (*(p_it+i) == 1) {
params[i] = &(*(code_it+pos+i));
if (*(paraMode_.begin()+1+i) == 1) {
params[i] = &(*(code_.begin()+pos_+1+i));
} else {
params[i] = &(*(code_it + *(code_it+pos+i)));
params[i] = &(*(code_.begin() + *(code_.begin()+pos_+1+i)));
}
}
}
// make template parameter for opcode (policies / expression templates)
void modify1(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify1()
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
*params[2] = (*params[0]) + (*params[1]);
}
void modify2(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify2()
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
*params[2] = (*params[0]) * (*params[1]);
}
void modify3(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify3()
{
int input = 0;
std::vector<int *> params(1);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
std::cin >> input;
*params[0] = input;
......@@ -60,62 +94,56 @@ void modify3(std::vector<int>::iterator p_it,
}
int modify4(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify4()
{
std::vector<int *> params(1);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
// std::cout << "Output: " << *params[0] << "\n";
return *params[0];
lastOut_ = *params[0];
}
size_t modify5(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify5()
{
std::vector<int *> params(2);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
if (*params[0] != 0) { return *params[1]; }
else { return pos + 2; }
if (*params[0] != 0) { pos_ = *params[1]; }
else { pos_ += 3; }
}
size_t modify6(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify6()
{
std::vector<int *> params(2);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
if (*params[0] == 0) { return *params[1]; }
else { return pos + 2; }
if (*params[0] == 0) { pos_ = *params[1]; }
else { pos_ += 3; }
}
void modify7(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify7()
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
if (*params[0] < *params[1]) { *params[2] = 1; } else { *params[2] = 0; }
}
void modify8(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
void intCode::modify8()
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
setParaMode(params);
if (*params[0] == *params[1]) { *params[2] = 1; } else { *params[2] = 0; }
}
void getParameterMode(const int & x, std::vector<int> & paraMode) {
void intCode::getParameterMode() {
// from first integer in instruction (of form ABCDE):
// - last two digits (DE) give opcode
// - other digits (ABC - read from right to left, so CBA) give parameter
......@@ -123,65 +151,66 @@ void getParameterMode(const int & x, std::vector<int> & paraMode) {
// mode, meaning the parameter is taken by value), or 0 (position mode,
// meaning the parameter gives the position/index of the value)
// If no parameter modes are provided, they are set to default - 0
// -------------------------
std::string xs = std::to_string(x);
paraMode_.resize(0);
std::string xs = std::to_string(code_[pos_]);
if (xs.size() > 2) {
std::string ops = xs.substr(xs.size()-2);
paraMode.push_back(std::stoi(ops));
// 1002
paraMode_.push_back(std::stoi(ops));
for (int i = xs.size()-3; i >= 0; i--) {
paraMode.push_back(xs[i]-'0');
paraMode_.push_back(xs[i]-'0');
}
} else {
paraMode.push_back(x);
paraMode_.push_back(code_[pos_]);
}
}
bool runIntCode(std::vector<int> code, int & out, const int start = 0) {
void intCode::runIntCode() {
// out must be passed by referenc in order to write out without exiting
// runIntCode().
// start gives starting position in int code (default = 0).
// -----------------
size_t step = 4;
size_t i = start; // start iterating through intCode
while (i < code.size()) {
std::vector<int> paraMode;
getParameterMode(code[i], paraMode);
int opcode = paraMode[0];
while (pos_ < code_.size()) {
getParameterMode();
int opcode = paraMode_[0];
if (opcode == 1 || opcode == 2 || opcode == 7 || opcode == 8) {
step = 4;
paraMode.resize(step);
paraMode_.resize(step);
} else if (opcode == 3 || opcode == 4) {
step = 2;
paraMode.resize(step);
paraMode_.resize(step);
} else if (opcode == 5 || opcode == 6) {
step = 3;
paraMode.resize(step);
paraMode_.resize(step);
}
if (opcode == 1) { // ALTERNATIVE: Expression templates
modify1(paraMode.begin()+1, code.begin(), i+1);
modify1();
} else if (opcode == 2) {
modify2(paraMode.begin()+1, code.begin(), i+1);
modify2();
} else if (opcode == 3) {
modify3(paraMode.begin()+1, code.begin(), i+1);
modify3();
} else if (opcode == 4) {
out = modify4(paraMode.begin()+1, code.begin(), i+1);
modify4();
} else if (opcode == 5) {
i = modify5(paraMode.begin()+1, code.begin(), i+1);
modify5();
} else if (opcode == 6) {
i = modify6(paraMode.begin()+1, code.begin(), i+1);
modify6();
} else if (opcode == 7) {
modify7(paraMode.begin()+1, code.begin(), i+1);
modify7();
} else if (opcode == 8) {
modify8(paraMode.begin()+1, code.begin(), i+1);
modify8();
} else if (opcode == 99) {
std::cout << " --- PROGRAM HALTED (op 99) --- \n";
// std::cout << " --- PROGRAM HALTED (op 99) --- \n";
break;
}
else {
......@@ -189,7 +218,7 @@ bool runIntCode(std::vector<int> code, int & out, const int start = 0) {
std::cout << "(must be in (1, 2, ..., 8, 99))\n";
}
if (opcode != 5 && opcode != 6) { i += step; }
if (opcode != 5 && opcode != 6) { pos_ += step; }
}
}
......
......@@ -38,11 +38,12 @@ void ampSeries(std::vector<int> & initCode) {
do {
int out = 0;
for (size_t i = 0; i < phase.size(); i++) {
std::cout << " ---- AMP " << i << " ---- \n";
// std::cout << " ---- AMP " << i << " ---- \n";
intCode IC(initCode, out);
oFile.open("outIntCode.txt");
oFile << phase[i] << "\n" << out;
oFile << phase[i] << "\n" << IC.getOutput();
oFile.close();
// redirect cin to file to read from that file at run time
......@@ -50,7 +51,8 @@ void ampSeries(std::vector<int> & initCode) {
// save old buf (standard input) and redirect to inout buffer
auto cinbuf = std::cin.rdbuf(inout.rdbuf());
runIntCode(initCode, out);
IC.runIntCode();
out = IC.getOutput();
inout.close();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment