-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathdoit.cpp
124 lines (113 loc) · 2.58 KB
/
doit.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <cassert>
#include <algorithm>
#define N 8
#define LEN 4
//#define X 0
//#define Y 20
int X, Y;
enum type {
ADD, ROL, XOR,
};
struct operation {
type t;
unsigned char arg;
};
struct four {
operation ops[LEN];
};
std::vector<four> generate_sequences(int n) {
if (n == 0) {
return {{}};
}
auto prev = generate_sequences(n - 1);
int cnt = 0;
for (auto& seq: prev) {
//type tx[] = {ADD, ROL, XOR, ADD};
//type t = tx[n-1];
for (auto t: {ADD, ROL, XOR}) {
if (n > 1 && seq.ops[n-2].t == t) continue;
if (n == 1 && t != ADD) continue;
if (n == 2 && t != ROL) continue;
if (n == 3 && t != XOR) continue;
if (n == 4 && t != ADD) continue;
for (int arg = 0; arg < (1<<N); arg++) {
if (t == ROL && arg == N) break;
if (t == ROL && arg == 0) continue;
cnt++;
}
}
}
std::vector<four> ret;
ret.reserve(cnt);
for (auto& seq: prev) {
//type tx[] = {ADD, ROL, XOR, ADD};
//type t = tx[n-1];
for (auto t: {ADD, ROL, XOR}) {
if (n > 1 && seq.ops[n-2].t == t) continue;
if (n == 1 && t != ADD) continue;
if (n == 2 && t != ROL) continue;
if (n == 3 && t != XOR) continue;
if (n == 4 && t != ADD) continue;
for (int arg = 0; arg < (1<<N); arg++) {
if (t == ROL && arg == N) break;
if (t == ROL && arg == 0) continue;
ret.push_back(seq);
ret.back().ops[n-1] = {t, (unsigned char)arg};
}
}
}
return ret;
}
void print_seq(const four& ops) {
printf("[");
for (auto op: ops.ops) {
//printf("%s %d\n", (op.t == ADD ? "ADD" : op.t == ROL ? "ROL" : "XOR"), op.arg);
printf("(%d, %d), ", op.t, op.arg);
}
printf("],\n");
}
int apply(int n, const four& ops) {
for (auto op: ops.ops) {
if (op.t == ADD) { n += op.arg; }
if (op.t == ROL) { n = (n << op.arg) | (n >> (N - op.arg)); }
if (op.t == XOR) { n ^= op.arg; }
n &= (1<<N) - 1;
}
return n;
}
bool correct(const four& ops) {
if (apply(0, ops) != Y || apply(Y, ops) != 0) return false;
for (int i = 1; i < (1<<N); i++) {
if (i == Y) continue;
if (apply(i, ops) != i) return false;
}
return true;
}
bool correct2(const four& ops) {
return apply(X, ops) == 0 && apply(Y, ops) == 1;
}
int main() {
auto seqs = generate_sequences(LEN);
std::random_shuffle(seqs.begin(), seqs.end());
printf("tab = {\n");
for (X = 0; X < (1<<N); X++) {
for (Y = X+1; Y < (1<<N); Y++) {
printf(" (%d, %d): ", X, Y);
bool any = false;
//printf(" y=%d\n", Y);
for (const auto& s: seqs) {
if (correct2(s)) {
print_seq(s);
any = true;
break;
}
}
assert(any);
}
}
printf("}\n");
}