diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-17 08:39:11 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-17 08:39:11 +0100 |
| commit | a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c (patch) | |
| tree | ddfc21c0d865d6f871e6ffa0722747ce62077ec0 /2024/17/day17b-bruteforce.cpp | |
| parent | 5a725f1b9e8cfbad2c31d9c220061e413adbbf8e (diff) | |
| download | aoc-a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c.tar.gz aoc-a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c.zip | |
Day 17 2024 - this was fun
Diffstat (limited to '2024/17/day17b-bruteforce.cpp')
| -rw-r--r-- | 2024/17/day17b-bruteforce.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
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 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdint> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <queue> | ||
| 6 | #include <ranges> | ||
| 7 | #include <set> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | class CPU { | ||
| 15 | public: | ||
| 16 | CPU(uint64_t a, uint64_t b, uint64_t c) : A{a}, B{b}, C{c} {} | ||
| 17 | |||
| 18 | bool process(const vector<uint64_t>& v) { | ||
| 19 | unsigned j = 0; | ||
| 20 | for (unsigned ip = 0; ip < v.size() && j <= v.size(); ) { | ||
| 21 | switch (v[ip]) { | ||
| 22 | case 0: | ||
| 23 | A >>= combo(v[ip+1]); | ||
| 24 | ip += 2; | ||
| 25 | break; | ||
| 26 | case 1: | ||
| 27 | B ^= v[ip+1]; | ||
| 28 | ip += 2; | ||
| 29 | break; | ||
| 30 | case 2: | ||
| 31 | B = combo(v[ip+1]) % 8; | ||
| 32 | ip += 2; | ||
| 33 | break; | ||
| 34 | case 3: | ||
| 35 | ip = A == 0 ? ip+2 : v[ip+1]; | ||
| 36 | break; | ||
| 37 | case 4: | ||
| 38 | B ^= C; | ||
| 39 | ip += 2; | ||
| 40 | break; | ||
| 41 | case 5: | ||
| 42 | if (j == v.size() || v[j] != combo(v[ip+1])%8) | ||
| 43 | return false; | ||
| 44 | j++; | ||
| 45 | ip += 2; | ||
| 46 | break; | ||
| 47 | case 6: | ||
| 48 | B = A >> combo(v[ip+1]); | ||
| 49 | ip += 2; | ||
| 50 | break; | ||
| 51 | case 7: | ||
| 52 | C = A >> combo(v[ip+1]); | ||
| 53 | ip += 2; | ||
| 54 | break; | ||
| 55 | default: | ||
| 56 | cout << "Error! Operator " << v[ip] << endl; | ||
| 57 | exit(1); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | return j == v.size(); | ||
| 61 | } | ||
| 62 | |||
| 63 | void setreg(uint64_t a, uint64_t b, uint64_t c) { | ||
| 64 | A = a; | ||
| 65 | B = b; | ||
| 66 | C = c; | ||
| 67 | } | ||
| 68 | private: | ||
| 69 | uint64_t A, B, C; | ||
| 70 | |||
| 71 | uint64_t& reg(uint64_t i) { | ||
| 72 | return i == 0 ? A : (i == 1 ? B : C); | ||
| 73 | } | ||
| 74 | |||
| 75 | uint64_t combo(uint64_t i) { | ||
| 76 | return i <= 3 ? i : reg(i-4); | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | int main() { | ||
| 81 | uint64_t a, b, c; | ||
| 82 | cin >> a >> b >> c; | ||
| 83 | CPU cpu(a, b, c); | ||
| 84 | |||
| 85 | vector<uint64_t> instructions; | ||
| 86 | while (cin >> a) | ||
| 87 | instructions.push_back(a); | ||
| 88 | |||
| 89 | const uint64_t M = 10000000000; | ||
| 90 | const uint64_t N = 100000000000; | ||
| 91 | for (a = M; a < N; a++) { | ||
| 92 | if (a % 1000000 == 0) | ||
| 93 | cout << "tring " << a << endl; | ||
| 94 | cpu.setreg(a, 0, 0); | ||
| 95 | if (cpu.process(instructions)) { | ||
| 96 | cout << "Found it: " << a << endl; | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | cout << "Not found for A < " << N << endl; | ||
| 101 | return 0; | ||
| 102 | } | ||
