diff --git a/day 17/day_17_felix.cpp b/day 17/day_17_felix.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d0e49918c10feab03593add52991140a860c84b --- /dev/null +++ b/day 17/day_17_felix.cpp @@ -0,0 +1,98 @@ +#include <bits/stdc++.h> +using namespace std; +const bool DEBUG = true; + +#define ll long long + +array<ll, 3> REGISTERS = {0, 0, 0}; +vector<int> OPTCODES; +vector<int> OPERANDS; + +void read_input() { + string line; + int reg = 0; + while (getline(cin, line) && line.size() > 0) { + REGISTERS[reg] = stoi(line.substr(12)); + if (DEBUG) cout << "Register " << reg << " = " << REGISTERS[reg] << endl; + reg++; + } + getline(cin, line); + if (DEBUG) cout << "Start reading optcodes from " << line << endl; + stringstream ss(line); + char comma; + int optcode, operand; + for (int i = 0; i < 8; i++) ss >> comma; //clear start + while (ss >> operand >> comma >> optcode) { + OPTCODES.push_back(optcode); + OPERANDS.push_back(operand); + if (DEBUG) cout << " Operand " << operand << " Optcode " << optcode << endl; + ss >> comma; + } + +} + +ll part1() { + int i_point = 0; + string result = ""; + string nocommas = ""; + while (i_point < OPTCODES.size()) { + if (DEBUG) cout << "Point " << i_point << " REGISTERS: " << REGISTERS[0] << " " << REGISTERS[1] << " " << REGISTERS[2] << endl; + int instruction = OPERANDS[i_point]; + int optcode = OPTCODES[i_point]; + ll combo = optcode < 4 ? optcode : REGISTERS[optcode - 4]; + double a; + if (DEBUG) cout << " Instruction " << instruction << " Optcode " << optcode << " Combo " << combo << endl; + switch (instruction) { + case 0: // adv + a = REGISTERS[0] / pow(2, combo); + REGISTERS[0] = trunc(a); + if (DEBUG) cout << " ADV " << REGISTERS[0] << endl; + break; + case 1: // bxl + REGISTERS[1] = REGISTERS[1] ^ optcode; + if (DEBUG) cout << " BXL " << REGISTERS[1] << endl; + break; + case 2: // bst + REGISTERS[1] = combo % 8; + break; + case 3: // jnz + if (DEBUG) cout << " JNZ " << REGISTERS[0] << endl; + if (REGISTERS[0] != 0){ + i_point = optcode / 2; // divide by two as we have two things... + i_point --; // to compensate the increment + } + break; + case 4: // bxc + REGISTERS[1] = REGISTERS[1] ^ REGISTERS[2]; + if (DEBUG) cout << " BXC " << REGISTERS[1] << endl; + break; + case 5: // out + result += to_string(combo % 8); + result += ","; + nocommas += to_string(combo % 8); + if (DEBUG) cout << " OUT " << combo % 8 << endl; + break; + case 6: // bdv + a = REGISTERS[0] / pow(2, combo); + REGISTERS[1] = trunc(a); + if (DEBUG) cout << " BDV " << REGISTERS[1] << endl; + break; + case 7: // cdv + a = REGISTERS[0] / pow(2, combo); + REGISTERS[2] = trunc(a); + if (DEBUG) cout << " CDV " << REGISTERS[2] << endl; + break; + } + i_point ++; + } + cout << result << endl; + cout << nocommas << endl; + + return stoll(nocommas); +} + +int main () { + read_input(); + part1(); + return 0; +} \ No newline at end of file diff --git a/day 17/day_17_felix_part2.cpp b/day 17/day_17_felix_part2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d951c246a17f55e4bf4a43fbf64482b77a49d24 --- /dev/null +++ b/day 17/day_17_felix_part2.cpp @@ -0,0 +1,37 @@ +#include <bits/stdc++.h> +using namespace std; +const bool DEBUG = true; +#define ll long long + +vector<int> PROGRAM = {2,4,1,1,7,5,4,7,1,4,0,3,5,5,3,0}; + +ll find_previous_A(int point, ll A) { + ll Aprev; + ll B, C; + if (point < 0) return A; + string debugspacs(16-point, ' '); + for (int a = 0; a < pow(2,3); a++) { + Aprev = pow(2,3)*A + a; + //cout << " Testing Aprev " << Aprev << endl; + B = Aprev % 8; + B = B ^ 1; + C = trunc(Aprev / pow(2, B)); + B = B ^ C; + B = B ^ 4; + B = B % 8; // mod 8 before printing in the out-command + //cout << " Resulted in B " << B << endl; + if (B == PROGRAM[point]) { + cout << debugspacs << "Point " << point << " Aprev " << Aprev << " B " << B << " C " << C << endl; + ll result = find_previous_A(point - 1, Aprev); + if (result != -1) return result; + } + } + return -1; +} + +int main () { + + cout << find_previous_A(PROGRAM.size() - 1, 0) << endl; + + return 0; +} \ No newline at end of file