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 7b29a410 authored by amiessen's avatar amiessen
Browse files

added day 5

parent d95f4292
CXX = g++
CXXFLAGS += -std=c++14
CXXFLAGS += -Wall -Wextra -Wpedantic
main05.exe: main05.cpp
$(CXX) $(CXXFLAGS) $< -o $@
run: main05.exe
./main05.exe
clean:
rm -v *.exe
.PHONY: run clean
3,225,1,225,6,6,1100,1,238,225,104,0,1001,152,55,224,1001,224,-68,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,62,41,225,1101,83,71,225,102,59,147,224,101,-944,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,2,40,139,224,1001,224,-3905,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1101,6,94,224,101,-100,224,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1102,75,30,225,1102,70,44,224,101,-3080,224,224,4,224,1002,223,8,223,1001,224,4,224,1,223,224,223,1101,55,20,225,1102,55,16,225,1102,13,94,225,1102,16,55,225,1102,13,13,225,1,109,143,224,101,-88,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1002,136,57,224,101,-1140,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,101,76,35,224,1001,224,-138,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,389,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,404,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,419,1001,223,1,223,8,226,677,224,102,2,223,223,1005,224,434,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,464,1001,223,1,223,8,226,226,224,1002,223,2,223,1005,224,479,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,494,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,509,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,524,101,1,223,223,1007,677,226,224,102,2,223,223,1006,224,539,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,584,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,599,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,614,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,629,101,1,223,223,107,226,677,224,102,2,223,223,1006,224,644,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,659,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226
#include <iostream>
#include <fstream>
#include <string>
#include <memory>
#include <vector>
void setParaMode(std::vector<int *> & params,
std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
// 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));
} else {
params[i] = &(*(code_it + *(code_it+pos+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)
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
*params[2] = (*params[0]) + (*params[1]);
// *** WHAT EACH FUNCTION "modify-x()" LOOKED LIKE BEFORE ***
//
// int *param1, *param2, *param3;
//
// if (*p_it == 1) { param1 = &(*(code_it+pos)); }
// else { param1 = &(*(code_it + *(code_it+pos))); }
//
// if (*(p_it+1) == 1) { param2 = &(*(code_it+pos+1)); }
// else { param2 = &(*(code_it + *(code_it+pos+1))); }
//
// if (*(p_it+2) == 1) { param3 = &(*(code_it+pos+2)); }
// else { param3 = &(*(code_it + *(code_it+pos+2))); }
//
// *param3 = (*param1) + (*param2);
}
void modify2(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
*params[2] = (*params[0]) * (*params[1]);
}
void modify3(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
int input = 0;
std::vector<int *> params(1);
setParaMode(params, p_it, code_it, pos);
std::cout << "Type Input here: ";
std::cin >> input;
*params[0] = input;
}
void modify4(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
std::vector<int *> params(1);
setParaMode(params, p_it, code_it, pos);
std::cout << "Output: " << *params[0] << "\n";
}
size_t modify5(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
std::vector<int *> params(2);
setParaMode(params, p_it, code_it, pos);
if (*params[0] != 0) { return *params[1]; }
else { return pos + 2; }
}
size_t modify6(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
std::vector<int *> params(2);
setParaMode(params, p_it, code_it, pos);
if (*params[0] == 0) { return *params[1]; }
else { return pos + 2; }
}
void modify7(std::vector<int>::iterator p_it,
std::vector<int>::iterator code_it, const int & pos)
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
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)
{
std::vector<int *> params(3);
setParaMode(params, p_it, code_it, pos);
if (*params[0] == *params[1]) { *params[2] = 1; } else { *params[2] = 0; }
}
void getParameterMode(const int & x, std::vector<int> & paraMode) {
// 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
// modes of parameters in instruction. Parameter mode can be 1 (immediate
// 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);
if (xs.size() > 2) {
std::string ops = xs.substr(xs.size()-2);
paraMode.push_back(std::stoi(ops));
// 1002
for (int i = xs.size()-3; i >= 0; i--) {
paraMode.push_back(xs[i]-'0');
}
} else {
paraMode.push_back(x);
}
}
void runIntCode(std::vector<int> & code) {
size_t step = 4;
size_t i = 0;
while (i < code.size()) {
std::vector<int> paraMode;
getParameterMode(code[i], paraMode);
int opcode = paraMode[0];
if (opcode == 1 || opcode == 2 || opcode == 7 || opcode == 8) {
step = 4;
paraMode.resize(step);
} else if (opcode == 3 || opcode == 4) {
step = 2;
paraMode.resize(step);
} else if (opcode == 5 || opcode == 6) {
step = 3;
paraMode.resize(step);
}
if (opcode == 1) { // ALTERNATIVE: Expression templates
modify1(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 2) {
modify2(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 3) {
modify3(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 4) {
modify4(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 5) {
i = modify5(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 6) {
i = modify6(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 7) {
modify7(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 8) {
modify8(paraMode.begin()+1, code.begin(), i+1);
} else if (opcode == 99) {
std::cout << " --- PROGRAM HALTED (op 99) --- \n";
break;
}
else {
std::cout << "ERROR: something went wrong - opcode = " << opcode;
std::cout << "(must be in (1, 2, ..., 8, 99))\n";
}
if (opcode != 5 && opcode != 6) { i += step; }
}
}
// ############
// --- MAIN ---
// ############
int main() {
std::ifstream inFile("in05.txt");
std::vector<int> initCode;
std::string val;
if (inFile.is_open()) {
while (getline(inFile, val, ',')) {
initCode.push_back(std::stoi(val));
}
}
// std::vector<int> test{};
std::cout << "You will be prompted for an input. Type\n";
std::cout << " 1 - for air con diagnostics code\n";
std::cout << " 5 - thermal radiator controller diagnostics code\n";
std::cout << " - - - - - - - - - - - - - - \n";
runIntCode(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