-
Notifications
You must be signed in to change notification settings - Fork 0
/
RubiksCubeBitboard.cpp
406 lines (312 loc) · 12.3 KB
/
RubiksCubeBitboard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// Created by Ayush on 01-08-2023.
//
#include "RubiksCube.h"
class RubiksCubeBitboard : public RubiksCube {
private:
uint64_t solved_side_config[6]{};
int arr[3][3] = {{0, 1, 2},
{7, 8, 3},
{6, 5, 4}};
uint64_t one_8 = (1 << 8) - 1, one_24 = (1 << 24) - 1;
void rotateFace(int ind) {
uint64_t side = bitboard[ind];
side = side >> (8 * 6);
bitboard[ind] = (bitboard[ind] << 16) | (side);
}
void rotateSide(int s1, int s1_1, int s1_2, int s1_3, int s2, int s2_1, int s2_2, int s2_3) {
uint64_t clr1 = (bitboard[s2] & (one_8 << (8 * s2_1))) >> (8 * s2_1);
uint64_t clr2 = (bitboard[s2] & (one_8 << (8 * s2_2))) >> (8 * s2_2);
uint64_t clr3 = (bitboard[s2] & (one_8 << (8 * s2_3))) >> (8 * s2_3);
bitboard[s1] = (bitboard[s1] & ~(one_8 << (8 * s1_1))) | (clr1 << (8 * s1_1));
bitboard[s1] = (bitboard[s1] & ~(one_8 << (8 * s1_2))) | (clr2 << (8 * s1_2));
bitboard[s1] = (bitboard[s1] & ~(one_8 << (8 * s1_3))) | (clr3 << (8 * s1_3));
}
// Helper to getCorners()
int get5bitCorner(string corner) {
int ret = 0;
string actual_str;
for (auto c: corner) {
if (c != 'W' && c != 'Y') continue;
actual_str.push_back(c);
if (c == 'Y') {
ret |= (1 << 2);
}
}
for (auto c: corner) {
if (c != 'R' && c != 'O') continue;
if (c == 'O') {
ret |= (1 << 1);
}
}
for (auto c: corner) {
if (c != 'B' && c != 'G') continue;
if (c == 'G') {
ret |= (1 << 0);
}
}
if (corner[1] == actual_str[0]) {
ret |= (1 << 3);
} else if (corner[2] == actual_str[0]) {
ret |= (1 << 4);
}
return ret;
}
// This function was used for testing / printing
// void print5bitbin(int a){
// for(int i=4; i>=0; i--){
// if(a & (1 << i)) cout << 1;
// else cout << 0;
// }
// }
public:
uint64_t bitboard[6]{};
RubiksCubeBitboard() {
for (int side = 0; side < 6; side++) {
uint64_t clr = 1 << side;
bitboard[side] = 0;
for (int faceIdx = 0; faceIdx < 8; faceIdx++) {
bitboard[side] |= clr << (8 * faceIdx);
}
solved_side_config[side] = bitboard[side];
}
}
COLOR getColor(FACE face, unsigned row, unsigned col) const override {
int idx = arr[row][col];
if (idx == 8) return (COLOR)((int) face);
uint64_t side = bitboard[(int) face];
uint64_t color = (side >> (8 * idx)) & one_8;
int bit_pos = 0;
while (color != 0) {
color = color >> 1;
bit_pos++;
}
return (COLOR)(bit_pos - 1);
}
bool isSolved() const override {
for (int i = 0; i < 6; i++) {
if (solved_side_config[i] != bitboard[i]) return false;
}
return true;
}
RubiksCube &u() override {
this->rotateFace(0);
uint64_t temp = bitboard[2] & one_24;
bitboard[2] = (bitboard[2] & ~one_24) | (bitboard[3] & one_24);
bitboard[3] = (bitboard[3] & ~one_24) | (bitboard[4] & one_24);
bitboard[4] = (bitboard[4] & ~one_24) | (bitboard[1] & one_24);
bitboard[1] = (bitboard[1] & ~one_24) | temp;
return *this;
}
RubiksCube &uPrime() override {
this->u();
this->u();
this->u();
return *this;
};
RubiksCube &u2() override {
this->u();
this->u();
return *this;
};
RubiksCube &l() override {
this->rotateFace(1);
uint64_t clr1 = (bitboard[2] & (one_8 << (8 * 0))) >> (8 * 0);
uint64_t clr2 = (bitboard[2] & (one_8 << (8 * 6))) >> (8 * 6);
uint64_t clr3 = (bitboard[2] & (one_8 << (8 * 7))) >> (8 * 7);
this->rotateSide(2, 0, 7, 6, 0, 0, 7, 6);
this->rotateSide(0, 0, 7, 6, 4, 4, 3, 2);
this->rotateSide(4, 4, 3, 2, 5, 0, 7, 6);
bitboard[5] = (bitboard[5] & ~(one_8 << (8 * 0))) | (clr1 << (8 * 0));
bitboard[5] = (bitboard[5] & ~(one_8 << (8 * 6))) | (clr2 << (8 * 6));
bitboard[5] = (bitboard[5] & ~(one_8 << (8 * 7))) | (clr3 << (8 * 7));
return *this;
};
RubiksCube &lPrime() override {
this->l();
this->l();
this->l();
return *this;
};
RubiksCube &l2() override {
this->l();
this->l();
return *this;
};
RubiksCube &f() override {
this->rotateFace(2);
uint64_t clr1 = (bitboard[0] & (one_8 << (8 * 4))) >> (8 * 4);
uint64_t clr2 = (bitboard[0] & (one_8 << (8 * 5))) >> (8 * 5);
uint64_t clr3 = (bitboard[0] & (one_8 << (8 * 6))) >> (8 * 6);
this->rotateSide(0, 4, 5, 6, 1, 2, 3, 4);
this->rotateSide(1, 2, 3, 4, 5, 0, 1, 2);
this->rotateSide(5, 0, 1, 2, 3, 6, 7, 0);
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 6))) | (clr1 << (8 * 6));
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 7))) | (clr2 << (8 * 7));
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 0))) | (clr3 << (8 * 0));
return *this;
};
RubiksCube &fPrime() override {
this->f();
this->f();
this->f();
return *this;
};
RubiksCube &f2() override {
this->f();
this->f();
return *this;
};
RubiksCube &r() override {
this->rotateFace(3);
uint64_t clr1 = (bitboard[0] & (one_8 << (8 * 2))) >> (8 * 2);
uint64_t clr2 = (bitboard[0] & (one_8 << (8 * 3))) >> (8 * 3);
uint64_t clr3 = (bitboard[0] & (one_8 << (8 * 4))) >> (8 * 4);
this->rotateSide(0, 2, 3, 4, 2, 2, 3, 4);
this->rotateSide(2, 2, 3, 4, 5, 2, 3, 4);
this->rotateSide(5, 2, 3, 4, 4, 7, 6, 0);
bitboard[4] = (bitboard[4] & ~(one_8 << (8 * 7))) | (clr1 << (8 * 7));
bitboard[4] = (bitboard[4] & ~(one_8 << (8 * 6))) | (clr2 << (8 * 6));
bitboard[4] = (bitboard[4] & ~(one_8 << (8 * 0))) | (clr3 << (8 * 0));
return *this;
};
RubiksCube &rPrime() override {
this->r();
this->r();
this->r();
return *this;
};
RubiksCube &r2() override {
this->r();
this->r();
return *this;
};
RubiksCube &b() override {
this->rotateFace(4);
uint64_t clr1 = (bitboard[0] & (one_8 << (8 * 0))) >> (8 * 0);
uint64_t clr2 = (bitboard[0] & (one_8 << (8 * 1))) >> (8 * 1);
uint64_t clr3 = (bitboard[0] & (one_8 << (8 * 2))) >> (8 * 2);
this->rotateSide(0, 0, 1, 2, 3, 2, 3, 4);
this->rotateSide(3, 2, 3, 4, 5, 4, 5, 6);
this->rotateSide(5, 4, 5, 6, 1, 6, 7, 0);
bitboard[1] = (bitboard[1] & ~(one_8 << (8 * 6))) | (clr1 << (8 * 6));
bitboard[1] = (bitboard[1] & ~(one_8 << (8 * 7))) | (clr2 << (8 * 7));
bitboard[1] = (bitboard[1] & ~(one_8 << (8 * 0))) | (clr3 << (8 * 0));
return *this;
};
RubiksCube &bPrime() override {
this->b();
this->b();
this->b();
return *this;
};
RubiksCube &b2() override {
this->b();
this->b();
return *this;
};
RubiksCube &d() override {
this->rotateFace(5);
uint64_t clr1 = (bitboard[2] & (one_8 << (8 * 4))) >> (8 * 4);
uint64_t clr2 = (bitboard[2] & (one_8 << (8 * 5))) >> (8 * 5);
uint64_t clr3 = (bitboard[2] & (one_8 << (8 * 6))) >> (8 * 6);
this->rotateSide(2, 4, 5, 6, 1, 4, 5, 6);
this->rotateSide(1, 4, 5, 6, 4, 4, 5, 6);
this->rotateSide(4, 4, 5, 6, 3, 4, 5, 6);
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 4))) | (clr1 << (8 * 4));
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 5))) | (clr2 << (8 * 5));
bitboard[3] = (bitboard[3] & ~(one_8 << (8 * 6))) | (clr3 << (8 * 6));
return *this;
};
RubiksCube &dPrime() override {
this->d();
this->d();
this->d();
return *this;
};
RubiksCube &d2() override {
this->d();
this->d();
return *this;
}
bool operator==(const RubiksCubeBitboard &r1) const {
for (int i = 0; i < 6; i++) {
if (bitboard[i] != r1.bitboard[i]) return false;
}
return true;
}
RubiksCubeBitboard &operator=(const RubiksCubeBitboard &r1) {
for (int i = 0; i < 6; i++) {
bitboard[i] = r1.bitboard[i];
}
return *this;
}
uint64_t getCorners() {
uint64_t ret = 0;
string top_front_right = "";
top_front_right += getColorLetter(getColor(FACE::UP, 2, 2));
top_front_right += getColorLetter(getColor(FACE::FRONT, 0, 2));
top_front_right += getColorLetter(getColor(FACE::RIGHT, 0, 0));
string top_front_left = "";
top_front_left += getColorLetter(getColor(FACE::UP, 2, 0));
top_front_left += getColorLetter(getColor(FACE::FRONT, 0, 0));
top_front_left += getColorLetter(getColor(FACE::LEFT, 0, 2));
string top_back_left = "";
top_back_left += getColorLetter(getColor(FACE::UP, 0, 0));
top_back_left += getColorLetter(getColor(FACE::BACK, 0, 2));
top_back_left += getColorLetter(getColor(FACE::LEFT, 0, 0));
string top_back_right = "";
top_back_right += getColorLetter(getColor(FACE::UP, 0, 2));
top_back_right += getColorLetter(getColor(FACE::BACK, 0, 0));
top_back_right += getColorLetter(getColor(FACE::RIGHT, 0, 2));
string bottom_front_right = "";
bottom_front_right += getColorLetter(getColor(FACE::DOWN, 0, 2));
bottom_front_right += getColorLetter(getColor(FACE::FRONT, 2, 2));
bottom_front_right += getColorLetter(getColor(FACE::RIGHT, 2, 0));
string bottom_front_left = "";
bottom_front_left += getColorLetter(getColor(FACE::DOWN, 0, 0));
bottom_front_left += getColorLetter(getColor(FACE::FRONT, 2, 0));
bottom_front_left += getColorLetter(getColor(FACE::LEFT, 2, 2));
string bottom_back_right = "";
bottom_back_right += getColorLetter(getColor(FACE::DOWN, 2, 2));
bottom_back_right += getColorLetter(getColor(FACE::BACK, 2, 0));
bottom_back_right += getColorLetter(getColor(FACE::RIGHT, 2, 2));
string bottom_back_left = "";
bottom_back_left += getColorLetter(getColor(FACE::DOWN, 2, 0));
bottom_back_left += getColorLetter(getColor(FACE::BACK, 2, 2));
bottom_back_left += getColorLetter(getColor(FACE::LEFT, 2, 0));
ret |= get5bitCorner(top_front_right);
ret = ret << 5;
ret |= get5bitCorner(top_front_left);
ret = ret << 5;
ret |= get5bitCorner(top_back_right);
ret = ret << 5;
ret |= get5bitCorner(top_back_left);
ret = ret << 5;
ret |= get5bitCorner(bottom_front_right);
ret = ret << 5;
ret |= get5bitCorner(bottom_front_left);
ret = ret << 5;
ret |= get5bitCorner(bottom_back_right);
ret = ret << 5;
ret |= get5bitCorner(bottom_back_left);
ret = ret << 5;
// Following was used for Testing / Printing
// cout << top_front_right << " "; print5bitbin(get5bitCorner(top_front_right )); cout << "\n";
// cout << top_front_left << " "; print5bitbin(get5bitCorner(top_front_left )); cout << "\n";
// cout << top_back_right << " "; print5bitbin(get5bitCorner(top_back_right )); cout << "\n";
// cout << top_back_left << " "; print5bitbin(get5bitCorner(top_back_left )); cout << "\n";
// cout << bottom_front_right << " "; print5bitbin(get5bitCorner(bottom_front_right )); cout << "\n";
// cout << bottom_front_left << " "; print5bitbin(get5bitCorner(bottom_front_left )); cout << "\n";
// cout << bottom_back_right << " "; print5bitbin(get5bitCorner(bottom_back_right )); cout << "\n";
// cout << bottom_back_left << " "; print5bitbin(get5bitCorner(bottom_back_left )); cout << "\n";
return ret;
}
};
struct HashBitboard {
size_t operator()(const RubiksCubeBitboard &r1) const {
uint64_t final_hash = r1.bitboard[0];
for (int i = 1; i < 6; i++) final_hash ^= r1.bitboard[i];
return (size_t) final_hash;
}
};