aboutsummaryrefslogtreecommitdiff
path: root/2024/17/day17b-bruteforce.cpp
blob: a400d5c529500e2238793696ae298c3b41a752d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <queue>
#include <ranges>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
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<uint64_t>& 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<uint64_t> 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;
}

Generated with cgit - Back to sebastiano.tronto.net