2024 day 17

This commit is contained in:
eriedaberrie 2024-12-16 23:48:43 -08:00
parent 9ee15ee9cf
commit a844e05d24
2 changed files with 126 additions and 0 deletions

80
2024/17/main-1.cpp Normal file
View file

@ -0,0 +1,80 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int ra, rb, rc;
cin.ignore(12);
cin >> ra;
cin.ignore(13);
cin >> rb;
cin.ignore(13);
cin >> rc;
cin.ignore(11);
vector<int> tape;
int n;
while (cin >> n) {
tape.push_back(n);
cin.ignore();
}
vector<int> out;
int p = 0;
while (p < tape.size() - 1) {
int opc = tape[p], opr = tape[p + 1], combo;
switch (opr) {
case 4:
combo = ra;
break;
case 5:
combo = rb;
break;
case 6:
combo = rc;
break;
default:
combo = opr;
}
switch (opc) {
case 0:
ra >>= combo;
break;
case 1:
rb ^= opr;
break;
case 2:
rb = combo & 7;
break;
case 3:
if (ra != 0) {
p = opr;
continue;
}
break;
case 4:
rb ^= rc;
break;
case 5:
out.push_back(combo & 7);
break;
case 6:
rb = ra >> combo;
break;
case 7:
rc = ra >> combo;
break;
}
p += 2;
}
if (out.size() > 0) {
cout << out[0];
for (int i = 1; i < out.size(); i++) {
cout << ',' << out[i];
}
}
cout << '\n';
}

46
2024/17/main-2.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
vector<int> tape;
// HACK: hardcoded input
ll rec(int i, ll a) {
for (ll aa = a; aa < a + 8; aa++) {
ll b = aa & 7;
b ^= 1;
ll c = aa >> b;
b ^= c ^ 4;
if ((b & 7) == tape[i]) {
if (i == 0) {
return aa;
} else {
ll n = rec(i - 1, aa << 3);
if (n != -1) {
return n;
}
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin.ignore(12);
cin >> n;
cin.ignore(13);
cin >> n;
cin.ignore(13);
cin >> n;
cin.ignore(11);
while (cin >> n) {
tape.push_back(n);
cin.ignore();
}
cout << rec(tape.size() - 1, 0) << '\n';
}