From a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 17 Dec 2024 08:39:11 +0100 Subject: Day 17 2024 - this was fun --- 2024/17/day17b-bruteforce.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 2024/17/day17b-bruteforce.cpp (limited to '2024/17/day17b-bruteforce.cpp') diff --git a/2024/17/day17b-bruteforce.cpp b/2024/17/day17b-bruteforce.cpp new file mode 100644 index 0000000..a400d5c --- /dev/null +++ b/2024/17/day17b-bruteforce.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +class CPU { +public: + CPU(uint64_t a, uint64_t b, uint64_t c) : A{a}, B{b}, C{c} {} + + bool process(const vector& v) { + unsigned j = 0; + for (unsigned ip = 0; ip < v.size() && j <= v.size(); ) { + switch (v[ip]) { + case 0: + A >>= combo(v[ip+1]); + ip += 2; + break; + case 1: + B ^= v[ip+1]; + ip += 2; + break; + case 2: + B = combo(v[ip+1]) % 8; + ip += 2; + break; + case 3: + ip = A == 0 ? ip+2 : v[ip+1]; + break; + case 4: + B ^= C; + ip += 2; + break; + case 5: + if (j == v.size() || v[j] != combo(v[ip+1])%8) + return false; + j++; + ip += 2; + break; + case 6: + B = A >> combo(v[ip+1]); + ip += 2; + break; + case 7: + C = A >> combo(v[ip+1]); + ip += 2; + break; + default: + cout << "Error! Operator " << v[ip] << endl; + exit(1); + } + } + return j == v.size(); + } + + void setreg(uint64_t a, uint64_t b, uint64_t c) { + A = a; + B = b; + C = c; + } +private: + uint64_t A, B, C; + + uint64_t& reg(uint64_t i) { + return i == 0 ? A : (i == 1 ? B : C); + } + + uint64_t combo(uint64_t i) { + return i <= 3 ? i : reg(i-4); + } +}; + +int main() { + uint64_t a, b, c; + cin >> a >> b >> c; + CPU cpu(a, b, c); + + vector instructions; + while (cin >> a) + instructions.push_back(a); + + const uint64_t M = 10000000000; + const uint64_t N = 100000000000; + for (a = M; a < N; a++) { +if (a % 1000000 == 0) +cout << "tring " << a << endl; + cpu.setreg(a, 0, 0); + if (cpu.process(instructions)) { + cout << "Found it: " << a << endl; + return 0; + } + } + cout << "Not found for A < " << N << endl; + return 0; +} -- cgit v1.3