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
|
STATIC bool cube_true(cube_t);
STATIC cube_t cubefromarray(uint8_t [SIZE(8)], uint8_t [SIZE(12)]);
STATIC bool isconsistent(oriented_cube_t);
STATIC bool issolvable(oriented_cube_t);
STATIC bool issolved(oriented_cube_t);
STATIC bool iserror(oriented_cube_t);
STATIC void getcube_fix(long long *, long long *,
long long *, long long *, long long *);
STATIC cube_t getcube(uint64_t, uint64_t, uint64_t, uint64_t);
STATIC oriented_cube_t readcube(const char *);
STATIC int64_t writecube(oriented_cube_t, size_t n, char *);
STATIC uint8_t readco(const char *);
STATIC uint8_t readcp(const char *);
STATIC uint8_t readeo(const char *);
STATIC uint8_t readep(const char *);
STATIC uint8_t b32toedge(char);
STATIC uint8_t b32tocorner(char);
STATIC char edgetob32(uint8_t);
STATIC char cornertob32(uint8_t);
/* Used e.g. by the CORNERS coordinate, when a function pointer is needed */
STATIC bool
cube_true(cube_t cube)
{
return true;
}
STATIC cube_t
cubefromarray(uint8_t c[SIZE(8)], uint8_t e[SIZE(12)])
{
return STATIC_CUBE(
c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7],
e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7],
e[8], e[9], e[10], e[11]);
}
STATIC bool
isconsistent(oriented_cube_t cube)
{
uint8_t i, p, e, piece, corner[8], edge[12];
bool found[12];
pieces(&cube.cube, corner, edge);
for (i = 0; i < 12; i++)
found[i] = false;
for (i = 0; i < 12; i++) {
piece = edge[i];
p = piece & PBITS;
e = piece & EOBIT;
if (p >= 12)
goto inconsistent_ep;
if (e != 0 && e != EOBIT)
goto inconsistent_eo;
found[p] = true;
}
for (i = 0; i < 12; i++)
if (!found[i])
goto inconsistent_ep;
for (i = 0; i < 8; i++)
found[i] = false;
for (i = 0; i < 8; i++) {
piece = corner[i];
p = piece & PBITS;
e = piece & COBITS;
if (p >= 8)
goto inconsistent_cp;
if (e != 0 && e != CTWIST_CW && e != CTWIST_CCW)
goto inconsistent_co;
found[p] = true;
}
for (i = 0; i < 8; i++)
if (!found[i])
goto inconsistent_cp;
if (cube.orientation >= 24)
goto inconsistent_orientation;
return true;
inconsistent_ep:
inconsistent_cp:
inconsistent_eo:
inconsistent_co:
inconsistent_orientation:
/* We used to do more logging here, hence the different labels */
return false;
}
STATIC bool
issolvable(oriented_cube_t cube)
{
uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8];
DBG_ASSERT(isconsistent(cube), "issolvable: cube is inconsistent\n");
pieces(&cube.cube, corner, edge);
for (i = 0; i < 12; i++)
ep[i] = edge[i] & PBITS;
for (i = 0; i < 8; i++)
cp[i] = corner[i] & PBITS;
if (permsign(12, ep) != permsign(8, cp))
goto issolvable_parity;
eo = 0;
for (i = 0; i < 12; i++) {
piece = edge[i];
eo += (piece & EOBIT) >> EOSHIFT;
}
if (eo % 2 != 0)
goto issolvable_eo;
co = 0;
for (i = 0; i < 8; i++) {
piece = corner[i];
co += (piece & COBITS) >> COSHIFT;
}
if (co % 3 != 0)
goto issolvable_co;
return true;
issolvable_parity:
LOG("There is parity\n");
return false;
issolvable_eo:
LOG("EO is not solvable\n");
return false;
issolvable_co:
LOG("CO is not solvable\n");
return false;
}
bool
issolved(oriented_cube_t cube)
{
return equal(cube.cube, SOLVED_CUBE);
}
bool
iserror(oriented_cube_t cube)
{
return equal(cube.cube, ZERO_CUBE);
}
STATIC void
getcube_fix(
long long *ep,
long long *eo,
long long *cp,
long long *co,
long long *orien
)
{
uint8_t e[12], c[8], coarr[8];
*ep = POSITIVE_MOD(*ep, (long long)FACT_12);
*eo = POSITIVE_MOD(*eo, (long long)POW_2_11);
*cp = POSITIVE_MOD(*cp, (long long)FACT_8);
*co = POSITIVE_MOD(*co, (long long)POW_3_7);
*orien = POSITIVE_MOD(*orien, 24LL);
indextoperm((uint64_t)*ep, 12, e);
indextoperm((uint64_t)*cp, 8, c);
if (permsign(12, e) != permsign(8, c)) {
SWAP(c[0], c[1]);
*cp = (long long)permtoindex(8, c);
sumzerotodigits((uint64_t)*co, 8, 3, coarr);
SWAP(coarr[0], coarr[1]);
*co = (uint64_t)digitstosumzero(8, coarr, 3);
}
}
STATIC cube_t
getcube(uint64_t ep, uint64_t eo, uint64_t cp, uint64_t co)
{
uint8_t i, earr[12], carr[8], eoarr[12], coarr[8];
sumzerotodigits(eo, 12, 2, eoarr);
DBG_ASSERT(eoarr[0] != UINT8_ERROR, "Error making EO");
indextoperm(ep, 12, earr);
DBG_ASSERT(earr[0] != UINT8_ERROR, "Error making EP");
for (i = 0; i < 12; i++)
earr[i] |= eoarr[i] << EOSHIFT;
sumzerotodigits(co, 8, 3, coarr);
DBG_ASSERT(coarr[0] != UINT8_ERROR, "Error making CO");
indextoperm(cp, 8, carr);
DBG_ASSERT(carr[0] != UINT8_ERROR, "Error making CP");
for (i = 0; i < 8; i++)
carr[i] |= coarr[i] << COSHIFT;
return cubefromarray(carr, earr);
}
STATIC uint8_t
readco(const char *str)
{
if (*str == '0')
return 0;
if (*str == '1')
return CTWIST_CW;
if (*str == '2')
return CTWIST_CCW;
LOG("Error reading CO\n");
return UINT8_ERROR;
}
STATIC uint8_t
readcp(const char *str)
{
uint8_t c;
for (c = 0; c < 8; c++)
if (!strncmp(str, cornerstr[c], 3) ||
!strncmp(str, cornerstralt[c], 3))
return c;
LOG("Error reading CP\n");
return UINT8_ERROR;
}
STATIC uint8_t
readeo(const char *str)
{
if (*str == '0')
return 0;
if (*str == '1')
return EFLIP;
LOG("Error reading EO\n");
return UINT8_ERROR;
}
STATIC uint8_t
readep(const char *str)
{
uint8_t e;
for (e = 0; e < 12; e++)
if (!strncmp(str, edgestr[e], 2))
return e;
LOG("Error reading EP\n");
return UINT8_ERROR;
}
STATIC oriented_cube_t
readcube(const char *buf)
{
int i;
uint8_t c[8], e[12], orientation;
for (i = 0; i < 8; i++) {
c[i] = b32tocorner(buf[i]);
if (c[i] == UINT8_ERROR) {
LOG("Error reading corner %d ", i);
if (buf[i] == 0) {
LOG("(string terminated early)\n");
} else {
LOG("(char '%c')\n", buf[i]);
}
return ZERO_ORIENTED_CUBE;
}
}
if (buf[8] != '=') {
LOG("Error reading separator: a single '=' "
"must be used to separate edges and corners\n");
return ZERO_ORIENTED_CUBE;
}
for (i = 0; i < 12; i++) {
e[i] = b32toedge(buf[i+9]);
if (e[i] == UINT8_ERROR) {
LOG("Error reading edge %d ", i);
if (buf[i+9] == 0) {
LOG("(string terminated early)\n");
} else {
LOG("(char '%c')\n", buf[i+9]);
}
return ZERO_ORIENTED_CUBE;
}
}
orientation = (uint8_t)(buf[22] - 'A');
if (orientation >= 24) {
LOG("Error reading orientation: impossible value %" PRIu8
" (%c)\n", orientation, buf[22]);
return ZERO_ORIENTED_CUBE;
}
return (oriented_cube_t) {
.cube = cubefromarray(c, e),
.orientation = orientation
};
}
STATIC int64_t
writecube(oriented_cube_t cube, size_t buf_size, char *buf)
{
int i;
uint8_t corner[8], edge[12];
if (buf_size < NISSY_SIZE_CUBE) {
LOG("Cannot write cube: buffer size must be at least %u "
"bytes, but the provided one is %zu bytes.\n",
NISSY_SIZE_CUBE, buf_size);
return NISSY_ERROR_BUFFER_SIZE;
}
pieces(&cube.cube, corner, edge);
for (i = 0; i < 8; i++)
buf[i] = cornertob32(corner[i]);
buf[8] = '=';
for (i = 0; i < 12; i++)
buf[i+9] = edgetob32(edge[i]);
buf[21] = '=';
buf[22] = (char)cube.orientation + 'A';
buf[23] = '\0';
return NISSY_OK;
}
STATIC uint8_t
b32toedge(char c)
{
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f')))
return UINT8_ERROR;
return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26;
}
STATIC uint8_t
b32tocorner(char c) {
uint8_t val;
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f')))
return UINT8_ERROR;
val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26;
return (val & 7) | ((val & 24) << 2);
}
STATIC char
edgetob32(uint8_t edge)
{
return edge < 26 ? 'A' + (char)edge : 'a' + (char)(edge - 26);
}
STATIC char
cornertob32(uint8_t corner)
{
uint8_t val;
val = (corner & 7) | ((corner & 96) >> 2);
return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26);
}
|