Skip to content
Snippets Groups Projects
Commit 69435179 authored by Felix Agner's avatar Felix Agner
Browse files

day 17

parent da2659dc
No related branches found
No related tags found
No related merge requests found
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment