Commit a730994c authored by amiessen's avatar amiessen
Browse files

added day 2, part 2; optimized part 1

parent 4365d4e8
......@@ -10,6 +10,6 @@ run: main07.exe
./main07.exe
clean:
rm -v *.exe out*.txt
rm -v *.exe
.PHONY: run clean
......@@ -15,6 +15,9 @@ class intCode {
std::vector<int> code_;
std::vector<int> paraMode_;
int lastOut_;
bool stopAtOutput_;
int input_;
size_t inputCount_;
// Private Member
void getParameterMode();
......@@ -30,10 +33,15 @@ class intCode {
public:
// Ctor
intCode(std::vector<int> code, int in, size_t pos = 0) {
intCode(std::vector<int> code, int in, bool stopAtOutput = false,
size_t pos = 0)
{
code_ = code;
lastOut_ = in;
stopAtOutput_ = stopAtOutput;
pos_ = pos;
input_ = 0;
inputCount_ = 0;
}
// Getters
......@@ -42,7 +50,7 @@ class intCode {
int getOutput() { return lastOut_; }
// Public Member
void runIntCode();
bool runIntCode(const std::vector<int> &);
};
......@@ -83,14 +91,10 @@ void intCode::modify2()
void intCode::modify3()
{
int input = 0;
std::vector<int *> params(1);
setParaMode(params);
std::cin >> input;
*params[0] = input;
// std::cout << "Input: " << input << "\n";
*params[0] = input_;
}
......@@ -99,7 +103,6 @@ void intCode::modify4()
std::vector<int *> params(1);
setParaMode(params);
// std::cout << "Output: " << *params[0] << "\n";
lastOut_ = *params[0];
}
......@@ -169,13 +172,14 @@ void intCode::getParameterMode() {
}
void intCode::runIntCode() {
bool intCode::runIntCode(const std::vector<int> & in) {
// out must be passed by referenc in order to write out without exiting
// runIntCode().
// start gives starting position in int code (default = 0).
// return true IFF program was halted (opcode 99), false otherwise
// -----------------
size_t step = 4;
size_t step = 0;
while (pos_ < code_.size()) {
getParameterMode();
......@@ -198,9 +202,22 @@ void intCode::runIntCode() {
} else if (opcode == 2) {
modify2();
} else if (opcode == 3) {
inputCount_++;
if (inputCount_ <= in.size()) {
input_ = in[inputCount_-1];
} else {
std::cout << " Too few input arguments provided, type ";
std::cout << "Input here: ";
std::cin >> input_;
}
modify3();
} else if (opcode == 4) {
modify4();
if (stopAtOutput_) {
inputCount_ = 0;
pos_ += step;
return false;
}
} else if (opcode == 5) {
modify5();
} else if (opcode == 6) {
......@@ -210,16 +227,18 @@ void intCode::runIntCode() {
} else if (opcode == 8) {
modify8();
} else if (opcode == 99) {
// std::cout << " --- PROGRAM HALTED (op 99) --- \n";
break;
return true; // program came to an end
}
else {
std::cout << "ERROR: something went wrong - opcode = " << opcode;
std::cout << "(must be in (1, 2, ..., 8, 99))\n";
return false;
}
if (opcode != 5 && opcode != 6) { pos_ += step; }
}
} // WHILE pos_ < code_.size()
return false;
}
......
......@@ -24,38 +24,75 @@ void ampSeries(std::vector<int> & initCode) {
// the last amplifier.
// -------------------
// create output file for amplifier signals (int code output)
std::ofstream oFile;
oFile.open("outIntCode.txt");
oFile << 0 << "\n" << 0;
oFile.close();
std::vector<int> phase{0,1,2,3,4}; // or with std::iota
std::vector<int> phase{0,1,2,3,4};
int maxOut = 0;
std::vector<int> maxPhase;
std::ifstream inout;
do {
std::vector<int> input(2);
int out = 0;
for (size_t i = 0; i < phase.size(); i++) {
intCode IC(initCode, out);
input[0] = phase[i];
input[1] = out;
IC.runIntCode(input);
out = IC.getOutput();
}
if (out > maxOut) {
maxOut = out;
maxPhase = phase;
}
} while (std::next_permutation(phase.begin(), phase.end()));
std::cout << " -- -- -- -- -- -- -- -- -- -- -- -- -- \n";
std::cout << " --- AMPLIFIER SERIES --- \n";
std::cout << "Max output: " << maxOut << " with phase ";
for (auto & i: maxPhase)
std::cout << i;
std::cout << "\n";
}
void ampFeedbackLoop(std::vector<int> & initCode) {
std::vector<int> phase{5,6,7,8,9};
int maxOut = 0;
std::vector<int> maxPhase;
do {
std::vector<intCode> amps;
bool halted = false;
std::vector<int> input(2);
int out = 0;
for (size_t i = 0; i < phase.size(); i++) {
// std::cout << " ---- AMP " << i << " ---- \n";
intCode IC(initCode, out);
oFile.open("outIntCode.txt");
oFile << phase[i] << "\n" << IC.getOutput();
oFile.close();
// initialize amplifiers until first output
// after first run of for loop, out is output of amp 5, input for amp 1
for (size_t i = 0; i < phase.size(); i++) {
intCode IC(initCode, out, true);
input[0] = phase[i];
input[1] = out;
halted = IC.runIntCode(input);
out = IC.getOutput();
// redirect cin to file to read from that file at run time
inout.open("outIntCode.txt");
// save old buf (standard input) and redirect to inout buffer
auto cinbuf = std::cin.rdbuf(inout.rdbuf());
amps.push_back(IC); // push_back() copies, so no scope issues
}
IC.runIntCode();
out = IC.getOutput();
input.resize(1);
inout.close();
while (!halted) {
for (size_t i = 0; i < amps.size(); i++) {
input[0] = out;
halted = amps[i].runIntCode(input);
out = amps[i].getOutput();
}
}
if (out > maxOut) {
maxOut = out;
......@@ -65,6 +102,7 @@ void ampSeries(std::vector<int> & initCode) {
std::cout << " -- -- -- -- -- -- -- -- -- -- -- -- -- \n";
std::cout << " --- AMPLIFIER FEEDBACK LOOP --- \n";
std::cout << "Max output: " << maxOut << " with phase ";
for (auto & i: maxPhase)
std::cout << i;
......@@ -74,7 +112,6 @@ void ampSeries(std::vector<int> & initCode) {
int main() {
std::string data_path = "in07.txt";
std::vector<int> initCode;
......@@ -83,4 +120,6 @@ int main() {
ampSeries(initCode);
ampFeedbackLoop(initCode);
}
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