diff options
100 files changed, 3755 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80de3c7 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| test/*/runtest | |||
| @@ -0,0 +1,636 @@ | |||
| 1 | The following license applies to every C source code and header file | ||
| 2 | distributed with this LICENSE file. | ||
| 3 | |||
| 4 | h48 - a prototype for an optimal rubik's cube solver | ||
| 5 | Copyright (C) 2023 Sebastiano Tronto <sebastiano@tronto.net> | ||
| 6 | |||
| 7 | This program is free software: you can redistribute it and/or modify | ||
| 8 | it under the terms of the GNU General Public License as published by | ||
| 9 | the Free Software Foundation, either version 3 of the License, or | ||
| 10 | (at your option) any later version. | ||
| 11 | |||
| 12 | This program is distributed in the hope that it will be useful, | ||
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | GNU General Public License for more details. | ||
| 16 | |||
| 17 | |||
| 18 | GNU GENERAL PUBLIC LICENSE | ||
| 19 | Version 3, 29 June 2007 | ||
| 20 | |||
| 21 | Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> | ||
| 22 | Everyone is permitted to copy and distribute verbatim copies | ||
| 23 | of this license document, but changing it is not allowed. | ||
| 24 | |||
| 25 | Preamble | ||
| 26 | |||
| 27 | The GNU General Public License is a free, copyleft license for | ||
| 28 | software and other kinds of works. | ||
| 29 | |||
| 30 | The licenses for most software and other practical works are designed | ||
| 31 | to take away your freedom to share and change the works. By contrast, | ||
| 32 | the GNU General Public License is intended to guarantee your freedom to | ||
| 33 | share and change all versions of a program--to make sure it remains free | ||
| 34 | software for all its users. We, the Free Software Foundation, use the | ||
| 35 | GNU General Public License for most of our software; it applies also to | ||
| 36 | any other work released this way by its authors. You can apply it to | ||
| 37 | your programs, too. | ||
| 38 | |||
| 39 | When we speak of free software, we are referring to freedom, not | ||
| 40 | price. Our General Public Licenses are designed to make sure that you | ||
| 41 | have the freedom to distribute copies of free software (and charge for | ||
| 42 | them if you wish), that you receive source code or can get it if you | ||
| 43 | want it, that you can change the software or use pieces of it in new | ||
| 44 | free programs, and that you know you can do these things. | ||
| 45 | |||
| 46 | To protect your rights, we need to prevent others from denying you | ||
| 47 | these rights or asking you to surrender the rights. Therefore, you have | ||
| 48 | certain responsibilities if you distribute copies of the software, or if | ||
| 49 | you modify it: responsibilities to respect the freedom of others. | ||
| 50 | |||
| 51 | For example, if you distribute copies of such a program, whether | ||
| 52 | gratis or for a fee, you must pass on to the recipients the same | ||
| 53 | freedoms that you received. You must make sure that they, too, receive | ||
| 54 | or can get the source code. And you must show them these terms so they | ||
| 55 | know their rights. | ||
| 56 | |||
| 57 | Developers that use the GNU GPL protect your rights with two steps: | ||
| 58 | (1) assert copyright on the software, and (2) offer you this License | ||
| 59 | giving you legal permission to copy, distribute and/or modify it. | ||
| 60 | |||
| 61 | For the developers' and authors' protection, the GPL clearly explains | ||
| 62 | that there is no warranty for this free software. For both users' and | ||
| 63 | authors' sake, the GPL requires that modified versions be marked as | ||
| 64 | changed, so that their problems will not be attributed erroneously to | ||
| 65 | authors of previous versions. | ||
| 66 | |||
| 67 | Some devices are designed to deny users access to install or run | ||
| 68 | modified versions of the software inside them, although the manufacturer | ||
| 69 | can do so. This is fundamentally incompatible with the aim of | ||
| 70 | protecting users' freedom to change the software. The systematic | ||
| 71 | pattern of such abuse occurs in the area of products for individuals to | ||
| 72 | use, which is precisely where it is most unacceptable. Therefore, we | ||
| 73 | have designed this version of the GPL to prohibit the practice for those | ||
| 74 | products. If such problems arise substantially in other domains, we | ||
| 75 | stand ready to extend this provision to those domains in future versions | ||
| 76 | of the GPL, as needed to protect the freedom of users. | ||
| 77 | |||
| 78 | Finally, every program is threatened constantly by software patents. | ||
| 79 | States should not allow patents to restrict development and use of | ||
| 80 | software on general-purpose computers, but in those that do, we wish to | ||
| 81 | avoid the special danger that patents applied to a free program could | ||
| 82 | make it effectively proprietary. To prevent this, the GPL assures that | ||
| 83 | patents cannot be used to render the program non-free. | ||
| 84 | |||
| 85 | The precise terms and conditions for copying, distribution and | ||
| 86 | modification follow. | ||
| 87 | |||
| 88 | TERMS AND CONDITIONS | ||
| 89 | |||
| 90 | 0. Definitions. | ||
| 91 | |||
| 92 | "This License" refers to version 3 of the GNU General Public License. | ||
| 93 | |||
| 94 | "Copyright" also means copyright-like laws that apply to other kinds of | ||
| 95 | works, such as semiconductor masks. | ||
| 96 | |||
| 97 | "The Program" refers to any copyrightable work licensed under this | ||
| 98 | License. Each licensee is addressed as "you". "Licensees" and | ||
| 99 | "recipients" may be individuals or organizations. | ||
| 100 | |||
| 101 | To "modify" a work means to copy from or adapt all or part of the work | ||
| 102 | in a fashion requiring copyright permission, other than the making of an | ||
| 103 | exact copy. The resulting work is called a "modified version" of the | ||
| 104 | earlier work or a work "based on" the earlier work. | ||
| 105 | |||
| 106 | A "covered work" means either the unmodified Program or a work based | ||
| 107 | on the Program. | ||
| 108 | |||
| 109 | To "propagate" a work means to do anything with it that, without | ||
| 110 | permission, would make you directly or secondarily liable for | ||
| 111 | infringement under applicable copyright law, except executing it on a | ||
| 112 | computer or modifying a private copy. Propagation includes copying, | ||
| 113 | distribution (with or without modification), making available to the | ||
| 114 | public, and in some countries other activities as well. | ||
| 115 | |||
| 116 | To "convey" a work means any kind of propagation that enables other | ||
| 117 | parties to make or receive copies. Mere interaction with a user through | ||
| 118 | a computer network, with no transfer of a copy, is not conveying. | ||
| 119 | |||
| 120 | An interactive user interface displays "Appropriate Legal Notices" | ||
| 121 | to the extent that it includes a convenient and prominently visible | ||
| 122 | feature that (1) displays an appropriate copyright notice, and (2) | ||
| 123 | tells the user that there is no warranty for the work (except to the | ||
| 124 | extent that warranties are provided), that licensees may convey the | ||
| 125 | work under this License, and how to view a copy of this License. If | ||
| 126 | the interface presents a list of user commands or options, such as a | ||
| 127 | menu, a prominent item in the list meets this criterion. | ||
| 128 | |||
| 129 | 1. Source Code. | ||
| 130 | |||
| 131 | The "source code" for a work means the preferred form of the work | ||
| 132 | for making modifications to it. "Object code" means any non-source | ||
| 133 | form of a work. | ||
| 134 | |||
| 135 | A "Standard Interface" means an interface that either is an official | ||
| 136 | standard defined by a recognized standards body, or, in the case of | ||
| 137 | interfaces specified for a particular programming language, one that | ||
| 138 | is widely used among developers working in that language. | ||
| 139 | |||
| 140 | The "System Libraries" of an executable work include anything, other | ||
| 141 | than the work as a whole, that (a) is included in the normal form of | ||
| 142 | packaging a Major Component, but which is not part of that Major | ||
| 143 | Component, and (b) serves only to enable use of the work with that | ||
| 144 | Major Component, or to implement a Standard Interface for which an | ||
| 145 | implementation is available to the public in source code form. A | ||
| 146 | "Major Component", in this context, means a major essential component | ||
| 147 | (kernel, window system, and so on) of the specific operating system | ||
| 148 | (if any) on which the executable work runs, or a compiler used to | ||
| 149 | produce the work, or an object code interpreter used to run it. | ||
| 150 | |||
| 151 | The "Corresponding Source" for a work in object code form means all | ||
| 152 | the source code needed to generate, install, and (for an executable | ||
| 153 | work) run the object code and to modify the work, including scripts to | ||
| 154 | control those activities. However, it does not include the work's | ||
| 155 | System Libraries, or general-purpose tools or generally available free | ||
| 156 | programs which are used unmodified in performing those activities but | ||
| 157 | which are not part of the work. For example, Corresponding Source | ||
| 158 | includes interface definition files associated with source files for | ||
| 159 | the work, and the source code for shared libraries and dynamically | ||
| 160 | linked subprograms that the work is specifically designed to require, | ||
| 161 | such as by intimate data communication or control flow between those | ||
| 162 | subprograms and other parts of the work. | ||
| 163 | |||
| 164 | The Corresponding Source need not include anything that users | ||
| 165 | can regenerate automatically from other parts of the Corresponding | ||
| 166 | Source. | ||
| 167 | |||
| 168 | The Corresponding Source for a work in source code form is that | ||
| 169 | same work. | ||
| 170 | |||
| 171 | 2. Basic Permissions. | ||
| 172 | |||
| 173 | All rights granted under this License are granted for the term of | ||
| 174 | copyright on the Program, and are irrevocable provided the stated | ||
| 175 | conditions are met. This License explicitly affirms your unlimited | ||
| 176 | permission to run the unmodified Program. The output from running a | ||
| 177 | covered work is covered by this License only if the output, given its | ||
| 178 | content, constitutes a covered work. This License acknowledges your | ||
| 179 | rights of fair use or other equivalent, as provided by copyright law. | ||
| 180 | |||
| 181 | You may make, run and propagate covered works that you do not | ||
| 182 | convey, without conditions so long as your license otherwise remains | ||
| 183 | in force. You may convey covered works to others for the sole purpose | ||
| 184 | of having them make modifications exclusively for you, or provide you | ||
| 185 | with facilities for running those works, provided that you comply with | ||
| 186 | the terms of this License in conveying all material for which you do | ||
| 187 | not control copyright. Those thus making or running the covered works | ||
| 188 | for you must do so exclusively on your behalf, under your direction | ||
| 189 | and control, on terms that prohibit them from making any copies of | ||
| 190 | your copyrighted material outside their relationship with you. | ||
| 191 | |||
| 192 | Conveying under any other circumstances is permitted solely under | ||
| 193 | the conditions stated below. Sublicensing is not allowed; section 10 | ||
| 194 | makes it unnecessary. | ||
| 195 | |||
| 196 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. | ||
| 197 | |||
| 198 | No covered work shall be deemed part of an effective technological | ||
| 199 | measure under any applicable law fulfilling obligations under article | ||
| 200 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or | ||
| 201 | similar laws prohibiting or restricting circumvention of such | ||
| 202 | measures. | ||
| 203 | |||
| 204 | When you convey a covered work, you waive any legal power to forbid | ||
| 205 | circumvention of technological measures to the extent such circumvention | ||
| 206 | is effected by exercising rights under this License with respect to | ||
| 207 | the covered work, and you disclaim any intention to limit operation or | ||
| 208 | modification of the work as a means of enforcing, against the work's | ||
| 209 | users, your or third parties' legal rights to forbid circumvention of | ||
| 210 | technological measures. | ||
| 211 | |||
| 212 | 4. Conveying Verbatim Copies. | ||
| 213 | |||
| 214 | You may convey verbatim copies of the Program's source code as you | ||
| 215 | receive it, in any medium, provided that you conspicuously and | ||
| 216 | appropriately publish on each copy an appropriate copyright notice; | ||
| 217 | keep intact all notices stating that this License and any | ||
| 218 | non-permissive terms added in accord with section 7 apply to the code; | ||
| 219 | keep intact all notices of the absence of any warranty; and give all | ||
| 220 | recipients a copy of this License along with the Program. | ||
| 221 | |||
| 222 | You may charge any price or no price for each copy that you convey, | ||
| 223 | and you may offer support or warranty protection for a fee. | ||
| 224 | |||
| 225 | 5. Conveying Modified Source Versions. | ||
| 226 | |||
| 227 | You may convey a work based on the Program, or the modifications to | ||
| 228 | produce it from the Program, in the form of source code under the | ||
| 229 | terms of section 4, provided that you also meet all of these conditions: | ||
| 230 | |||
| 231 | a) The work must carry prominent notices stating that you modified | ||
| 232 | it, and giving a relevant date. | ||
| 233 | |||
| 234 | b) The work must carry prominent notices stating that it is | ||
| 235 | released under this License and any conditions added under section | ||
| 236 | 7. This requirement modifies the requirement in section 4 to | ||
| 237 | "keep intact all notices". | ||
| 238 | |||
| 239 | c) You must license the entire work, as a whole, under this | ||
| 240 | License to anyone who comes into possession of a copy. This | ||
| 241 | License will therefore apply, along with any applicable section 7 | ||
| 242 | additional terms, to the whole of the work, and all its parts, | ||
| 243 | regardless of how they are packaged. This License gives no | ||
| 244 | permission to license the work in any other way, but it does not | ||
| 245 | invalidate such permission if you have separately received it. | ||
| 246 | |||
| 247 | d) If the work has interactive user interfaces, each must display | ||
| 248 | Appropriate Legal Notices; however, if the Program has interactive | ||
| 249 | interfaces that do not display Appropriate Legal Notices, your | ||
| 250 | work need not make them do so. | ||
| 251 | |||
| 252 | A compilation of a covered work with other separate and independent | ||
| 253 | works, which are not by their nature extensions of the covered work, | ||
| 254 | and which are not combined with it such as to form a larger program, | ||
| 255 | in or on a volume of a storage or distribution medium, is called an | ||
| 256 | "aggregate" if the compilation and its resulting copyright are not | ||
| 257 | used to limit the access or legal rights of the compilation's users | ||
| 258 | beyond what the individual works permit. Inclusion of a covered work | ||
| 259 | in an aggregate does not cause this License to apply to the other | ||
| 260 | parts of the aggregate. | ||
| 261 | |||
| 262 | 6. Conveying Non-Source Forms. | ||
| 263 | |||
| 264 | You may convey a covered work in object code form under the terms | ||
| 265 | of sections 4 and 5, provided that you also convey the | ||
| 266 | machine-readable Corresponding Source under the terms of this License, | ||
| 267 | in one of these ways: | ||
| 268 | |||
| 269 | a) Convey the object code in, or embodied in, a physical product | ||
| 270 | (including a physical distribution medium), accompanied by the | ||
| 271 | Corresponding Source fixed on a durable physical medium | ||
| 272 | customarily used for software interchange. | ||
| 273 | |||
| 274 | b) Convey the object code in, or embodied in, a physical product | ||
| 275 | (including a physical distribution medium), accompanied by a | ||
| 276 | written offer, valid for at least three years and valid for as | ||
| 277 | long as you offer spare parts or customer support for that product | ||
| 278 | model, to give anyone who possesses the object code either (1) a | ||
| 279 | copy of the Corresponding Source for all the software in the | ||
| 280 | product that is covered by this License, on a durable physical | ||
| 281 | medium customarily used for software interchange, for a price no | ||
| 282 | more than your reasonable cost of physically performing this | ||
| 283 | conveying of source, or (2) access to copy the | ||
| 284 | Corresponding Source from a network server at no charge. | ||
| 285 | |||
| 286 | c) Convey individual copies of the object code with a copy of the | ||
| 287 | written offer to provide the Corresponding Source. This | ||
| 288 | alternative is allowed only occasionally and noncommercially, and | ||
| 289 | only if you received the object code with such an offer, in accord | ||
| 290 | with subsection 6b. | ||
| 291 | |||
| 292 | d) Convey the object code by offering access from a designated | ||
| 293 | place (gratis or for a charge), and offer equivalent access to the | ||
| 294 | Corresponding Source in the same way through the same place at no | ||
| 295 | further charge. You need not require recipients to copy the | ||
| 296 | Corresponding Source along with the object code. If the place to | ||
| 297 | copy the object code is a network server, the Corresponding Source | ||
| 298 | may be on a different server (operated by you or a third party) | ||
| 299 | that supports equivalent copying facilities, provided you maintain | ||
| 300 | clear directions next to the object code saying where to find the | ||
| 301 | Corresponding Source. Regardless of what server hosts the | ||
| 302 | Corresponding Source, you remain obligated to ensure that it is | ||
| 303 | available for as long as needed to satisfy these requirements. | ||
| 304 | |||
| 305 | e) Convey the object code using peer-to-peer transmission, provided | ||
| 306 | you inform other peers where the object code and Corresponding | ||
| 307 | Source of the work are being offered to the general public at no | ||
| 308 | charge under subsection 6d. | ||
| 309 | |||
| 310 | A separable portion of the object code, whose source code is excluded | ||
| 311 | from the Corresponding Source as a System Library, need not be | ||
| 312 | included in conveying the object code work. | ||
| 313 | |||
| 314 | A "User Product" is either (1) a "consumer product", which means any | ||
| 315 | tangible personal property which is normally used for personal, family, | ||
| 316 | or household purposes, or (2) anything designed or sold for incorporation | ||
| 317 | into a dwelling. In determining whether a product is a consumer product, | ||
| 318 | doubtful cases shall be resolved in favor of coverage. For a particular | ||
| 319 | product received by a particular user, "normally used" refers to a | ||
| 320 | typical or common use of that class of product, regardless of the status | ||
| 321 | of the particular user or of the way in which the particular user | ||
| 322 | actually uses, or expects or is expected to use, the product. A product | ||
| 323 | is a consumer product regardless of whether the product has substantial | ||
| 324 | commercial, industrial or non-consumer uses, unless such uses represent | ||
| 325 | the only significant mode of use of the product. | ||
| 326 | |||
| 327 | "Installation Information" for a User Product means any methods, | ||
| 328 | procedures, authorization keys, or other information required to install | ||
| 329 | and execute modified versions of a covered work in that User Product from | ||
| 330 | a modified version of its Corresponding Source. The information must | ||
| 331 | suffice to ensure that the continued functioning of the modified object | ||
| 332 | code is in no case prevented or interfered with solely because | ||
| 333 | modification has been made. | ||
| 334 | |||
| 335 | If you convey an object code work under this section in, or with, or | ||
| 336 | specifically for use in, a User Product, and the conveying occurs as | ||
| 337 | part of a transaction in which the right of possession and use of the | ||
| 338 | User Product is transferred to the recipient in perpetuity or for a | ||
| 339 | fixed term (regardless of how the transaction is characterized), the | ||
| 340 | Corresponding Source conveyed under this section must be accompanied | ||
| 341 | by the Installation Information. But this requirement does not apply | ||
| 342 | if neither you nor any third party retains the ability to install | ||
| 343 | modified object code on the User Product (for example, the work has | ||
| 344 | been installed in ROM). | ||
| 345 | |||
| 346 | The requirement to provide Installation Information does not include a | ||
| 347 | requirement to continue to provide support service, warranty, or updates | ||
| 348 | for a work that has been modified or installed by the recipient, or for | ||
| 349 | the User Product in which it has been modified or installed. Access to a | ||
| 350 | network may be denied when the modification itself materially and | ||
| 351 | adversely affects the operation of the network or violates the rules and | ||
| 352 | protocols for communication across the network. | ||
| 353 | |||
| 354 | Corresponding Source conveyed, and Installation Information provided, | ||
| 355 | in accord with this section must be in a format that is publicly | ||
| 356 | documented (and with an implementation available to the public in | ||
| 357 | source code form), and must require no special password or key for | ||
| 358 | unpacking, reading or copying. | ||
| 359 | |||
| 360 | 7. Additional Terms. | ||
| 361 | |||
| 362 | "Additional permissions" are terms that supplement the terms of this | ||
| 363 | License by making exceptions from one or more of its conditions. | ||
| 364 | Additional permissions that are applicable to the entire Program shall | ||
| 365 | be treated as though they were included in this License, to the extent | ||
| 366 | that they are valid under applicable law. If additional permissions | ||
| 367 | apply only to part of the Program, that part may be used separately | ||
| 368 | under those permissions, but the entire Program remains governed by | ||
| 369 | this License without regard to the additional permissions. | ||
| 370 | |||
| 371 | When you convey a copy of a covered work, you may at your option | ||
| 372 | remove any additional permissions from that copy, or from any part of | ||
| 373 | it. (Additional permissions may be written to require their own | ||
| 374 | removal in certain cases when you modify the work.) You may place | ||
| 375 | additional permissions on material, added by you to a covered work, | ||
| 376 | for which you have or can give appropriate copyright permission. | ||
| 377 | |||
| 378 | Notwithstanding any other provision of this License, for material you | ||
| 379 | add to a covered work, you may (if authorized by the copyright holders of | ||
| 380 | that material) supplement the terms of this License with terms: | ||
| 381 | |||
| 382 | a) Disclaiming warranty or limiting liability differently from the | ||
| 383 | terms of sections 15 and 16 of this License; or | ||
| 384 | |||
| 385 | b) Requiring preservation of specified reasonable legal notices or | ||
| 386 | author attributions in that material or in the Appropriate Legal | ||
| 387 | Notices displayed by works containing it; or | ||
| 388 | |||
| 389 | c) Prohibiting misrepresentation of the origin of that material, or | ||
| 390 | requiring that modified versions of such material be marked in | ||
| 391 | reasonable ways as different from the original version; or | ||
| 392 | |||
| 393 | d) Limiting the use for publicity purposes of names of licensors or | ||
| 394 | authors of the material; or | ||
| 395 | |||
| 396 | e) Declining to grant rights under trademark law for use of some | ||
| 397 | trade names, trademarks, or service marks; or | ||
| 398 | |||
| 399 | f) Requiring indemnification of licensors and authors of that | ||
| 400 | material by anyone who conveys the material (or modified versions of | ||
| 401 | it) with contractual assumptions of liability to the recipient, for | ||
| 402 | any liability that these contractual assumptions directly impose on | ||
| 403 | those licensors and authors. | ||
| 404 | |||
| 405 | All other non-permissive additional terms are considered "further | ||
| 406 | restrictions" within the meaning of section 10. If the Program as you | ||
| 407 | received it, or any part of it, contains a notice stating that it is | ||
| 408 | governed by this License along with a term that is a further | ||
| 409 | restriction, you may remove that term. If a license document contains | ||
| 410 | a further restriction but permits relicensing or conveying under this | ||
| 411 | License, you may add to a covered work material governed by the terms | ||
| 412 | of that license document, provided that the further restriction does | ||
| 413 | not survive such relicensing or conveying. | ||
| 414 | |||
| 415 | If you add terms to a covered work in accord with this section, you | ||
| 416 | must place, in the relevant source files, a statement of the | ||
| 417 | additional terms that apply to those files, or a notice indicating | ||
| 418 | where to find the applicable terms. | ||
| 419 | |||
| 420 | Additional terms, permissive or non-permissive, may be stated in the | ||
| 421 | form of a separately written license, or stated as exceptions; | ||
| 422 | the above requirements apply either way. | ||
| 423 | |||
| 424 | 8. Termination. | ||
| 425 | |||
| 426 | You may not propagate or modify a covered work except as expressly | ||
| 427 | provided under this License. Any attempt otherwise to propagate or | ||
| 428 | modify it is void, and will automatically terminate your rights under | ||
| 429 | this License (including any patent licenses granted under the third | ||
| 430 | paragraph of section 11). | ||
| 431 | |||
| 432 | However, if you cease all violation of this License, then your | ||
| 433 | license from a particular copyright holder is reinstated (a) | ||
| 434 | provisionally, unless and until the copyright holder explicitly and | ||
| 435 | finally terminates your license, and (b) permanently, if the copyright | ||
| 436 | holder fails to notify you of the violation by some reasonable means | ||
| 437 | prior to 60 days after the cessation. | ||
| 438 | |||
| 439 | Moreover, your license from a particular copyright holder is | ||
| 440 | reinstated permanently if the copyright holder notifies you of the | ||
| 441 | violation by some reasonable means, this is the first time you have | ||
| 442 | received notice of violation of this License (for any work) from that | ||
| 443 | copyright holder, and you cure the violation prior to 30 days after | ||
| 444 | your receipt of the notice. | ||
| 445 | |||
| 446 | Termination of your rights under this section does not terminate the | ||
| 447 | licenses of parties who have received copies or rights from you under | ||
| 448 | this License. If your rights have been terminated and not permanently | ||
| 449 | reinstated, you do not qualify to receive new licenses for the same | ||
| 450 | material under section 10. | ||
| 451 | |||
| 452 | 9. Acceptance Not Required for Having Copies. | ||
| 453 | |||
| 454 | You are not required to accept this License in order to receive or | ||
| 455 | run a copy of the Program. Ancillary propagation of a covered work | ||
| 456 | occurring solely as a consequence of using peer-to-peer transmission | ||
| 457 | to receive a copy likewise does not require acceptance. However, | ||
| 458 | nothing other than this License grants you permission to propagate or | ||
| 459 | modify any covered work. These actions infringe copyright if you do | ||
| 460 | not accept this License. Therefore, by modifying or propagating a | ||
| 461 | covered work, you indicate your acceptance of this License to do so. | ||
| 462 | |||
| 463 | 10. Automatic Licensing of Downstream Recipients. | ||
| 464 | |||
| 465 | Each time you convey a covered work, the recipient automatically | ||
| 466 | receives a license from the original licensors, to run, modify and | ||
| 467 | propagate that work, subject to this License. You are not responsible | ||
| 468 | for enforcing compliance by third parties with this License. | ||
| 469 | |||
| 470 | An "entity transaction" is a transaction transferring control of an | ||
| 471 | organization, or substantially all assets of one, or subdividing an | ||
| 472 | organization, or merging organizations. If propagation of a covered | ||
| 473 | work results from an entity transaction, each party to that | ||
| 474 | transaction who receives a copy of the work also receives whatever | ||
| 475 | licenses to the work the party's predecessor in interest had or could | ||
| 476 | give under the previous paragraph, plus a right to possession of the | ||
| 477 | Corresponding Source of the work from the predecessor in interest, if | ||
| 478 | the predecessor has it or can get it with reasonable efforts. | ||
| 479 | |||
| 480 | You may not impose any further restrictions on the exercise of the | ||
| 481 | rights granted or affirmed under this License. For example, you may | ||
| 482 | not impose a license fee, royalty, or other charge for exercise of | ||
| 483 | rights granted under this License, and you may not initiate litigation | ||
| 484 | (including a cross-claim or counterclaim in a lawsuit) alleging that | ||
| 485 | any patent claim is infringed by making, using, selling, offering for | ||
| 486 | sale, or importing the Program or any portion of it. | ||
| 487 | |||
| 488 | 11. Patents. | ||
| 489 | |||
| 490 | A "contributor" is a copyright holder who authorizes use under this | ||
| 491 | License of the Program or a work on which the Program is based. The | ||
| 492 | work thus licensed is called the contributor's "contributor version". | ||
| 493 | |||
| 494 | A contributor's "essential patent claims" are all patent claims | ||
| 495 | owned or controlled by the contributor, whether already acquired or | ||
| 496 | hereafter acquired, that would be infringed by some manner, permitted | ||
| 497 | by this License, of making, using, or selling its contributor version, | ||
| 498 | but do not include claims that would be infringed only as a | ||
| 499 | consequence of further modification of the contributor version. For | ||
| 500 | purposes of this definition, "control" includes the right to grant | ||
| 501 | patent sublicenses in a manner consistent with the requirements of | ||
| 502 | this License. | ||
| 503 | |||
| 504 | Each contributor grants you a non-exclusive, worldwide, royalty-free | ||
| 505 | patent license under the contributor's essential patent claims, to | ||
| 506 | make, use, sell, offer for sale, import and otherwise run, modify and | ||
| 507 | propagate the contents of its contributor version. | ||
| 508 | |||
| 509 | In the following three paragraphs, a "patent license" is any express | ||
| 510 | agreement or commitment, however denominated, not to enforce a patent | ||
| 511 | (such as an express permission to practice a patent or covenant not to | ||
| 512 | sue for patent infringement). To "grant" such a patent license to a | ||
| 513 | party means to make such an agreement or commitment not to enforce a | ||
| 514 | patent against the party. | ||
| 515 | |||
| 516 | If you convey a covered work, knowingly relying on a patent license, | ||
| 517 | and the Corresponding Source of the work is not available for anyone | ||
| 518 | to copy, free of charge and under the terms of this License, through a | ||
| 519 | publicly available network server or other readily accessible means, | ||
| 520 | then you must either (1) cause the Corresponding Source to be so | ||
| 521 | available, or (2) arrange to deprive yourself of the benefit of the | ||
| 522 | patent license for this particular work, or (3) arrange, in a manner | ||
| 523 | consistent with the requirements of this License, to extend the patent | ||
| 524 | license to downstream recipients. "Knowingly relying" means you have | ||
| 525 | actual knowledge that, but for the patent license, your conveying the | ||
| 526 | covered work in a country, or your recipient's use of the covered work | ||
| 527 | in a country, would infringe one or more identifiable patents in that | ||
| 528 | country that you have reason to believe are valid. | ||
| 529 | |||
| 530 | If, pursuant to or in connection with a single transaction or | ||
| 531 | arrangement, you convey, or propagate by procuring conveyance of, a | ||
| 532 | covered work, and grant a patent license to some of the parties | ||
| 533 | receiving the covered work authorizing them to use, propagate, modify | ||
| 534 | or convey a specific copy of the covered work, then the patent license | ||
| 535 | you grant is automatically extended to all recipients of the covered | ||
| 536 | work and works based on it. | ||
| 537 | |||
| 538 | A patent license is "discriminatory" if it does not include within | ||
| 539 | the scope of its coverage, prohibits the exercise of, or is | ||
| 540 | conditioned on the non-exercise of one or more of the rights that are | ||
| 541 | specifically granted under this License. You may not convey a covered | ||
| 542 | work if you are a party to an arrangement with a third party that is | ||
| 543 | in the business of distributing software, under which you make payment | ||
| 544 | to the third party based on the extent of your activity of conveying | ||
| 545 | the work, and under which the third party grants, to any of the | ||
| 546 | parties who would receive the covered work from you, a discriminatory | ||
| 547 | patent license (a) in connection with copies of the covered work | ||
| 548 | conveyed by you (or copies made from those copies), or (b) primarily | ||
| 549 | for and in connection with specific products or compilations that | ||
| 550 | contain the covered work, unless you entered into that arrangement, | ||
| 551 | or that patent license was granted, prior to 28 March 2007. | ||
| 552 | |||
| 553 | Nothing in this License shall be construed as excluding or limiting | ||
| 554 | any implied license or other defenses to infringement that may | ||
| 555 | otherwise be available to you under applicable patent law. | ||
| 556 | |||
| 557 | 12. No Surrender of Others' Freedom. | ||
| 558 | |||
| 559 | If conditions are imposed on you (whether by court order, agreement or | ||
| 560 | otherwise) that contradict the conditions of this License, they do not | ||
| 561 | excuse you from the conditions of this License. If you cannot convey a | ||
| 562 | covered work so as to satisfy simultaneously your obligations under this | ||
| 563 | License and any other pertinent obligations, then as a consequence you may | ||
| 564 | not convey it at all. For example, if you agree to terms that obligate you | ||
| 565 | to collect a royalty for further conveying from those to whom you convey | ||
| 566 | the Program, the only way you could satisfy both those terms and this | ||
| 567 | License would be to refrain entirely from conveying the Program. | ||
| 568 | |||
| 569 | 13. Use with the GNU Affero General Public License. | ||
| 570 | |||
| 571 | Notwithstanding any other provision of this License, you have | ||
| 572 | permission to link or combine any covered work with a work licensed | ||
| 573 | under version 3 of the GNU Affero General Public License into a single | ||
| 574 | combined work, and to convey the resulting work. The terms of this | ||
| 575 | License will continue to apply to the part which is the covered work, | ||
| 576 | but the special requirements of the GNU Affero General Public License, | ||
| 577 | section 13, concerning interaction through a network will apply to the | ||
| 578 | combination as such. | ||
| 579 | |||
| 580 | 14. Revised Versions of this License. | ||
| 581 | |||
| 582 | The Free Software Foundation may publish revised and/or new versions of | ||
| 583 | the GNU General Public License from time to time. Such new versions will | ||
| 584 | be similar in spirit to the present version, but may differ in detail to | ||
| 585 | address new problems or concerns. | ||
| 586 | |||
| 587 | Each version is given a distinguishing version number. If the | ||
| 588 | Program specifies that a certain numbered version of the GNU General | ||
| 589 | Public License "or any later version" applies to it, you have the | ||
| 590 | option of following the terms and conditions either of that numbered | ||
| 591 | version or of any later version published by the Free Software | ||
| 592 | Foundation. If the Program does not specify a version number of the | ||
| 593 | GNU General Public License, you may choose any version ever published | ||
| 594 | by the Free Software Foundation. | ||
| 595 | |||
| 596 | If the Program specifies that a proxy can decide which future | ||
| 597 | versions of the GNU General Public License can be used, that proxy's | ||
| 598 | public statement of acceptance of a version permanently authorizes you | ||
| 599 | to choose that version for the Program. | ||
| 600 | |||
| 601 | Later license versions may give you additional or different | ||
| 602 | permissions. However, no additional obligations are imposed on any | ||
| 603 | author or copyright holder as a result of your choosing to follow a | ||
| 604 | later version. | ||
| 605 | |||
| 606 | 15. Disclaimer of Warranty. | ||
| 607 | |||
| 608 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY | ||
| 609 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT | ||
| 610 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY | ||
| 611 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, | ||
| 612 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 613 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM | ||
| 614 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF | ||
| 615 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | ||
| 616 | |||
| 617 | 16. Limitation of Liability. | ||
| 618 | |||
| 619 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 620 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS | ||
| 621 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY | ||
| 622 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE | ||
| 623 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF | ||
| 624 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD | ||
| 625 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), | ||
| 626 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | ||
| 627 | SUCH DAMAGES. | ||
| 628 | |||
| 629 | 17. Interpretation of Sections 15 and 16. | ||
| 630 | |||
| 631 | If the disclaimer of warranty and limitation of liability provided | ||
| 632 | above cannot be given local legal effect according to their terms, | ||
| 633 | reviewing courts shall apply local law that most closely approximates | ||
| 634 | an absolute waiver of all civil liability in connection with the | ||
| 635 | Program, unless a warranty or assumption of liability accompanies a | ||
| 636 | copy of the Program in return for a fee. | ||
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9ae2a4f --- /dev/null +++ b/Makefile | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | # See LICENSE file for copyright and license details. | ||
| 2 | |||
| 3 | CFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ | ||
| 4 | -Wno-unused-parameter -O3 | ||
| 5 | DBGFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ | ||
| 6 | -Wno-unused-parameter -Wno-unused-function -g3 \ | ||
| 7 | -fsanitize=address -fsanitize=undefined | ||
| 8 | |||
| 9 | CC = cc | ||
| 10 | |||
| 11 | all: solve | ||
| 12 | |||
| 13 | solve: clean | ||
| 14 | ${CC} ${CFLAGS} -o solve src/*.c | ||
| 15 | |||
| 16 | debug: | ||
| 17 | ${CC} ${DBGFLAGS} -o solve src/*.c | ||
| 18 | |||
| 19 | clean: | ||
| 20 | rm -rf solve | ||
| 21 | |||
| 22 | test: | ||
| 23 | ./test/test.sh | ||
| 24 | |||
| 25 | .PHONY: all debug clean test | ||
diff --git a/README.md b/README.md new file mode 100644 index 0000000..68126a3 --- /dev/null +++ b/README.md | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | # Prototype for a new optimal solver | ||
| 2 | |||
| 3 | Work in progress | ||
diff --git a/src/cube.c b/src/cube.c new file mode 100644 index 0000000..4372b5e --- /dev/null +++ b/src/cube.c | |||
| @@ -0,0 +1,888 @@ | |||
| 1 | /* | ||
| 2 | # Cube representation, moves, transformations and (tentatively) indexing | ||
| 3 | |||
| 4 | ## Textual description | ||
| 5 | |||
| 6 | The functions readcube() and writecube() use the following format. | ||
| 7 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 8 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 9 | Similarly, each corner is represented by three letters and a number | ||
| 10 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 11 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 12 | relative to the U / D axis. | ||
| 13 | |||
| 14 | The correct order of the pieces is the same as that defined in the | ||
| 15 | section "Internal cube representation", except that pieces are read | ||
| 16 | left-to-right. Pieces are divided by slices, so the ordering is not the | ||
| 17 | most intuitive, but it is more convenient for the internal representation. | ||
| 18 | |||
| 19 | Whitespaces between pieces are ignored when reading the cube, and a | ||
| 20 | single whitespace character is added between pieces when writing. | ||
| 21 | |||
| 22 | For example, the solved cube looks like this: | ||
| 23 | |||
| 24 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 \ (no newline) | ||
| 25 | UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 26 | |||
| 27 | The cube after the moves R'U'F looks like this: | ||
| 28 | |||
| 29 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 \ (no newline) | ||
| 30 | UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 31 | |||
| 32 | More formats might be supported in the future. | ||
| 33 | |||
| 34 | ## Internal cube representation | ||
| 35 | |||
| 36 | The cube_t data structure implemented in this file is designed to | ||
| 37 | efficiently perform common operations on a 3x3x3 Rubik's cube when | ||
| 38 | solving it with an iterative-deepening DFS search. It is not the most | ||
| 39 | general, complete, easy to read or compact one. Since the cube can | ||
| 40 | be trivially reoriented before the search, we only encode permutations | ||
| 41 | of the cube that keep the center pieces in a fixed position (that is, | ||
| 42 | we do not encode the position of the centers). | ||
| 43 | |||
| 44 | The cube state is encoded in two 64-bit integers, one for edges and one | ||
| 45 | for centers. We explain how edges are encoded first, and the highlight | ||
| 46 | the few differences with corners afterwards. | ||
| 47 | |||
| 48 | For encoding edges, only the 60 least-significant bits are used. Each | ||
| 49 | edge described by 5 bits. The position of a 5-bit block in the 64-bit | ||
| 50 | integer determine the position of the edge piece in the cube, according | ||
| 51 | to the following table (least-significant bits on the right): | ||
| 52 | |||
| 53 | 55-59 50-54 45-49 40-44 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 54 | BR BL FL FR DR DL UL UR DF DB UB UF | ||
| 55 | ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee | ||
| 56 | |||
| 57 | For each edge, the 4 least-significant bits ('ssee' in the table) | ||
| 58 | determine the piece. The two bits marked with 'ss' determine the internal | ||
| 59 | slice the piece belongs to, i.e. they are either '00' for M, '01' for | ||
| 60 | S or '10' for E. The other two bits (marked with 'ee') determine the | ||
| 61 | actual edge piece among the 4 in the same slice, and they are assigned | ||
| 62 | somewhat arbitarily. Using this representation and the ordering defined | ||
| 63 | in the table above, the edges are correctly permuted when these 4 bits | ||
| 64 | for each represent the numbers 0 to 11 in the correct order. | ||
| 65 | |||
| 66 | The last bit determines the orientation. The orientation of an edge | ||
| 67 | depends on its position, and it is defined being 0 if the edge can be | ||
| 68 | moved to its place in the solved orientation by permutations in the | ||
| 69 | subgroup <U, D, R, L, F2, B2>. | ||
| 70 | |||
| 71 | Corners are encoded in the 48 least-significant bits, and are described | ||
| 72 | by 6 bits each, their position being defined by the following table: | ||
| 73 | |||
| 74 | 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 75 | DBL DFL UBR UFL DBR DFL UBL UFR | ||
| 76 | oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc | ||
| 77 | |||
| 78 | The bit marked with an 'x' describes the axis the corner belongs to. | ||
| 79 | The 0 axis consists of the corners UFR, UBL, DFL and DBR, and the other | ||
| 80 | four corners form the axis marked with 1. Then two bits are needed to | ||
| 81 | identify the corner among the four of the same axis. The last three bits | ||
| 82 | determine the orientation, where one corner is defined to be oriented | ||
| 83 | (marked with '000') if its top or bottom sticker faces the top or bottom | ||
| 84 | side. A corner a clockwise turn away from being oriented, thus requiring | ||
| 85 | a counter-clockwise turn to be oriented correctly, is marked with '001', | ||
| 86 | and a corner a counter-clockwise turn away is marked with '010'. The most | ||
| 87 | significant bit is not used to determine the corner orientation, but it | ||
| 88 | must always be set to '0' to simplify the moving operations (see below). | ||
| 89 | |||
| 90 | ## Basic moves | ||
| 91 | |||
| 92 | The 18 basic moves of the cube could be performed by applying a suitable | ||
| 93 | general permutation (see below), but they have instead been manually | ||
| 94 | implemented with a few simple operations each, to improve performance. | ||
| 95 | |||
| 96 | For each move we first permute the pieces. This amounts to shifting | ||
| 97 | around 4 blocks of bits for edges and 4 for corners. Since in some cases | ||
| 98 | adjacent pieces on the cube are also adjacent in the bit representation we | ||
| 99 | use, we can save some operations by shifting multiple blocks together. | ||
| 100 | |||
| 101 | There are some moves that change the orientation of the pieces. Namely, | ||
| 102 | the moves F, F', B and B' change the orientation of the edges and those | ||
| 103 | moves as well as R, R', L and L' change the orientation of the corners. | ||
| 104 | Edge orientation is easy to address: we simply xor the edge representation | ||
| 105 | by a bit mask with zeroes everywhere except for the 4 edges that need | ||
| 106 | to be flipped (i.e. the ones on the twisted face). | ||
| 107 | |||
| 108 | Corner orientation is harder to reproduce efficiently working only with | ||
| 109 | bitwise operations, as it involves performing operations modulo 3. | ||
| 110 | However, with the help of the extra bit we reserved, we are able to | ||
| 111 | do this using only two additions and 3 bitwise operations, without | ||
| 112 | any multiplication, division or modulo operation. The trick is | ||
| 113 | to use the following formula to sum two numbers x, y in {0,1,2}: | ||
| 114 | |||
| 115 | ((x+y) + (x+y+1)/4) % 4 | ||
| 116 | |||
| 117 | The thrid bit is needed because x+y and x+y+1 can exceed 3. We can | ||
| 118 | apply this operation to multiple pairs of bits representing ternary | ||
| 119 | digits at once using binary operations. See the function coapply() | ||
| 120 | below for the details. | ||
| 121 | |||
| 122 | ## Inverting the cube | ||
| 123 | |||
| 124 | TODO | ||
| 125 | |||
| 126 | ## Transformations (conjugations by full-cube rotations) | ||
| 127 | |||
| 128 | TODO | ||
| 129 | |||
| 130 | ## Indexing (tentative) | ||
| 131 | |||
| 132 | TODO - subgroup description etc | ||
| 133 | |||
| 134 | */ | ||
| 135 | |||
| 136 | #include <stdbool.h> | ||
| 137 | #include <stdint.h> | ||
| 138 | #include <string.h> | ||
| 139 | |||
| 140 | #ifdef DEBUG | ||
| 141 | #include <stdio.h> | ||
| 142 | #endif | ||
| 143 | |||
| 144 | #include "cube.h" | ||
| 145 | |||
| 146 | #define _error 0xFFFFFFFF | ||
| 147 | |||
| 148 | #define _eoblock 0x10ULL /* 10000 */ | ||
| 149 | #define _epblock 0x0FULL /* 01111 */ | ||
| 150 | #define _eblock 0x1FULL /* 11111 */ | ||
| 151 | #define _eblock2 0x3FFULL /* 1111111111 */ | ||
| 152 | |||
| 153 | #define _coblock 0x18ULL /* 011000 */ | ||
| 154 | #define _cpblock 0x07ULL /* 000111 */ | ||
| 155 | #define _cblock 0x3FULL /* 111111 */ | ||
| 156 | #define _cblock2 0xFFFULL /* 111111111111 */ | ||
| 157 | |||
| 158 | #define _eomask 0x842108421084210ULL /* 10000 repeated 12 times */ | ||
| 159 | #define _comask 0x618618618618ULL /* 011000 repeated 8 times */ | ||
| 160 | #define _coonemask 0x208208208208ULL /* 001000 repeated 8 times */ | ||
| 161 | #define _coextramask 0x820820820820ULL /* 100000 repeated 8 times */ | ||
| 162 | |||
| 163 | #define _emask_u (_eblock2 | _eblock2 << 20ULL) | ||
| 164 | #define _emask_d (_eblock2 << 10ULL | _eblock2 << 30ULL) | ||
| 165 | #define _emask_r (_eblock << 20ULL | _eblock2 << 35ULL | _eblock << 55ULL) | ||
| 166 | #define _emask_l (_eblock2 << 45ULL | _eblock2 << 25ULL) | ||
| 167 | #define _emask_f (_eblock | _eblock << 15ULL | _eblock2 << 40ULL) | ||
| 168 | #define _emask_b (_eblock2 << 5ULL | _eblock2 << 50ULL) | ||
| 169 | |||
| 170 | #define _cmask_u (_cblock2 | _cblock2 << 24ULL) | ||
| 171 | #define _cmask_d (_cblock2 << 12ULL | _cblock2 << 36ULL) | ||
| 172 | #define _cmask_r (_cblock | _cblock << 18ULL | _cblock2 << 30ULL) | ||
| 173 | #define _cmask_l (_cblock2 << 6ULL | _cblock << 24ULL | _cblock << 42ULL) | ||
| 174 | #define _cmask_f (_cblock | _cblock << 12ULL | _cblock << 24ULL | _cblock << 36ULL) | ||
| 175 | #define _cmask_b (_cblock << 6ULL | _cblock << 18ULL | _cblock << 30ULL | _cblock << 42ULL) | ||
| 176 | |||
| 177 | #define _eomask_f (1ULL << 4ULL | 1ULL << 19ULL | 1ULL << 44ULL | 1ULL << 49ULL) | ||
| 178 | #define _eomask_b (1ULL << 9ULL | 1ULL << 14ULL | 1ULL << 54ULL | 1ULL << 59ULL) | ||
| 179 | |||
| 180 | #define _comask_r (2ULL << 3ULL | 1ULL << 33ULL | 2ULL << 21ULL | 1ULL << 39ULL) | ||
| 181 | #define _comask_l (1ULL << 27ULL | 2ULL << 9ULL | 2ULL << 15ULL | 1ULL << 45ULL) | ||
| 182 | #define _comask_f (1ULL << 3ULL | 1ULL << 15ULL | 2ULL << 27ULL | 2ULL << 39ULL) | ||
| 183 | #define _comask_b (1ULL << 9ULL | 1ULL << 21ULL | 2ULL << 33ULL | 2ULL << 45ULL) | ||
| 184 | |||
| 185 | static inline uint64_t coapply(uint64_t, uint64_t); | ||
| 186 | static uint64_t permsign(uint64_t *, int); | ||
| 187 | static uint64_t readep(char *); | ||
| 188 | static uint64_t readeo(char *); | ||
| 189 | static uint64_t readcp(char *); | ||
| 190 | static uint64_t readco(char *); | ||
| 191 | static uint64_t readmove(char); | ||
| 192 | static uint64_t readmodifier(char); | ||
| 193 | |||
| 194 | static char *edgestr[] = { | ||
| 195 | "UF", "UB", "DB", "DF", | ||
| 196 | "UR", "UL", "DL", "DR", | ||
| 197 | "FR", "FL", "BL", "BR" | ||
| 198 | }; | ||
| 199 | static char *cornerstr[] = { | ||
| 200 | "UFR", "UBL", "DFL", "DBR", | ||
| 201 | "UFL", "UBR", "DFR", "DBL" | ||
| 202 | }; | ||
| 203 | static char *cornerstralt[] = { | ||
| 204 | "URF", "ULB", "DLF", "DRB", | ||
| 205 | "ULF", "URB", "DRF", "DLB" | ||
| 206 | }; | ||
| 207 | static char *movestr[] = { | ||
| 208 | [U] = "U", [U2] = "U2", [U3] = "U'", | ||
| 209 | [D] = "D", [D2] = "D2", [D3] = "D'", | ||
| 210 | [R] = "R", [R2] = "R2", [R3] = "R'", | ||
| 211 | [L] = "L", [L2] = "L2", [L3] = "L'", | ||
| 212 | [F] = "F", [F2] = "F2", [F3] = "F'", | ||
| 213 | [B] = "B", [B2] = "B2", [B3] = "B'", | ||
| 214 | }; | ||
| 215 | |||
| 216 | cube_t solvedcube = { .e = 0x5A928398A418820ULL, .c = 0x1C61440C2040ULL }; | ||
| 217 | cube_t errorcube = { .e = _error, .c = _error }; | ||
| 218 | |||
| 219 | |||
| 220 | static uint64_t | ||
| 221 | permsign(uint64_t *a, int n) | ||
| 222 | { | ||
| 223 | int i, j; | ||
| 224 | uint64_t ret; | ||
| 225 | |||
| 226 | ret = 0; | ||
| 227 | |||
| 228 | for (i = 0; i < n; i++) | ||
| 229 | for (j = i+1; j < n; j++) | ||
| 230 | ret += a[i] > a[j] ? 1 : 0; | ||
| 231 | |||
| 232 | return ret % 2; | ||
| 233 | } | ||
| 234 | |||
| 235 | bool | ||
| 236 | isconsistent(cube_t cube) | ||
| 237 | { | ||
| 238 | uint64_t x, p[12], sum, co; | ||
| 239 | bool found[12]; | ||
| 240 | int i; | ||
| 241 | |||
| 242 | sum = 0; | ||
| 243 | |||
| 244 | /* Check for EP consistency */ | ||
| 245 | for (i = 0; i < 12; i++) | ||
| 246 | found[i] = false; | ||
| 247 | for (i = 0, x = cube.e; i < 12; i++, x >>= 5ULL) { | ||
| 248 | p[i] = x & _epblock; | ||
| 249 | if (p[i] >= 12) | ||
| 250 | goto inconsistent_ep; | ||
| 251 | found[p[i]] = true; | ||
| 252 | } | ||
| 253 | for (i = 0; i < 12; i++) | ||
| 254 | if (!found[i]) | ||
| 255 | goto inconsistent_ep; | ||
| 256 | sum = permsign(p, 12); | ||
| 257 | |||
| 258 | /* Check for CP consistency */ | ||
| 259 | for (i = 0; i < 8; i++) | ||
| 260 | found[i] = false; | ||
| 261 | for (i = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 262 | p[i] = x & _cpblock; | ||
| 263 | if (p[i] >= 8) | ||
| 264 | goto inconsistent_cp; | ||
| 265 | found[p[i]] = true; | ||
| 266 | } | ||
| 267 | for (i = 0; i < 8; i++) | ||
| 268 | if (!found[i]) | ||
| 269 | goto inconsistent_cp; | ||
| 270 | sum += permsign(p, 8); | ||
| 271 | |||
| 272 | /* Check permutation parity */ | ||
| 273 | if (sum % 2 != 0) | ||
| 274 | goto inconsistent_parity; | ||
| 275 | |||
| 276 | /* Check for EO parity */ | ||
| 277 | for (i = 0, sum = 0, x = cube.e; i < 12; i++, x >>= 5ULL) | ||
| 278 | sum += (x & _eoblock) >> 4ULL; | ||
| 279 | if (sum % 2 != 0) | ||
| 280 | goto inconsistent_eo; | ||
| 281 | |||
| 282 | /* Check for CO parity */ | ||
| 283 | for (i = 0, sum = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 284 | co = (x & _coblock) >> 3ULL; | ||
| 285 | if (co > 2) | ||
| 286 | goto inconsistent_co3; | ||
| 287 | sum += co; | ||
| 288 | } | ||
| 289 | if (sum % 3 != 0) | ||
| 290 | goto inconsistent_co; | ||
| 291 | |||
| 292 | /* Check that CO extra bit is zero */ | ||
| 293 | if (cube.c & _coextramask) | ||
| 294 | goto inconsistent_coextra; | ||
| 295 | |||
| 296 | return true; | ||
| 297 | |||
| 298 | inconsistent_ep: | ||
| 299 | #ifdef DEBUG | ||
| 300 | fprintf(stderr, "Inconsistent EP\n"); | ||
| 301 | #endif | ||
| 302 | goto inconsistent_return; | ||
| 303 | inconsistent_cp: | ||
| 304 | #ifdef DEBUG | ||
| 305 | fprintf(stderr, "Inconsistent CP\n"); | ||
| 306 | #endif | ||
| 307 | goto inconsistent_return; | ||
| 308 | inconsistent_parity: | ||
| 309 | #ifdef DEBUG | ||
| 310 | fprintf(stderr, "Inconsistent parity\n"); | ||
| 311 | #endif | ||
| 312 | goto inconsistent_return; | ||
| 313 | inconsistent_eo: | ||
| 314 | #ifdef DEBUG | ||
| 315 | fprintf(stderr, "Inconsistent EO\n"); | ||
| 316 | #endif | ||
| 317 | goto inconsistent_return; | ||
| 318 | inconsistent_co3: | ||
| 319 | #ifdef DEBUG | ||
| 320 | fprintf(stderr, "Inconsistent CO=3\n"); | ||
| 321 | #endif | ||
| 322 | goto inconsistent_return; | ||
| 323 | inconsistent_co: | ||
| 324 | #ifdef DEBUG | ||
| 325 | fprintf(stderr, "Inconsistent CO\n"); | ||
| 326 | #endif | ||
| 327 | goto inconsistent_return; | ||
| 328 | inconsistent_coextra: | ||
| 329 | #ifdef DEBUG | ||
| 330 | fprintf(stderr, "Inconsistent extra bit for CO\n"); | ||
| 331 | #endif | ||
| 332 | goto inconsistent_return; | ||
| 333 | inconsistent_return: | ||
| 334 | return false; | ||
| 335 | } | ||
| 336 | |||
| 337 | bool | ||
| 338 | issolved(cube_t cube) | ||
| 339 | { | ||
| 340 | return cube.c == solvedcube.c && cube.e == solvedcube.e; | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | static uint64_t | ||
| 345 | readep(char *str) | ||
| 346 | { | ||
| 347 | uint64_t e; | ||
| 348 | |||
| 349 | for (e = 0; e < 12; e++) | ||
| 350 | if (!strncmp(str, edgestr[e], 2)) | ||
| 351 | return e; | ||
| 352 | |||
| 353 | #ifdef DEBUG | ||
| 354 | fprintf(stderr, "Error reading EP\n"); | ||
| 355 | #endif | ||
| 356 | return _error; | ||
| 357 | } | ||
| 358 | |||
| 359 | static uint64_t | ||
| 360 | readeo(char *str) | ||
| 361 | { | ||
| 362 | if (*str == '0') | ||
| 363 | return 0ULL; | ||
| 364 | if (*str == '1') | ||
| 365 | return 1ULL; | ||
| 366 | |||
| 367 | #ifdef DEBUG | ||
| 368 | fprintf(stderr, "Error reading EO\n"); | ||
| 369 | #endif | ||
| 370 | return _error; | ||
| 371 | } | ||
| 372 | |||
| 373 | static uint64_t | ||
| 374 | readcp(char *str) | ||
| 375 | { | ||
| 376 | uint64_t c; | ||
| 377 | |||
| 378 | for (c = 0; c < 8; c++) | ||
| 379 | if (!strncmp(str, cornerstr[c], 3) || | ||
| 380 | !strncmp(str, cornerstralt[c], 3)) | ||
| 381 | return c; | ||
| 382 | |||
| 383 | #ifdef DEBUG | ||
| 384 | fprintf(stderr, "Error reading CP\n"); | ||
| 385 | #endif | ||
| 386 | return _error; | ||
| 387 | } | ||
| 388 | |||
| 389 | static uint64_t | ||
| 390 | readco(char *str) | ||
| 391 | { | ||
| 392 | if (*str == '0') | ||
| 393 | return 0ULL; | ||
| 394 | if (*str == '1') | ||
| 395 | return 1ULL; | ||
| 396 | if (*str == '2') | ||
| 397 | return 2ULL; | ||
| 398 | |||
| 399 | #ifdef DEBUG | ||
| 400 | fprintf(stderr, "Error reading CO\n"); | ||
| 401 | #endif | ||
| 402 | return _error; | ||
| 403 | } | ||
| 404 | |||
| 405 | cube_t | ||
| 406 | readcube(char *buf) | ||
| 407 | { | ||
| 408 | int i; | ||
| 409 | uint64_t piece, orient; | ||
| 410 | cube_t ret = {0}; | ||
| 411 | char *b = buf; | ||
| 412 | |||
| 413 | for (i = 0; i < 12; i++) { | ||
| 414 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 415 | b++; | ||
| 416 | if ((piece = readep(b)) == _error) | ||
| 417 | goto readcube_error; | ||
| 418 | b += 2; | ||
| 419 | if ((orient = readeo(b)) == _error) | ||
| 420 | goto readcube_error; | ||
| 421 | b++; | ||
| 422 | ret.e |= (piece << (i * 5ULL)) | (orient << (i * 5ULL + 4ULL)); | ||
| 423 | } | ||
| 424 | for (i = 0; i < 8; i++) { | ||
| 425 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 426 | b++; | ||
| 427 | if ((piece = readcp(b)) == _error) | ||
| 428 | goto readcube_error; | ||
| 429 | b += 3; | ||
| 430 | if ((orient = readco(b)) == _error) | ||
| 431 | goto readcube_error; | ||
| 432 | b++; | ||
| 433 | ret.c |= (piece << (i * 6ULL)) | (orient << (i * 6ULL + 3ULL)); | ||
| 434 | } | ||
| 435 | |||
| 436 | return ret; | ||
| 437 | |||
| 438 | readcube_error: | ||
| 439 | #ifdef DEBUG | ||
| 440 | fprintf(stderr, "readcube error\n"); | ||
| 441 | #endif | ||
| 442 | return errorcube; | ||
| 443 | } | ||
| 444 | |||
| 445 | void | ||
| 446 | writecube(cube_t cube, char *buf) | ||
| 447 | { | ||
| 448 | char *errormsg; | ||
| 449 | uint64_t piece, orien, x; | ||
| 450 | size_t len; | ||
| 451 | int i; | ||
| 452 | |||
| 453 | if (!isconsistent(cube)) { | ||
| 454 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 455 | goto writecube_error; | ||
| 456 | } | ||
| 457 | |||
| 458 | for (i = 0, x = cube.e; i < 12; i++, x >>= 5ULL) { | ||
| 459 | piece = x & _epblock; | ||
| 460 | orien = (x & _eoblock) >> 4ULL; | ||
| 461 | buf[4*i ] = edgestr[piece][0]; | ||
| 462 | buf[4*i + 1] = edgestr[piece][1]; | ||
| 463 | buf[4*i + 2] = orien + '0'; | ||
| 464 | buf[4*i + 3] = ' '; | ||
| 465 | } | ||
| 466 | for (i = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 467 | piece = x & _cpblock; | ||
| 468 | orien = (x & _coblock) >> 3ULL; | ||
| 469 | buf[48 + 5*i ] = cornerstr[piece][0]; | ||
| 470 | buf[48 + 5*i + 1] = cornerstr[piece][1]; | ||
| 471 | buf[48 + 5*i + 2] = cornerstr[piece][2]; | ||
| 472 | buf[48 + 5*i + 3] = orien + '0'; | ||
| 473 | buf[48 + 5*i + 4] = ' '; | ||
| 474 | } | ||
| 475 | |||
| 476 | buf[48+39] = '\0'; | ||
| 477 | |||
| 478 | return; | ||
| 479 | |||
| 480 | writecube_error: | ||
| 481 | #ifdef DEBUG | ||
| 482 | fprintf(stderr, "writecube error, see stdout for details\n"); | ||
| 483 | #endif | ||
| 484 | len = strlen(errormsg); | ||
| 485 | memcpy(buf, errormsg, len); | ||
| 486 | buf[len] = '\n'; | ||
| 487 | buf[len+1] = '\0'; | ||
| 488 | } | ||
| 489 | |||
| 490 | |||
| 491 | static uint64_t | ||
| 492 | readmove(char c) | ||
| 493 | { | ||
| 494 | switch (c) { | ||
| 495 | case 'U': | ||
| 496 | return U; | ||
| 497 | case 'D': | ||
| 498 | return D; | ||
| 499 | case 'R': | ||
| 500 | return R; | ||
| 501 | case 'L': | ||
| 502 | return L; | ||
| 503 | case 'F': | ||
| 504 | return F; | ||
| 505 | case 'B': | ||
| 506 | return B; | ||
| 507 | default: | ||
| 508 | return _error; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | static uint64_t | ||
| 513 | readmodifier(char c) | ||
| 514 | { | ||
| 515 | switch (c) { | ||
| 516 | case '1': /* Fallthrough */ | ||
| 517 | case '2': /* Fallthrough */ | ||
| 518 | case '3': | ||
| 519 | return c - '0' - 1; | ||
| 520 | case '\'': | ||
| 521 | return 2; | ||
| 522 | default: | ||
| 523 | return 0; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | int | ||
| 528 | readmoves(char *buf, move_t *m) | ||
| 529 | { | ||
| 530 | int n; | ||
| 531 | uint64_t r; | ||
| 532 | char *b; | ||
| 533 | |||
| 534 | for (b = buf, n = 0; *b != '\0'; b++) { | ||
| 535 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 536 | b++; | ||
| 537 | if (*b == '\0') | ||
| 538 | return n; | ||
| 539 | if ((r = readmove(*b)) == _error) | ||
| 540 | goto readmoves_error; | ||
| 541 | m[n] = (move_t)r; | ||
| 542 | if ((r = readmodifier(*(b+1))) != 0) { | ||
| 543 | b++; | ||
| 544 | m[n] += r; | ||
| 545 | } | ||
| 546 | n++; | ||
| 547 | } | ||
| 548 | |||
| 549 | return n; | ||
| 550 | |||
| 551 | readmoves_error: | ||
| 552 | #ifdef DEBUG | ||
| 553 | fprintf(stderr, "readmoves error\n"); | ||
| 554 | #endif | ||
| 555 | return -1; | ||
| 556 | } | ||
| 557 | |||
| 558 | void | ||
| 559 | writemoves(move_t *m, int n, char *buf) | ||
| 560 | { | ||
| 561 | int i; | ||
| 562 | size_t len; | ||
| 563 | char *b, *s; | ||
| 564 | |||
| 565 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 566 | s = movestr[m[i]]; | ||
| 567 | len = strlen(s); | ||
| 568 | memcpy(b, s, len); | ||
| 569 | b += len; | ||
| 570 | *b = ' '; | ||
| 571 | } | ||
| 572 | *b = '\0'; | ||
| 573 | } | ||
| 574 | |||
| 575 | |||
| 576 | static inline uint64_t | ||
| 577 | coapply(uint64_t c, uint64_t m) | ||
| 578 | { | ||
| 579 | uint64_t z, b; | ||
| 580 | |||
| 581 | z = c + m; | ||
| 582 | b = ((z + _coonemask) & _coextramask) >> 2ULL; | ||
| 583 | |||
| 584 | return (z + b) & ~_coextramask; | ||
| 585 | } | ||
| 586 | |||
| 587 | cube_t | ||
| 588 | move(cube_t c, move_t m) | ||
| 589 | { | ||
| 590 | cube_t ret = {0}; | ||
| 591 | |||
| 592 | #ifdef DEBUG | ||
| 593 | if (!isconsistent(c)) { | ||
| 594 | fprintf(stderr, "move error, inconsistent cube\n"); | ||
| 595 | goto move_error; | ||
| 596 | } | ||
| 597 | #endif | ||
| 598 | |||
| 599 | switch (m) { | ||
| 600 | case U: | ||
| 601 | ret.e = c.e & ~_emask_u; | ||
| 602 | ret.e |= | ||
| 603 | (c.e & _eblock) << 25ULL | | ||
| 604 | (c.e & _eblock << 5ULL) << 15ULL | | ||
| 605 | (c.e & _eblock2 << 20ULL) >> 20ULL; | ||
| 606 | |||
| 607 | ret.c = c.c & ~_cmask_u; | ||
| 608 | ret.c |= | ||
| 609 | (c.c & _cblock2) << 24ULL | | ||
| 610 | (c.c & _cblock << 24ULL) >> 18ULL | | ||
| 611 | (c.c & _cblock << 30ULL) >> 30ULL; | ||
| 612 | |||
| 613 | return ret; | ||
| 614 | case U2: | ||
| 615 | ret.e = c.e & ~_emask_u; | ||
| 616 | ret.e |= | ||
| 617 | (c.e & (_eblock | _eblock << 20ULL)) << 5ULL | | ||
| 618 | (c.e & (_eblock << 5ULL | _eblock << 25ULL)) >> 5ULL; | ||
| 619 | |||
| 620 | ret.c = c.c & ~_cmask_u; | ||
| 621 | ret.c |= | ||
| 622 | (c.c & (_cblock | _cblock << 24ULL)) << 6ULL | | ||
| 623 | (c.c & (_cblock << 6ULL | _cblock << 30ULL)) >> 6ULL; | ||
| 624 | |||
| 625 | return ret; | ||
| 626 | case U3: | ||
| 627 | ret.e = c.e & ~_emask_u; | ||
| 628 | ret.e |= | ||
| 629 | (c.e & _eblock2) << 20ULL | | ||
| 630 | (c.e & _eblock << 25ULL) >> 25ULL | | ||
| 631 | (c.e & _eblock << 20ULL) >> 15ULL; | ||
| 632 | |||
| 633 | ret.c = c.c & ~_cmask_u; | ||
| 634 | ret.c |= | ||
| 635 | (c.c & _cblock) << 30ULL | | ||
| 636 | (c.c & _cblock << 6ULL) << 18ULL | | ||
| 637 | (c.c & _cblock2 << 24ULL) >> 24ULL; | ||
| 638 | |||
| 639 | return ret; | ||
| 640 | case D: | ||
| 641 | ret.e = c.e & ~_emask_d; | ||
| 642 | ret.e |= | ||
| 643 | (c.e & _eblock2 << 10ULL) << 20ULL | | ||
| 644 | (c.e & _eblock << 30ULL) >> 15ULL | | ||
| 645 | (c.e & _eblock << 35ULL) >> 25ULL; | ||
| 646 | |||
| 647 | ret.c = c.c & ~_cmask_d; | ||
| 648 | ret.c |= | ||
| 649 | (c.c & _cblock2 << 12ULL) << 24ULL | | ||
| 650 | (c.c & _cblock << 36ULL) >> 18ULL | | ||
| 651 | (c.c & _cblock << 42ULL) >> 30ULL; | ||
| 652 | |||
| 653 | return ret; | ||
| 654 | case D2: | ||
| 655 | ret.e = c.e & ~_emask_d; | ||
| 656 | ret.e |= | ||
| 657 | (c.e & (_eblock << 10ULL | _eblock << 30ULL)) << 5ULL | | ||
| 658 | (c.e & (_eblock << 15ULL | _eblock << 35ULL)) >> 5ULL; | ||
| 659 | |||
| 660 | ret.c = c.c & ~_cmask_d; | ||
| 661 | ret.c |= | ||
| 662 | (c.c & (_cblock << 12ULL | _cblock << 36ULL)) << 6ULL | | ||
| 663 | (c.c & (_cblock << 18ULL | _cblock << 42ULL)) >> 6ULL; | ||
| 664 | |||
| 665 | return ret; | ||
| 666 | case D3: | ||
| 667 | ret.e = c.e & ~_emask_d; | ||
| 668 | ret.e |= | ||
| 669 | (c.e & _eblock << 10ULL) << 25ULL | | ||
| 670 | (c.e & _eblock << 15ULL) << 15ULL | | ||
| 671 | (c.e & _eblock2 << 30ULL) >> 20ULL; | ||
| 672 | |||
| 673 | ret.c = c.c & ~_cmask_d; | ||
| 674 | ret.c |= | ||
| 675 | (c.c & _cblock << 12ULL) << 30ULL | | ||
| 676 | (c.c & _cblock << 18ULL) << 18ULL | | ||
| 677 | (c.c & _cblock2 << 36ULL) >> 24ULL; | ||
| 678 | |||
| 679 | return ret; | ||
| 680 | case R: | ||
| 681 | ret.e = c.e & ~_emask_r; | ||
| 682 | ret.e |= | ||
| 683 | (c.e & _eblock << 20ULL) << 35ULL | | ||
| 684 | (c.e & _eblock << 55ULL) >> 20ULL | | ||
| 685 | (c.e & _eblock << 35ULL) << 5ULL | | ||
| 686 | (c.e & _eblock << 40ULL) >> 20ULL; | ||
| 687 | |||
| 688 | ret.c = c.c & ~_cmask_r; | ||
| 689 | ret.c |= | ||
| 690 | (c.c & _cblock) << 30ULL | | ||
| 691 | (c.c & _cblock << 30ULL) >> 12ULL | | ||
| 692 | (c.c & _cblock << 18ULL) << 18ULL | | ||
| 693 | (c.c & _cblock << 36ULL) >> 36ULL; | ||
| 694 | |||
| 695 | ret.c = coapply(ret.c, _comask_r); | ||
| 696 | |||
| 697 | return ret; | ||
| 698 | case R2: | ||
| 699 | ret.e = c.e & ~_emask_r; | ||
| 700 | ret.e |= | ||
| 701 | (c.e & (_eblock << 20ULL | _eblock << 40ULL)) << 15ULL | | ||
| 702 | (c.e & (_eblock << 35ULL | _eblock << 55ULL)) >> 15ULL; | ||
| 703 | |||
| 704 | ret.c = c.c & ~_cmask_r; | ||
| 705 | ret.c |= | ||
| 706 | (c.c & _cblock) << 18ULL | | ||
| 707 | (c.c & _cblock << 18ULL) >> 18ULL | | ||
| 708 | (c.c & _cblock << 30ULL) << 6ULL | | ||
| 709 | (c.c & _cblock << 36ULL) >> 6ULL; | ||
| 710 | |||
| 711 | return ret; | ||
| 712 | case R3: | ||
| 713 | ret.e = c.e & ~_emask_r; | ||
| 714 | ret.e |= | ||
| 715 | (c.e & (_eblock << 20ULL | _eblock << 35ULL)) << 20ULL | | ||
| 716 | (c.e & _eblock << 55ULL) >> 35ULL | | ||
| 717 | (c.e & _eblock << 40ULL) >> 5ULL; | ||
| 718 | |||
| 719 | ret.c = c.c & ~_cmask_r; | ||
| 720 | ret.c |= | ||
| 721 | (c.c & _cblock) << 36ULL | | ||
| 722 | (c.c & _cblock << 30ULL) >> 30ULL | | ||
| 723 | (c.c & _cblock << 18ULL) << 12ULL | | ||
| 724 | (c.c & _cblock << 36ULL) >> 18ULL; | ||
| 725 | |||
| 726 | ret.c = coapply(ret.c, _comask_r); | ||
| 727 | |||
| 728 | return ret; | ||
| 729 | case L: | ||
| 730 | ret.e = c.e & ~_emask_l; | ||
| 731 | ret.e |= | ||
| 732 | (c.e & _eblock2 << 25ULL) << 20ULL | | ||
| 733 | (c.e & _eblock << 45ULL) >> 15ULL | | ||
| 734 | (c.e & _eblock << 50ULL) >> 25ULL; | ||
| 735 | |||
| 736 | ret.c = c.c & ~_cmask_l; | ||
| 737 | ret.c |= | ||
| 738 | (c.c & _cblock << 6ULL) << 18ULL | | ||
| 739 | (c.c & _cblock << 12ULL) << 30ULL | | ||
| 740 | (c.c & _cblock << 24ULL) >> 12ULL | | ||
| 741 | (c.c & _cblock << 42ULL) >> 36ULL; | ||
| 742 | |||
| 743 | ret.c = coapply(ret.c, _comask_l); | ||
| 744 | |||
| 745 | return ret; | ||
| 746 | case L2: | ||
| 747 | ret.e = c.e & ~_emask_l; | ||
| 748 | ret.e |= | ||
| 749 | (c.e & (_eblock << 25ULL | _eblock << 45ULL)) << 5ULL | | ||
| 750 | (c.e & (_eblock << 30ULL | _eblock << 50ULL)) >> 5ULL; | ||
| 751 | |||
| 752 | ret.c = c.c & ~_cmask_l; | ||
| 753 | ret.c |= | ||
| 754 | (c.c & _cblock << 6ULL) << 6ULL | | ||
| 755 | (c.c & _cblock << 12ULL) >> 6ULL | | ||
| 756 | (c.c & _cblock << 24ULL) << 18ULL | | ||
| 757 | (c.c & _cblock << 42ULL) >> 18ULL; | ||
| 758 | |||
| 759 | return ret; | ||
| 760 | case L3: | ||
| 761 | ret.e = c.e & ~_emask_l; | ||
| 762 | ret.e |= | ||
| 763 | (c.e & _eblock << 25ULL) << 25ULL | | ||
| 764 | (c.e & _eblock << 30ULL) << 15ULL | | ||
| 765 | (c.e & _eblock2 << 45ULL) >> 20ULL; | ||
| 766 | |||
| 767 | ret.c = c.c & ~_cmask_l; | ||
| 768 | ret.c |= | ||
| 769 | (c.c & _cblock << 6ULL) << 36ULL | | ||
| 770 | (c.c & _cblock << 12ULL) << 12ULL | | ||
| 771 | (c.c & _cblock << 24ULL) >> 18ULL | | ||
| 772 | (c.c & _cblock << 42ULL) >> 30ULL; | ||
| 773 | |||
| 774 | ret.c = coapply(ret.c, _comask_l); | ||
| 775 | |||
| 776 | return ret; | ||
| 777 | case F: | ||
| 778 | ret.e = c.e & ~_emask_f; | ||
| 779 | ret.e |= | ||
| 780 | (c.e & _eblock) << 40ULL | | ||
| 781 | (c.e & _eblock << 15ULL) << 30ULL | | ||
| 782 | (c.e & _eblock << 40ULL) >> 25ULL | | ||
| 783 | (c.e & _eblock << 45ULL) >> 45ULL; | ||
| 784 | |||
| 785 | ret.e ^= _eomask_f; | ||
| 786 | |||
| 787 | ret.c = c.c & ~_cmask_f; | ||
| 788 | ret.c |= | ||
| 789 | (c.c & _cblock) << 36ULL | | ||
| 790 | (c.c & (_cblock << 24ULL | _cblock << 36ULL)) >> 24ULL | | ||
| 791 | (c.c & _cblock << 12ULL) << 12ULL; | ||
| 792 | |||
| 793 | ret.c = coapply(ret.c, _comask_f); | ||
| 794 | |||
| 795 | return ret; | ||
| 796 | case F2: | ||
| 797 | ret.e = c.e & ~_emask_f; | ||
| 798 | ret.e |= | ||
| 799 | (c.e & _eblock) << 15ULL | | ||
| 800 | (c.e & _eblock << 15ULL) >> 15ULL | | ||
| 801 | (c.e & _eblock << 40ULL) << 5ULL | | ||
| 802 | (c.e & _eblock << 45ULL) >> 5ULL; | ||
| 803 | |||
| 804 | ret.c = c.c & ~_cmask_f; | ||
| 805 | ret.c |= | ||
| 806 | (c.c & (_cblock | _cblock << 24ULL)) << 12ULL | | ||
| 807 | (c.c & (_cblock << 12ULL | _cblock << 36ULL)) >> 12ULL; | ||
| 808 | |||
| 809 | return ret; | ||
| 810 | case F3: | ||
| 811 | ret.e = c.e & ~_emask_f; | ||
| 812 | ret.e |= | ||
| 813 | (c.e & _eblock) << 45ULL | | ||
| 814 | (c.e & _eblock << 15ULL) << 25ULL | | ||
| 815 | (c.e & _eblock << 40ULL) >> 40ULL | | ||
| 816 | (c.e & _eblock << 45ULL) >> 30ULL; | ||
| 817 | |||
| 818 | ret.e ^= _eomask_f; | ||
| 819 | |||
| 820 | ret.c = c.c & ~_cmask_f; | ||
| 821 | ret.c |= | ||
| 822 | (c.c & (_cblock | _cblock << 12ULL)) << 24ULL | | ||
| 823 | (c.c & _cblock << 24ULL) >> 12ULL | | ||
| 824 | (c.c & _cblock << 36ULL) >> 36ULL; | ||
| 825 | |||
| 826 | ret.c = coapply(ret.c, _comask_f); | ||
| 827 | |||
| 828 | return ret; | ||
| 829 | case B: | ||
| 830 | ret.e = c.e & ~_emask_b; | ||
| 831 | ret.e |= | ||
| 832 | (c.e & _eblock2 << 5ULL) << 45ULL | | ||
| 833 | (c.e & _eblock << 50ULL) >> 40ULL | | ||
| 834 | (c.e & _eblock << 55ULL) >> 50ULL; | ||
| 835 | |||
| 836 | ret.e ^= _eomask_b; | ||
| 837 | |||
| 838 | ret.c = c.c & ~_cmask_b; | ||
| 839 | ret.c |= | ||
| 840 | (c.c & _cblock << 6ULL) << 36ULL | | ||
| 841 | (c.c & _cblock << 18ULL) << 12ULL | | ||
| 842 | (c.c & (_cblock << 30ULL | _cblock << 42ULL)) >> 24ULL; | ||
| 843 | |||
| 844 | ret.c = coapply(ret.c, _comask_b); | ||
| 845 | |||
| 846 | return ret; | ||
| 847 | case B2: | ||
| 848 | ret.e = c.e & ~_emask_b; | ||
| 849 | ret.e |= | ||
| 850 | (c.e & (_eblock << 5ULL | _eblock << 50ULL)) << 5ULL | | ||
| 851 | (c.e & (_eblock << 10ULL | _eblock << 55ULL)) >> 5ULL; | ||
| 852 | |||
| 853 | ret.c = c.c & ~_cmask_b; | ||
| 854 | ret.c |= | ||
| 855 | (c.c & (_cblock << 6ULL | _cblock << 30ULL)) << 12ULL | | ||
| 856 | (c.c & (_cblock << 18ULL | _cblock << 42ULL)) >> 12ULL; | ||
| 857 | |||
| 858 | return ret; | ||
| 859 | case B3: | ||
| 860 | ret.e = c.e & ~_emask_b; | ||
| 861 | ret.e |= | ||
| 862 | (c.e & _eblock << 5ULL) << 50ULL | | ||
| 863 | (c.e & _eblock << 10ULL) << 40ULL | | ||
| 864 | (c.e & _eblock2 << 50ULL) >> 45ULL; | ||
| 865 | |||
| 866 | ret.e ^= _eomask_b; | ||
| 867 | |||
| 868 | ret.c = c.c & ~_cmask_b; | ||
| 869 | ret.c |= | ||
| 870 | (c.c & (_cblock << 6ULL | _cblock << 18ULL)) << 24ULL | | ||
| 871 | (c.c & _cblock << 30ULL) >> 12ULL | | ||
| 872 | (c.c & _cblock << 42ULL) >> 36ULL; | ||
| 873 | |||
| 874 | ret.c = coapply(ret.c, _comask_b); | ||
| 875 | |||
| 876 | return ret; | ||
| 877 | default: | ||
| 878 | goto move_unknown; | ||
| 879 | } | ||
| 880 | |||
| 881 | move_unknown: | ||
| 882 | #ifdef DEBUG | ||
| 883 | fprintf(stderr, "move error, unknow move\n"); | ||
| 884 | #endif | ||
| 885 | goto move_error; | ||
| 886 | move_error: | ||
| 887 | return errorcube; | ||
| 888 | } | ||
diff --git a/src/cube.h b/src/cube.h new file mode 100644 index 0000000..b916f96 --- /dev/null +++ b/src/cube.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | typedef enum { | ||
| 2 | U, U2, U3, D, D2, D3, | ||
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | typedef struct { | ||
| 7 | uint64_t e; | ||
| 8 | uint64_t c; | ||
| 9 | } cube_t; | ||
| 10 | |||
| 11 | extern cube_t solvedcube; | ||
| 12 | extern cube_t errorcube; | ||
| 13 | |||
| 14 | bool isconsistent(cube_t); | ||
| 15 | bool issolved(cube_t); | ||
| 16 | |||
| 17 | cube_t readcube(char *); | ||
| 18 | void writecube(cube_t, char *); | ||
| 19 | |||
| 20 | int readmoves(char *, move_t *); | ||
| 21 | void writemoves(move_t *, int, char *); | ||
| 22 | |||
| 23 | cube_t move(cube_t, move_t); | ||
| 24 | |||
| 25 | /* | ||
| 26 | cube_t inverse(cube_t); | ||
| 27 | */ | ||
diff --git a/src/cube.sync-conflict-20230524-181836-JOKKFPA.c b/src/cube.sync-conflict-20230524-181836-JOKKFPA.c new file mode 100644 index 0000000..e64acca --- /dev/null +++ b/src/cube.sync-conflict-20230524-181836-JOKKFPA.c | |||
| @@ -0,0 +1,614 @@ | |||
| 1 | /* | ||
| 2 | # Cube representation, moves, transformations and indexing | ||
| 3 | |||
| 4 | ## String description | ||
| 5 | |||
| 6 | The functions readcube() and writecube() use the following format. | ||
| 7 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 8 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 9 | Similarly, each corner is represented by three letters and a number | ||
| 10 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 11 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 12 | relative to the U / D axis. | ||
| 13 | |||
| 14 | The correct order of the pieces is the same as that defined in the | ||
| 15 | section "Internal cube representation", except that pieces are read | ||
| 16 | left-to-right. Pieces are divided by slices, so the ordering is not the | ||
| 17 | most intuitive, but it is more convenient for the internal representation. | ||
| 18 | |||
| 19 | Whitespaces between pieces are ignored when reading the cube, and a | ||
| 20 | single whitespace character is added between pieces when writing. | ||
| 21 | |||
| 22 | For example, the solved cube looks like this: | ||
| 23 | |||
| 24 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 25 | |||
| 26 | The cube after the moves R'U'F looks like this: | ||
| 27 | |||
| 28 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 29 | |||
| 30 | More formats might be supported in the future. | ||
| 31 | |||
| 32 | ## Internal cube representation | ||
| 33 | |||
| 34 | The cube_t data structure implemented in this file is designed to | ||
| 35 | efficiently perform common operations on a 3x3x3 Rubik's cube when | ||
| 36 | solving it with an iterative-deepening DFS search. It is not the most | ||
| 37 | general, complete, easy to read or compact one. Since the cube can | ||
| 38 | be trivially reoriented before the search, we only encode permutations | ||
| 39 | of the cube that keep the center pieces in a fixed position (that is, | ||
| 40 | we do not encode the position of the centers). | ||
| 41 | |||
| 42 | The cube state is encoded in two 64-bit integers, one for edges and one | ||
| 43 | for centers. We explain how edges are encoded first, and the highlight | ||
| 44 | the few differences with corners afterwards. | ||
| 45 | |||
| 46 | For encoding edges, only the 60 least-significant bits are used. Each | ||
| 47 | edge described by 5 bits. The position of a 5-bit block in the 64-bit | ||
| 48 | integer determine the position of the edge piece in the cube, according | ||
| 49 | to the following table (least-significant bits on the right): | ||
| 50 | |||
| 51 | 55-59 50-54 45-49 40-44 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 52 | BR BL FL FR DR DL UL UR DF DB UB UF | ||
| 53 | ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee | ||
| 54 | |||
| 55 | For each edge, the 4 least-significant bits ('ssee' in the table) | ||
| 56 | determine the piece. The two bits marked with 'ss' determine the internal | ||
| 57 | slice the piece belongs to, i.e. they are either '00' for M, '01' for | ||
| 58 | S or '10' for E. The other two bits (marked with 'ee') determine the | ||
| 59 | actual edge piece among the 4 in the same slice, and they are assigned | ||
| 60 | somewhat arbitarily. Using this representation and the ordering defined | ||
| 61 | in the table above, the edges are correctly permuted when these 4 bits | ||
| 62 | for each represent the numbers 0 to 11 in the correct order. | ||
| 63 | |||
| 64 | The last bit determines the orientation. The orientation of an edge | ||
| 65 | depends on its position, and it is defined being 0 if the edge can be | ||
| 66 | moved to its place in the solved orientation by permutations in the | ||
| 67 | subgroup <U, D, R, L, F2, B2>. | ||
| 68 | |||
| 69 | Corners are encoded in the 48 least-significant bits, and are described | ||
| 70 | by 6 bits each, their position being defined by the following table: | ||
| 71 | |||
| 72 | 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 73 | DBL DFL UBR UFL DBR DFL UBL UFR | ||
| 74 | oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc | ||
| 75 | |||
| 76 | The bit marked with an 'x' describes the axis the corner belongs to. | ||
| 77 | The 0 axis consists of the corners UFR, UBL, DFL and DBR, and the other | ||
| 78 | four corners form the axis marked with 1. Then two bits are needed to | ||
| 79 | identify the corner among the four of the same axis. The last three bits | ||
| 80 | determine the orientation, where one corner is defined to be oriented | ||
| 81 | (marked with '000') if its top or bottom sticker faces the top or bottom | ||
| 82 | side. A corner a clockwise turn away from being oriented, thus requiring | ||
| 83 | a counter-clockwise turn to be oriented correctly, is marked with '001', | ||
| 84 | and a corner a counter-clockwise turn away is marked with '010'. The most | ||
| 85 | significant bit is not used to determine the corner orientation, but it | ||
| 86 | must always be set to '0' to simplify the moving operations (see below). | ||
| 87 | |||
| 88 | ## Basic moves | ||
| 89 | |||
| 90 | The 18 basic moves of the cube could be performed by applying a suitable | ||
| 91 | general permutation (see below), but they have instead been manually | ||
| 92 | implemented with a few simple operations each, to improve performance. | ||
| 93 | |||
| 94 | For each move we first permute the pieces. This amounts to shifting | ||
| 95 | around 4 blocks of bits for edges and 4 for corners. Since in some cases | ||
| 96 | adjacent pieces on the cube are also adjacent in the bit representation we | ||
| 97 | use, we can save some operations by shifting multiple blocks together. | ||
| 98 | For example, for the move U for edges we shift a block of 15 bits 5 | ||
| 99 | positions to the left and a block of 5 bits 15 positions to the right. | ||
| 100 | |||
| 101 | There are some moves that change the orientation of the pieces. Namely, | ||
| 102 | the moves F, F', B and B' change the orientation of the edges and those | ||
| 103 | moves as well as R, R', L and L' change the orientation of the corners. | ||
| 104 | Edge orientation is easy to address: we simply xor the edge representation | ||
| 105 | by a bit mask with zeroes everywhere except for the 4 edges that need | ||
| 106 | to be flipped (i.e. the ones on the twisted face). | ||
| 107 | |||
| 108 | Corner orientation is harder to reproduce efficiently working only with | ||
| 109 | bitwise operations, as it involves performing operations modulo 3. | ||
| 110 | However, with the help of the extra bit we reserved, we are able to | ||
| 111 | do this using only two additions and 3 bitwise operations, without | ||
| 112 | any multiplication, division or modulo operation. The trick is | ||
| 113 | to use the following formula to sum two numbers x, y in {0,1,2}: | ||
| 114 | |||
| 115 | ((x+y) + (x+y+1)/4) % 4 | ||
| 116 | |||
| 117 | The thrid bit is needed because x+y and x+y+1 can exceed 3. | ||
| 118 | See below (in the code) for the details. | ||
| 119 | |||
| 120 | ## Inverting the cube | ||
| 121 | |||
| 122 | TODO | ||
| 123 | |||
| 124 | ## Transformations (conjugations by full-cube rotations) | ||
| 125 | |||
| 126 | TODO | ||
| 127 | |||
| 128 | ## Indexing | ||
| 129 | |||
| 130 | TODO - subgroup description etc | ||
| 131 | |||
| 132 | Ideas for pruning (for another file?): | ||
| 133 | - Use corner separation + CO as main coordinate (~150k states) | ||
| 134 | - for huge tables, htr corners can be used (6 times larger) | ||
| 135 | - Symmetry table, one entry or each main coordinate value with the | ||
| 136 | following info: | ||
| 137 | - index of the corresponding main symcoord (13 bits) | ||
| 138 | - transtorep (6 bits) | ||
| 139 | - base value for pruning table (5 bits, probably 4) | ||
| 140 | - pruning value for only main coord, i.e. fallback (4 bits) | ||
| 141 | - To get a full coord for the cube: | ||
| 142 | - get first coord c, get the transtorep | ||
| 143 | - transform edges with transtorep | ||
| 144 | - get second coordinate e | ||
| 145 | - return c * MAXE + e | ||
| 146 | - This is still too big, so divide by a power of 2 to get the hashed index | ||
| 147 | - second coordinate: ep always, + varible number of eo bit (0 to 11) | ||
| 148 | - Generate table: | ||
| 149 | - first probe for base value: | ||
| 150 | - solve coord using fallback table for pruning for 10k random states or so | ||
| 151 | - loop over all possible values (even if going for smaller table) | ||
| 152 | - with inverse-index strategy or what? | ||
| 153 | - use 1 bit per entry (more than base value or not) | ||
| 154 | */ | ||
| 155 | |||
| 156 | #include <stdbool.h> | ||
| 157 | #include <stdint.h> | ||
| 158 | #include <string.h> | ||
| 159 | |||
| 160 | #ifdef DEBUG | ||
| 161 | #include <stdio.h> | ||
| 162 | #endif | ||
| 163 | |||
| 164 | #include "cube.h" | ||
| 165 | |||
| 166 | #define _error 0xFFFFFFFF | ||
| 167 | |||
| 168 | #define _esize 5ULL | ||
| 169 | #define _eoblock 0x10ULL /* 10000 */ | ||
| 170 | #define _epblock 0x0FULL /* 01111 */ | ||
| 171 | #define _eblock 0x1FULL /* 11111 */ | ||
| 172 | |||
| 173 | #define _csize 6ULL | ||
| 174 | #define _coblock 0x18ULL /* 011000 */ | ||
| 175 | #define _cpblock 0x07ULL /* 000111 */ | ||
| 176 | #define _cblock 0x3FULL /* 111111 */ | ||
| 177 | |||
| 178 | #define _edge_uf 0ULL /* 00 00 */ | ||
| 179 | #define _edge_ub 1ULL /* 00 01 */ | ||
| 180 | #define _edge_db 2ULL /* 00 10 */ | ||
| 181 | #define _edge_df 3ULL /* 00 11 */ | ||
| 182 | #define _edge_ur 4ULL /* 01 00 */ | ||
| 183 | #define _edge_ul 5ULL /* 01 01 */ | ||
| 184 | #define _edge_dl 6ULL /* 01 10 */ | ||
| 185 | #define _edge_dr 7ULL /* 01 11 */ | ||
| 186 | #define _edge_fr 8ULL /* 10 00 */ | ||
| 187 | #define _edge_fl 9ULL /* 10 01 */ | ||
| 188 | #define _edge_bl 10ULL /* 10 10 */ | ||
| 189 | #define _edge_br 11ULL /* 10 11 */ | ||
| 190 | |||
| 191 | #define _corner_ufr 0ULL /* 0 00 */ | ||
| 192 | #define _corner_ubl 1ULL /* 0 01 */ | ||
| 193 | #define _corner_dfl 2ULL /* 0 10 */ | ||
| 194 | #define _corner_dbr 3ULL /* 0 11 */ | ||
| 195 | #define _corner_ufl 4ULL /* 1 00 */ | ||
| 196 | #define _corner_ubr 5ULL /* 1 01 */ | ||
| 197 | #define _corner_dfr 6ULL /* 1 10 */ | ||
| 198 | #define _corner_dbl 7ULL /* 1 11 */ | ||
| 199 | |||
| 200 | #define ESHIFT(i) ((i) * _esize) | ||
| 201 | #define EOSHIFT(i) (4ULL + (i) * _esize) | ||
| 202 | #define EMASK(i) (_eblock << ESHIFT(i)) | ||
| 203 | #define EOMASK(i) (_eoblock << ESHIFT(i)) | ||
| 204 | #define EPMASK(i) (_epblock << ESHIFT(i)) | ||
| 205 | #define ESOLVED(ee) (_edge_##ee << (_edge_##ee * _esize)) | ||
| 206 | #define EDGEAT(e, i) (((e) & EMASK(i)) >> ESHIFT(i)) | ||
| 207 | #define EOAT(e, i) (((e) & EOMASK(i)) >> EOSHIFT(i)) | ||
| 208 | #define EPAT(e, i) (((e) & EPMASK(i)) >> ESHIFT(i)) | ||
| 209 | |||
| 210 | #define CSHIFT(i) ((i) * _csize) | ||
| 211 | #define COSHIFT(i) (3ULL + (i) * _csize) | ||
| 212 | #define CMASK(i) (_cblock << CSHIFT(i)) | ||
| 213 | #define COMASK(i) (_coblock << CSHIFT(i)) | ||
| 214 | #define CPMASK(i) (_cpblock << CSHIFT(i)) | ||
| 215 | #define CSOLVED(ccc) (_corner_##ccc << (_corner_##ccc * _csize)) | ||
| 216 | #define CORNERAT(c, i) (((c) & CMASK(i)) >> CSHIFT(i)) | ||
| 217 | #define COAT(c, i) (((c) & COMASK(i)) >> COSHIFT(i)) | ||
| 218 | #define CPAT(c, i) (((c) & CPMASK(i)) >> CSHIFT(i)) | ||
| 219 | |||
| 220 | #define _emask_u (EMASK(uf) | EMASK(ul) | EMASK(ub) | EMASK(ur)) | ||
| 221 | #define _emask_d (EMASK(df) | EMASK(dl) | EMASK(db) | EMASK(dr)) | ||
| 222 | #define _emask_r (EMASK(ur) | EMASK(dr) | EMASK(fr) | EMASK(br)) | ||
| 223 | #define _emask_l (EMASK(ul) | EMASK(dl) | EMASK(fl) | EMASK(bl)) | ||
| 224 | #define _emask_f (EMASK(uf) | EMASK(df) | EMASK(fr) | EMASK(fl)) | ||
| 225 | #define _emask_b (EMASK(ub) | EMASK(db) | EMASK(br) | EMASK(bl)) | ||
| 226 | |||
| 227 | #define _cmask_u (CMASK(ufr) | CMASK(ufl) | CMASK(ubl) | CMASK(ubr)) | ||
| 228 | #define _cmask_d (CMASK(dfr) | CMASK(dfl) | CMASK(dbl) | CMASK(dbr)) | ||
| 229 | #define _cmask_r (CMASK(ufr) | CMASK(dfr) | CMASK(ubr) | CMASK(dbr)) | ||
| 230 | #define _cmask_l (CMASK(ufl) | CMASK(dfl) | CMASK(ubl) | CMASK(dbl)) | ||
| 231 | #define _cmask_f (CMASK(ufr) | CMASK(ufl) | CMASK(dfr) | CMASK(dfl)) | ||
| 232 | #define _cmask_b (CMASK(ubr) | CMASK(ubl) | CMASK(dbr) | CMASK(dbl)) | ||
| 233 | |||
| 234 | #define _eomask (EOMASK(uf) | EOMASK(ul) | EOMASK(ub) | EOMASK(ur) \ | ||
| 235 | EOMASK(df) | EOMASK(dl) | EOMASK(db) | EOMASK(dr) \ | ||
| 236 | EOMASK(fr) | EOMASK(fl) | EOMASK(bl) | EOMASK(br)) | ||
| 237 | #define _comask (COMASK(ufr) | COMASK(ufl) | COMASK(ubl) | COMASK(ubr) \ | ||
| 238 | COMASK(dfr) | COMASK(dfl) | COMASK(dbl) | COMASK(dbr)) | ||
| 239 | |||
| 240 | static uint64_t permsign(uint64_t *, int); | ||
| 241 | static uint64_t readep(char *); | ||
| 242 | static uint64_t readeo(char *); | ||
| 243 | static uint64_t readcp(char *); | ||
| 244 | static uint64_t readco(char *); | ||
| 245 | static uint64_t readmove(char); | ||
| 246 | static uint64_t readmodifier(char); | ||
| 247 | |||
| 248 | static char *edgestr[] = { | ||
| 249 | [_edge_uf] = "UF", | ||
| 250 | [_edge_ub] = "UB", | ||
| 251 | [_edge_db] = "DB", | ||
| 252 | [_edge_df] = "DF", | ||
| 253 | [_edge_ur] = "UR", | ||
| 254 | [_edge_ul] = "UL", | ||
| 255 | [_edge_dl] = "DL", | ||
| 256 | [_edge_dr] = "DR", | ||
| 257 | [_edge_fr] = "FR", | ||
| 258 | [_edge_fl] = "FL", | ||
| 259 | [_edge_bl] = "BL", | ||
| 260 | [_edge_br] = "BR" | ||
| 261 | }; | ||
| 262 | static char *cornerstr[] = { | ||
| 263 | [_corner_ufr] = "UFR", | ||
| 264 | [_corner_ubl] = "UBL", | ||
| 265 | [_corner_dfl] = "DFL", | ||
| 266 | [_corner_dbr] = "DBR", | ||
| 267 | [_corner_ufl] = "UFL", | ||
| 268 | [_corner_ubr] = "UBR", | ||
| 269 | [_corner_dfr] = "DFR", | ||
| 270 | [_corner_dbl] = "DBL" | ||
| 271 | }; | ||
| 272 | static char *movestr[] = { | ||
| 273 | [U] = "U", [U2] = "U2", [U3] = "U'", | ||
| 274 | [D] = "D", [D2] = "D2", [D3] = "D'", | ||
| 275 | [R] = "R", [R2] = "R2", [R3] = "R'", | ||
| 276 | [L] = "L", [L2] = "L2", [L3] = "L'", | ||
| 277 | [F] = "F", [F2] = "F2", [F3] = "F'", | ||
| 278 | [B] = "B", [B2] = "B2", [B3] = "B'", | ||
| 279 | }; | ||
| 280 | |||
| 281 | cube_t solvedcube = { | ||
| 282 | .e = ESOLVED(uf) | ESOLVED(ul) | ESOLVED(ub) | ESOLVED(ur) | | ||
| 283 | ESOLVED(df) | ESOLVED(dl) | ESOLVED(db) | ESOLVED(dr) | | ||
| 284 | ESOLVED(fr) | ESOLVED(fl) | ESOLVED(bl) | ESOLVED(br), | ||
| 285 | .c = CSOLVED(ufr) | CSOLVED(ufl) | CSOLVED(ubl) | CSOLVED(ubr) | | ||
| 286 | CSOLVED(dfr) | CSOLVED(dfl) | CSOLVED(dbl) | CSOLVED(dbr), | ||
| 287 | }; | ||
| 288 | cube_t errorcube = { .e = _error, .c = _error }; | ||
| 289 | |||
| 290 | |||
| 291 | static uint64_t | ||
| 292 | permsign(uint64_t *a, int n) | ||
| 293 | { | ||
| 294 | int i, j; | ||
| 295 | uint64_t ret; | ||
| 296 | |||
| 297 | ret = 0; | ||
| 298 | |||
| 299 | for (i = 0; i < n; i++) | ||
| 300 | for (j = i+1; j < n; j++) | ||
| 301 | ret += a[i] > a[j] ? 1 : 0; | ||
| 302 | |||
| 303 | return ret % 2; | ||
| 304 | } | ||
| 305 | |||
| 306 | bool | ||
| 307 | isconsistent(cube_t cube) | ||
| 308 | { | ||
| 309 | uint64_t p[12], sum; | ||
| 310 | bool found[12]; | ||
| 311 | int i; | ||
| 312 | |||
| 313 | sum = 0; | ||
| 314 | |||
| 315 | /* Check for EP consistency */ | ||
| 316 | for (i = 0; i < 12; i++) | ||
| 317 | found[i] = false; | ||
| 318 | for (i = 0; i < 12; i++) { | ||
| 319 | p[i] = EPAT(cube.e, i); | ||
| 320 | found[p[i]] = true; | ||
| 321 | } | ||
| 322 | for (i = 0; i < 12; i++) | ||
| 323 | if (!found[i]) | ||
| 324 | return false; | ||
| 325 | sum = permsign(p, 12); | ||
| 326 | |||
| 327 | /* Check for CP consistency */ | ||
| 328 | for (i = 0; i < 8; i++) | ||
| 329 | found[i] = false; | ||
| 330 | for (i = 0; i < 8; i++) { | ||
| 331 | p[i] = CPAT(cube.c, i); | ||
| 332 | found[p[i]] = true; | ||
| 333 | } | ||
| 334 | for (i = 0; i < 8; i++) | ||
| 335 | if (!found[i]) | ||
| 336 | return false; | ||
| 337 | sum += permsign(p, 8); | ||
| 338 | |||
| 339 | /* Check permutation parity */ | ||
| 340 | if (sum % 2 != 0) | ||
| 341 | return false; | ||
| 342 | |||
| 343 | /* Check for EO parity */ | ||
| 344 | for (i = 0, sum = 0; i < 12; i++) | ||
| 345 | sum += EOAT(cube.e, i); | ||
| 346 | if (sum % 2 != 0) | ||
| 347 | return false; | ||
| 348 | |||
| 349 | /* Check for CO parity */ | ||
| 350 | for (i = 0, sum = 0; i < 8; i++) | ||
| 351 | sum += COAT(cube.c, i); | ||
| 352 | if (sum % 3 != 0) | ||
| 353 | return false; | ||
| 354 | |||
| 355 | /* Check that CO extra bit is zero */ | ||
| 356 | for (i = 0; i < 8; i++) | ||
| 357 | if (cube.c & (1ULL << (5 + i * _csize))) | ||
| 358 | return false; | ||
| 359 | |||
| 360 | return true; | ||
| 361 | } | ||
| 362 | |||
| 363 | bool | ||
| 364 | issolved(cube_t cube) | ||
| 365 | { | ||
| 366 | return cube.c == solvedcube.c && cube.e == solvedcube.e; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | static uint64_t | ||
| 371 | readep(char *str) | ||
| 372 | { | ||
| 373 | if (!strncmp(str, "UF", 2)) | ||
| 374 | return _edge_uf; | ||
| 375 | if (!strncmp(str, "UL", 2)) | ||
| 376 | return _edge_ul; | ||
| 377 | if (!strncmp(str, "UB", 2)) | ||
| 378 | return _edge_ub; | ||
| 379 | if (!strncmp(str, "UR", 2)) | ||
| 380 | return _edge_ur; | ||
| 381 | if (!strncmp(str, "DF", 2)) | ||
| 382 | return _edge_df; | ||
| 383 | if (!strncmp(str, "DL", 2)) | ||
| 384 | return _edge_dl; | ||
| 385 | if (!strncmp(str, "DB", 2)) | ||
| 386 | return _edge_db; | ||
| 387 | if (!strncmp(str, "DR", 2)) | ||
| 388 | return _edge_dr; | ||
| 389 | if (!strncmp(str, "FR", 2)) | ||
| 390 | return _edge_fr; | ||
| 391 | if (!strncmp(str, "FL", 2)) | ||
| 392 | return _edge_fl; | ||
| 393 | if (!strncmp(str, "BL", 2)) | ||
| 394 | return _edge_bl; | ||
| 395 | if (!strncmp(str, "BR", 2)) | ||
| 396 | return _edge_br; | ||
| 397 | |||
| 398 | return _error; | ||
| 399 | } | ||
| 400 | |||
| 401 | static uint64_t | ||
| 402 | readeo(char *str) | ||
| 403 | { | ||
| 404 | if (*str == '0') | ||
| 405 | return 0ULL; | ||
| 406 | if (*str == '1') | ||
| 407 | return 1ULL; | ||
| 408 | |||
| 409 | return _error; | ||
| 410 | } | ||
| 411 | |||
| 412 | static uint64_t | ||
| 413 | readcp(char *str) | ||
| 414 | { | ||
| 415 | if (!strncmp(str, "UFR", 3) || !strncmp(str, "URF", 3)) | ||
| 416 | return _corner_ufr; | ||
| 417 | if (!strncmp(str, "UFL", 3) || !strncmp(str, "ULF", 3)) | ||
| 418 | return _corner_ufl; | ||
| 419 | if (!strncmp(str, "UBL", 3) || !strncmp(str, "ULB", 3)) | ||
| 420 | return _corner_ubl; | ||
| 421 | if (!strncmp(str, "UBR", 3) || !strncmp(str, "URB", 3)) | ||
| 422 | return _corner_ubr; | ||
| 423 | if (!strncmp(str, "DFR", 3) || !strncmp(str, "DRF", 3)) | ||
| 424 | return _corner_dfr; | ||
| 425 | if (!strncmp(str, "DFL", 3) || !strncmp(str, "DLF", 3)) | ||
| 426 | return _corner_dfl; | ||
| 427 | if (!strncmp(str, "DBL", 3) || !strncmp(str, "DLB", 3)) | ||
| 428 | return _corner_dbl; | ||
| 429 | if (!strncmp(str, "DBR", 3) || !strncmp(str, "DRB", 3)) | ||
| 430 | return _corner_dbr; | ||
| 431 | |||
| 432 | return _error; | ||
| 433 | } | ||
| 434 | |||
| 435 | static uint64_t | ||
| 436 | readco(char *str) | ||
| 437 | { | ||
| 438 | if (*str == '0') | ||
| 439 | return 0ULL; | ||
| 440 | if (*str == '1') | ||
| 441 | return 1ULL; | ||
| 442 | if (*str == '2') | ||
| 443 | return 2ULL; | ||
| 444 | |||
| 445 | return _error; | ||
| 446 | } | ||
| 447 | |||
| 448 | cube_t | ||
| 449 | readcube(char *buf) | ||
| 450 | { | ||
| 451 | int i; | ||
| 452 | uint64_t piece, orient; | ||
| 453 | cube_t ret = {0}; | ||
| 454 | char *b = buf; | ||
| 455 | |||
| 456 | for (i = 0; i < 12; i++) { | ||
| 457 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 458 | b++; | ||
| 459 | if ((piece = readep(b)) == _error) | ||
| 460 | goto readcube_error; | ||
| 461 | b += 2; | ||
| 462 | if ((orient = readeo(b)) == _error) | ||
| 463 | goto readcube_error; | ||
| 464 | b++; | ||
| 465 | ret.e |= (piece << ESHIFT(i)) | (orient << EOSHIFT(i)); | ||
| 466 | } | ||
| 467 | for (i = 0; i < 8; i++) { | ||
| 468 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 469 | b++; | ||
| 470 | if ((piece = readcp(b)) == _error) | ||
| 471 | goto readcube_error; | ||
| 472 | b += 3; | ||
| 473 | if ((orient = readco(b)) == _error) | ||
| 474 | goto readcube_error; | ||
| 475 | b++; | ||
| 476 | ret.c |= (piece << CSHIFT(i)) | (orient << COSHIFT(i)); | ||
| 477 | } | ||
| 478 | |||
| 479 | return ret; | ||
| 480 | |||
| 481 | readcube_error: | ||
| 482 | return errorcube; | ||
| 483 | } | ||
| 484 | |||
| 485 | void | ||
| 486 | writecube(cube_t cube, char *buf) | ||
| 487 | { | ||
| 488 | char *errormsg; | ||
| 489 | uint64_t piece; | ||
| 490 | size_t len; | ||
| 491 | int i; | ||
| 492 | |||
| 493 | if (!isconsistent(cube)) { | ||
| 494 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 495 | goto writecube_error; | ||
| 496 | } | ||
| 497 | |||
| 498 | for (i = 0; i < 12; i++) { | ||
| 499 | piece = EPAT(cube.e, i); | ||
| 500 | buf[4*i ] = edgestr[piece][0]; | ||
| 501 | buf[4*i + 1] = edgestr[piece][1]; | ||
| 502 | buf[4*i + 2] = EOAT(cube.e, i) + '0'; | ||
| 503 | buf[4*i + 3] = ' '; | ||
| 504 | } | ||
| 505 | for (i = 0; i < 8; i++) { | ||
| 506 | piece = CPAT(cube.c, i); | ||
| 507 | buf[48 + 5*i ] = cornerstr[piece][0]; | ||
| 508 | buf[48 + 5*i + 1] = cornerstr[piece][1]; | ||
| 509 | buf[48 + 5*i + 2] = cornerstr[piece][2]; | ||
| 510 | buf[48 + 5*i + 3] = COAT(cube.c, i) + '0'; | ||
| 511 | buf[48 + 5*i + 4] = ' '; | ||
| 512 | } | ||
| 513 | |||
| 514 | buf[48+39] = '\0'; | ||
| 515 | |||
| 516 | return; | ||
| 517 | |||
| 518 | writecube_error: | ||
| 519 | len = strlen(errormsg); | ||
| 520 | strcpy(buf, errormsg); | ||
| 521 | buf[len] = '\n'; | ||
| 522 | buf[len+1] = '\0'; | ||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | static uint64_t | ||
| 527 | readmove(char c) | ||
| 528 | { | ||
| 529 | switch (c) { | ||
| 530 | case 'U': | ||
| 531 | return U; | ||
| 532 | case 'D': | ||
| 533 | return D; | ||
| 534 | case 'R': | ||
| 535 | return R; | ||
| 536 | case 'L': | ||
| 537 | return L; | ||
| 538 | case 'F': | ||
| 539 | return F; | ||
| 540 | case 'B': | ||
| 541 | return B; | ||
| 542 | default: | ||
| 543 | return _error; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | static uint64_t | ||
| 548 | readmodifier(char c) | ||
| 549 | { | ||
| 550 | switch (c) { | ||
| 551 | case '1': /* Fallthrough */ | ||
| 552 | case '2': /* Fallthrough */ | ||
| 553 | case '3': | ||
| 554 | return c - '0' - 1; | ||
| 555 | case '\'': | ||
| 556 | return 2; | ||
| 557 | default: | ||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | int | ||
| 563 | readmoves(char *buf, move_t *m) | ||
| 564 | { | ||
| 565 | int n; | ||
| 566 | uint64_t r; | ||
| 567 | char *b; | ||
| 568 | |||
| 569 | for (b = buf, n = 0; *b != '\0'; b++) { | ||
| 570 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 571 | b++; | ||
| 572 | if ((r = readmove(*b)) == _error) | ||
| 573 | return -1; | ||
| 574 | m[n] = (move_t)r; | ||
| 575 | if ((r = readmodifier(*(b+1))) != 0) { | ||
| 576 | b++; | ||
| 577 | m[n] += r; | ||
| 578 | } | ||
| 579 | n++; | ||
| 580 | } | ||
| 581 | |||
| 582 | return n; | ||
| 583 | } | ||
| 584 | |||
| 585 | void | ||
| 586 | writemoves(move_t *m, int n, char *buf) | ||
| 587 | { | ||
| 588 | int i; | ||
| 589 | char *b, *s; | ||
| 590 | |||
| 591 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 592 | s = movestr[m[i]]; | ||
| 593 | strcpy(b, s); | ||
| 594 | b += strlen(s); | ||
| 595 | *b = ' '; | ||
| 596 | } | ||
| 597 | *b = '\0'; | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | cube_t | ||
| 602 | move(move_t m, cube_t c) | ||
| 603 | { | ||
| 604 | /* TODO - not implemented yet */ | ||
| 605 | |||
| 606 | cube_t ret = {0}; | ||
| 607 | |||
| 608 | switch (m) { | ||
| 609 | case U: | ||
| 610 | return ret; | ||
| 611 | default: | ||
| 612 | return ret; | ||
| 613 | } | ||
| 614 | } | ||
diff --git a/src/cube.sync-conflict-20230524-181836-JOKKFPA.h b/src/cube.sync-conflict-20230524-181836-JOKKFPA.h new file mode 100644 index 0000000..2763d16 --- /dev/null +++ b/src/cube.sync-conflict-20230524-181836-JOKKFPA.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | typedef enum { | ||
| 2 | U, U2, U3, D, D2, D3, | ||
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | typedef struct { | ||
| 7 | uint64_t e; | ||
| 8 | uint64_t c; | ||
| 9 | } cube_t; | ||
| 10 | |||
| 11 | extern cube_t solvedcube; | ||
| 12 | extern cube_t errorcube; | ||
| 13 | |||
| 14 | bool isconsistent(cube_t); | ||
| 15 | bool issolved(cube_t); | ||
| 16 | |||
| 17 | cube_t readcube(char *); | ||
| 18 | void writecube(cube_t, char *); | ||
| 19 | |||
| 20 | int readmoves(char *, move_t *); | ||
| 21 | void writemoves(move_t *, int, char *); | ||
| 22 | |||
| 23 | /* | ||
| 24 | cube_t move(move_t, cube_t); | ||
| 25 | cube_t inverse(cube_t); | ||
| 26 | |||
| 27 | */ | ||
diff --git a/src/cube.sync-conflict-20230524-182143-JOKKFPA.c b/src/cube.sync-conflict-20230524-182143-JOKKFPA.c new file mode 100644 index 0000000..e64acca --- /dev/null +++ b/src/cube.sync-conflict-20230524-182143-JOKKFPA.c | |||
| @@ -0,0 +1,614 @@ | |||
| 1 | /* | ||
| 2 | # Cube representation, moves, transformations and indexing | ||
| 3 | |||
| 4 | ## String description | ||
| 5 | |||
| 6 | The functions readcube() and writecube() use the following format. | ||
| 7 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 8 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 9 | Similarly, each corner is represented by three letters and a number | ||
| 10 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 11 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 12 | relative to the U / D axis. | ||
| 13 | |||
| 14 | The correct order of the pieces is the same as that defined in the | ||
| 15 | section "Internal cube representation", except that pieces are read | ||
| 16 | left-to-right. Pieces are divided by slices, so the ordering is not the | ||
| 17 | most intuitive, but it is more convenient for the internal representation. | ||
| 18 | |||
| 19 | Whitespaces between pieces are ignored when reading the cube, and a | ||
| 20 | single whitespace character is added between pieces when writing. | ||
| 21 | |||
| 22 | For example, the solved cube looks like this: | ||
| 23 | |||
| 24 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 25 | |||
| 26 | The cube after the moves R'U'F looks like this: | ||
| 27 | |||
| 28 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 29 | |||
| 30 | More formats might be supported in the future. | ||
| 31 | |||
| 32 | ## Internal cube representation | ||
| 33 | |||
| 34 | The cube_t data structure implemented in this file is designed to | ||
| 35 | efficiently perform common operations on a 3x3x3 Rubik's cube when | ||
| 36 | solving it with an iterative-deepening DFS search. It is not the most | ||
| 37 | general, complete, easy to read or compact one. Since the cube can | ||
| 38 | be trivially reoriented before the search, we only encode permutations | ||
| 39 | of the cube that keep the center pieces in a fixed position (that is, | ||
| 40 | we do not encode the position of the centers). | ||
| 41 | |||
| 42 | The cube state is encoded in two 64-bit integers, one for edges and one | ||
| 43 | for centers. We explain how edges are encoded first, and the highlight | ||
| 44 | the few differences with corners afterwards. | ||
| 45 | |||
| 46 | For encoding edges, only the 60 least-significant bits are used. Each | ||
| 47 | edge described by 5 bits. The position of a 5-bit block in the 64-bit | ||
| 48 | integer determine the position of the edge piece in the cube, according | ||
| 49 | to the following table (least-significant bits on the right): | ||
| 50 | |||
| 51 | 55-59 50-54 45-49 40-44 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 52 | BR BL FL FR DR DL UL UR DF DB UB UF | ||
| 53 | ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee | ||
| 54 | |||
| 55 | For each edge, the 4 least-significant bits ('ssee' in the table) | ||
| 56 | determine the piece. The two bits marked with 'ss' determine the internal | ||
| 57 | slice the piece belongs to, i.e. they are either '00' for M, '01' for | ||
| 58 | S or '10' for E. The other two bits (marked with 'ee') determine the | ||
| 59 | actual edge piece among the 4 in the same slice, and they are assigned | ||
| 60 | somewhat arbitarily. Using this representation and the ordering defined | ||
| 61 | in the table above, the edges are correctly permuted when these 4 bits | ||
| 62 | for each represent the numbers 0 to 11 in the correct order. | ||
| 63 | |||
| 64 | The last bit determines the orientation. The orientation of an edge | ||
| 65 | depends on its position, and it is defined being 0 if the edge can be | ||
| 66 | moved to its place in the solved orientation by permutations in the | ||
| 67 | subgroup <U, D, R, L, F2, B2>. | ||
| 68 | |||
| 69 | Corners are encoded in the 48 least-significant bits, and are described | ||
| 70 | by 6 bits each, their position being defined by the following table: | ||
| 71 | |||
| 72 | 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 73 | DBL DFL UBR UFL DBR DFL UBL UFR | ||
| 74 | oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc | ||
| 75 | |||
| 76 | The bit marked with an 'x' describes the axis the corner belongs to. | ||
| 77 | The 0 axis consists of the corners UFR, UBL, DFL and DBR, and the other | ||
| 78 | four corners form the axis marked with 1. Then two bits are needed to | ||
| 79 | identify the corner among the four of the same axis. The last three bits | ||
| 80 | determine the orientation, where one corner is defined to be oriented | ||
| 81 | (marked with '000') if its top or bottom sticker faces the top or bottom | ||
| 82 | side. A corner a clockwise turn away from being oriented, thus requiring | ||
| 83 | a counter-clockwise turn to be oriented correctly, is marked with '001', | ||
| 84 | and a corner a counter-clockwise turn away is marked with '010'. The most | ||
| 85 | significant bit is not used to determine the corner orientation, but it | ||
| 86 | must always be set to '0' to simplify the moving operations (see below). | ||
| 87 | |||
| 88 | ## Basic moves | ||
| 89 | |||
| 90 | The 18 basic moves of the cube could be performed by applying a suitable | ||
| 91 | general permutation (see below), but they have instead been manually | ||
| 92 | implemented with a few simple operations each, to improve performance. | ||
| 93 | |||
| 94 | For each move we first permute the pieces. This amounts to shifting | ||
| 95 | around 4 blocks of bits for edges and 4 for corners. Since in some cases | ||
| 96 | adjacent pieces on the cube are also adjacent in the bit representation we | ||
| 97 | use, we can save some operations by shifting multiple blocks together. | ||
| 98 | For example, for the move U for edges we shift a block of 15 bits 5 | ||
| 99 | positions to the left and a block of 5 bits 15 positions to the right. | ||
| 100 | |||
| 101 | There are some moves that change the orientation of the pieces. Namely, | ||
| 102 | the moves F, F', B and B' change the orientation of the edges and those | ||
| 103 | moves as well as R, R', L and L' change the orientation of the corners. | ||
| 104 | Edge orientation is easy to address: we simply xor the edge representation | ||
| 105 | by a bit mask with zeroes everywhere except for the 4 edges that need | ||
| 106 | to be flipped (i.e. the ones on the twisted face). | ||
| 107 | |||
| 108 | Corner orientation is harder to reproduce efficiently working only with | ||
| 109 | bitwise operations, as it involves performing operations modulo 3. | ||
| 110 | However, with the help of the extra bit we reserved, we are able to | ||
| 111 | do this using only two additions and 3 bitwise operations, without | ||
| 112 | any multiplication, division or modulo operation. The trick is | ||
| 113 | to use the following formula to sum two numbers x, y in {0,1,2}: | ||
| 114 | |||
| 115 | ((x+y) + (x+y+1)/4) % 4 | ||
| 116 | |||
| 117 | The thrid bit is needed because x+y and x+y+1 can exceed 3. | ||
| 118 | See below (in the code) for the details. | ||
| 119 | |||
| 120 | ## Inverting the cube | ||
| 121 | |||
| 122 | TODO | ||
| 123 | |||
| 124 | ## Transformations (conjugations by full-cube rotations) | ||
| 125 | |||
| 126 | TODO | ||
| 127 | |||
| 128 | ## Indexing | ||
| 129 | |||
| 130 | TODO - subgroup description etc | ||
| 131 | |||
| 132 | Ideas for pruning (for another file?): | ||
| 133 | - Use corner separation + CO as main coordinate (~150k states) | ||
| 134 | - for huge tables, htr corners can be used (6 times larger) | ||
| 135 | - Symmetry table, one entry or each main coordinate value with the | ||
| 136 | following info: | ||
| 137 | - index of the corresponding main symcoord (13 bits) | ||
| 138 | - transtorep (6 bits) | ||
| 139 | - base value for pruning table (5 bits, probably 4) | ||
| 140 | - pruning value for only main coord, i.e. fallback (4 bits) | ||
| 141 | - To get a full coord for the cube: | ||
| 142 | - get first coord c, get the transtorep | ||
| 143 | - transform edges with transtorep | ||
| 144 | - get second coordinate e | ||
| 145 | - return c * MAXE + e | ||
| 146 | - This is still too big, so divide by a power of 2 to get the hashed index | ||
| 147 | - second coordinate: ep always, + varible number of eo bit (0 to 11) | ||
| 148 | - Generate table: | ||
| 149 | - first probe for base value: | ||
| 150 | - solve coord using fallback table for pruning for 10k random states or so | ||
| 151 | - loop over all possible values (even if going for smaller table) | ||
| 152 | - with inverse-index strategy or what? | ||
| 153 | - use 1 bit per entry (more than base value or not) | ||
| 154 | */ | ||
| 155 | |||
| 156 | #include <stdbool.h> | ||
| 157 | #include <stdint.h> | ||
| 158 | #include <string.h> | ||
| 159 | |||
| 160 | #ifdef DEBUG | ||
| 161 | #include <stdio.h> | ||
| 162 | #endif | ||
| 163 | |||
| 164 | #include "cube.h" | ||
| 165 | |||
| 166 | #define _error 0xFFFFFFFF | ||
| 167 | |||
| 168 | #define _esize 5ULL | ||
| 169 | #define _eoblock 0x10ULL /* 10000 */ | ||
| 170 | #define _epblock 0x0FULL /* 01111 */ | ||
| 171 | #define _eblock 0x1FULL /* 11111 */ | ||
| 172 | |||
| 173 | #define _csize 6ULL | ||
| 174 | #define _coblock 0x18ULL /* 011000 */ | ||
| 175 | #define _cpblock 0x07ULL /* 000111 */ | ||
| 176 | #define _cblock 0x3FULL /* 111111 */ | ||
| 177 | |||
| 178 | #define _edge_uf 0ULL /* 00 00 */ | ||
| 179 | #define _edge_ub 1ULL /* 00 01 */ | ||
| 180 | #define _edge_db 2ULL /* 00 10 */ | ||
| 181 | #define _edge_df 3ULL /* 00 11 */ | ||
| 182 | #define _edge_ur 4ULL /* 01 00 */ | ||
| 183 | #define _edge_ul 5ULL /* 01 01 */ | ||
| 184 | #define _edge_dl 6ULL /* 01 10 */ | ||
| 185 | #define _edge_dr 7ULL /* 01 11 */ | ||
| 186 | #define _edge_fr 8ULL /* 10 00 */ | ||
| 187 | #define _edge_fl 9ULL /* 10 01 */ | ||
| 188 | #define _edge_bl 10ULL /* 10 10 */ | ||
| 189 | #define _edge_br 11ULL /* 10 11 */ | ||
| 190 | |||
| 191 | #define _corner_ufr 0ULL /* 0 00 */ | ||
| 192 | #define _corner_ubl 1ULL /* 0 01 */ | ||
| 193 | #define _corner_dfl 2ULL /* 0 10 */ | ||
| 194 | #define _corner_dbr 3ULL /* 0 11 */ | ||
| 195 | #define _corner_ufl 4ULL /* 1 00 */ | ||
| 196 | #define _corner_ubr 5ULL /* 1 01 */ | ||
| 197 | #define _corner_dfr 6ULL /* 1 10 */ | ||
| 198 | #define _corner_dbl 7ULL /* 1 11 */ | ||
| 199 | |||
| 200 | #define ESHIFT(i) ((i) * _esize) | ||
| 201 | #define EOSHIFT(i) (4ULL + (i) * _esize) | ||
| 202 | #define EMASK(i) (_eblock << ESHIFT(i)) | ||
| 203 | #define EOMASK(i) (_eoblock << ESHIFT(i)) | ||
| 204 | #define EPMASK(i) (_epblock << ESHIFT(i)) | ||
| 205 | #define ESOLVED(ee) (_edge_##ee << (_edge_##ee * _esize)) | ||
| 206 | #define EDGEAT(e, i) (((e) & EMASK(i)) >> ESHIFT(i)) | ||
| 207 | #define EOAT(e, i) (((e) & EOMASK(i)) >> EOSHIFT(i)) | ||
| 208 | #define EPAT(e, i) (((e) & EPMASK(i)) >> ESHIFT(i)) | ||
| 209 | |||
| 210 | #define CSHIFT(i) ((i) * _csize) | ||
| 211 | #define COSHIFT(i) (3ULL + (i) * _csize) | ||
| 212 | #define CMASK(i) (_cblock << CSHIFT(i)) | ||
| 213 | #define COMASK(i) (_coblock << CSHIFT(i)) | ||
| 214 | #define CPMASK(i) (_cpblock << CSHIFT(i)) | ||
| 215 | #define CSOLVED(ccc) (_corner_##ccc << (_corner_##ccc * _csize)) | ||
| 216 | #define CORNERAT(c, i) (((c) & CMASK(i)) >> CSHIFT(i)) | ||
| 217 | #define COAT(c, i) (((c) & COMASK(i)) >> COSHIFT(i)) | ||
| 218 | #define CPAT(c, i) (((c) & CPMASK(i)) >> CSHIFT(i)) | ||
| 219 | |||
| 220 | #define _emask_u (EMASK(uf) | EMASK(ul) | EMASK(ub) | EMASK(ur)) | ||
| 221 | #define _emask_d (EMASK(df) | EMASK(dl) | EMASK(db) | EMASK(dr)) | ||
| 222 | #define _emask_r (EMASK(ur) | EMASK(dr) | EMASK(fr) | EMASK(br)) | ||
| 223 | #define _emask_l (EMASK(ul) | EMASK(dl) | EMASK(fl) | EMASK(bl)) | ||
| 224 | #define _emask_f (EMASK(uf) | EMASK(df) | EMASK(fr) | EMASK(fl)) | ||
| 225 | #define _emask_b (EMASK(ub) | EMASK(db) | EMASK(br) | EMASK(bl)) | ||
| 226 | |||
| 227 | #define _cmask_u (CMASK(ufr) | CMASK(ufl) | CMASK(ubl) | CMASK(ubr)) | ||
| 228 | #define _cmask_d (CMASK(dfr) | CMASK(dfl) | CMASK(dbl) | CMASK(dbr)) | ||
| 229 | #define _cmask_r (CMASK(ufr) | CMASK(dfr) | CMASK(ubr) | CMASK(dbr)) | ||
| 230 | #define _cmask_l (CMASK(ufl) | CMASK(dfl) | CMASK(ubl) | CMASK(dbl)) | ||
| 231 | #define _cmask_f (CMASK(ufr) | CMASK(ufl) | CMASK(dfr) | CMASK(dfl)) | ||
| 232 | #define _cmask_b (CMASK(ubr) | CMASK(ubl) | CMASK(dbr) | CMASK(dbl)) | ||
| 233 | |||
| 234 | #define _eomask (EOMASK(uf) | EOMASK(ul) | EOMASK(ub) | EOMASK(ur) \ | ||
| 235 | EOMASK(df) | EOMASK(dl) | EOMASK(db) | EOMASK(dr) \ | ||
| 236 | EOMASK(fr) | EOMASK(fl) | EOMASK(bl) | EOMASK(br)) | ||
| 237 | #define _comask (COMASK(ufr) | COMASK(ufl) | COMASK(ubl) | COMASK(ubr) \ | ||
| 238 | COMASK(dfr) | COMASK(dfl) | COMASK(dbl) | COMASK(dbr)) | ||
| 239 | |||
| 240 | static uint64_t permsign(uint64_t *, int); | ||
| 241 | static uint64_t readep(char *); | ||
| 242 | static uint64_t readeo(char *); | ||
| 243 | static uint64_t readcp(char *); | ||
| 244 | static uint64_t readco(char *); | ||
| 245 | static uint64_t readmove(char); | ||
| 246 | static uint64_t readmodifier(char); | ||
| 247 | |||
| 248 | static char *edgestr[] = { | ||
| 249 | [_edge_uf] = "UF", | ||
| 250 | [_edge_ub] = "UB", | ||
| 251 | [_edge_db] = "DB", | ||
| 252 | [_edge_df] = "DF", | ||
| 253 | [_edge_ur] = "UR", | ||
| 254 | [_edge_ul] = "UL", | ||
| 255 | [_edge_dl] = "DL", | ||
| 256 | [_edge_dr] = "DR", | ||
| 257 | [_edge_fr] = "FR", | ||
| 258 | [_edge_fl] = "FL", | ||
| 259 | [_edge_bl] = "BL", | ||
| 260 | [_edge_br] = "BR" | ||
| 261 | }; | ||
| 262 | static char *cornerstr[] = { | ||
| 263 | [_corner_ufr] = "UFR", | ||
| 264 | [_corner_ubl] = "UBL", | ||
| 265 | [_corner_dfl] = "DFL", | ||
| 266 | [_corner_dbr] = "DBR", | ||
| 267 | [_corner_ufl] = "UFL", | ||
| 268 | [_corner_ubr] = "UBR", | ||
| 269 | [_corner_dfr] = "DFR", | ||
| 270 | [_corner_dbl] = "DBL" | ||
| 271 | }; | ||
| 272 | static char *movestr[] = { | ||
| 273 | [U] = "U", [U2] = "U2", [U3] = "U'", | ||
| 274 | [D] = "D", [D2] = "D2", [D3] = "D'", | ||
| 275 | [R] = "R", [R2] = "R2", [R3] = "R'", | ||
| 276 | [L] = "L", [L2] = "L2", [L3] = "L'", | ||
| 277 | [F] = "F", [F2] = "F2", [F3] = "F'", | ||
| 278 | [B] = "B", [B2] = "B2", [B3] = "B'", | ||
| 279 | }; | ||
| 280 | |||
| 281 | cube_t solvedcube = { | ||
| 282 | .e = ESOLVED(uf) | ESOLVED(ul) | ESOLVED(ub) | ESOLVED(ur) | | ||
| 283 | ESOLVED(df) | ESOLVED(dl) | ESOLVED(db) | ESOLVED(dr) | | ||
| 284 | ESOLVED(fr) | ESOLVED(fl) | ESOLVED(bl) | ESOLVED(br), | ||
| 285 | .c = CSOLVED(ufr) | CSOLVED(ufl) | CSOLVED(ubl) | CSOLVED(ubr) | | ||
| 286 | CSOLVED(dfr) | CSOLVED(dfl) | CSOLVED(dbl) | CSOLVED(dbr), | ||
| 287 | }; | ||
| 288 | cube_t errorcube = { .e = _error, .c = _error }; | ||
| 289 | |||
| 290 | |||
| 291 | static uint64_t | ||
| 292 | permsign(uint64_t *a, int n) | ||
| 293 | { | ||
| 294 | int i, j; | ||
| 295 | uint64_t ret; | ||
| 296 | |||
| 297 | ret = 0; | ||
| 298 | |||
| 299 | for (i = 0; i < n; i++) | ||
| 300 | for (j = i+1; j < n; j++) | ||
| 301 | ret += a[i] > a[j] ? 1 : 0; | ||
| 302 | |||
| 303 | return ret % 2; | ||
| 304 | } | ||
| 305 | |||
| 306 | bool | ||
| 307 | isconsistent(cube_t cube) | ||
| 308 | { | ||
| 309 | uint64_t p[12], sum; | ||
| 310 | bool found[12]; | ||
| 311 | int i; | ||
| 312 | |||
| 313 | sum = 0; | ||
| 314 | |||
| 315 | /* Check for EP consistency */ | ||
| 316 | for (i = 0; i < 12; i++) | ||
| 317 | found[i] = false; | ||
| 318 | for (i = 0; i < 12; i++) { | ||
| 319 | p[i] = EPAT(cube.e, i); | ||
| 320 | found[p[i]] = true; | ||
| 321 | } | ||
| 322 | for (i = 0; i < 12; i++) | ||
| 323 | if (!found[i]) | ||
| 324 | return false; | ||
| 325 | sum = permsign(p, 12); | ||
| 326 | |||
| 327 | /* Check for CP consistency */ | ||
| 328 | for (i = 0; i < 8; i++) | ||
| 329 | found[i] = false; | ||
| 330 | for (i = 0; i < 8; i++) { | ||
| 331 | p[i] = CPAT(cube.c, i); | ||
| 332 | found[p[i]] = true; | ||
| 333 | } | ||
| 334 | for (i = 0; i < 8; i++) | ||
| 335 | if (!found[i]) | ||
| 336 | return false; | ||
| 337 | sum += permsign(p, 8); | ||
| 338 | |||
| 339 | /* Check permutation parity */ | ||
| 340 | if (sum % 2 != 0) | ||
| 341 | return false; | ||
| 342 | |||
| 343 | /* Check for EO parity */ | ||
| 344 | for (i = 0, sum = 0; i < 12; i++) | ||
| 345 | sum += EOAT(cube.e, i); | ||
| 346 | if (sum % 2 != 0) | ||
| 347 | return false; | ||
| 348 | |||
| 349 | /* Check for CO parity */ | ||
| 350 | for (i = 0, sum = 0; i < 8; i++) | ||
| 351 | sum += COAT(cube.c, i); | ||
| 352 | if (sum % 3 != 0) | ||
| 353 | return false; | ||
| 354 | |||
| 355 | /* Check that CO extra bit is zero */ | ||
| 356 | for (i = 0; i < 8; i++) | ||
| 357 | if (cube.c & (1ULL << (5 + i * _csize))) | ||
| 358 | return false; | ||
| 359 | |||
| 360 | return true; | ||
| 361 | } | ||
| 362 | |||
| 363 | bool | ||
| 364 | issolved(cube_t cube) | ||
| 365 | { | ||
| 366 | return cube.c == solvedcube.c && cube.e == solvedcube.e; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | static uint64_t | ||
| 371 | readep(char *str) | ||
| 372 | { | ||
| 373 | if (!strncmp(str, "UF", 2)) | ||
| 374 | return _edge_uf; | ||
| 375 | if (!strncmp(str, "UL", 2)) | ||
| 376 | return _edge_ul; | ||
| 377 | if (!strncmp(str, "UB", 2)) | ||
| 378 | return _edge_ub; | ||
| 379 | if (!strncmp(str, "UR", 2)) | ||
| 380 | return _edge_ur; | ||
| 381 | if (!strncmp(str, "DF", 2)) | ||
| 382 | return _edge_df; | ||
| 383 | if (!strncmp(str, "DL", 2)) | ||
| 384 | return _edge_dl; | ||
| 385 | if (!strncmp(str, "DB", 2)) | ||
| 386 | return _edge_db; | ||
| 387 | if (!strncmp(str, "DR", 2)) | ||
| 388 | return _edge_dr; | ||
| 389 | if (!strncmp(str, "FR", 2)) | ||
| 390 | return _edge_fr; | ||
| 391 | if (!strncmp(str, "FL", 2)) | ||
| 392 | return _edge_fl; | ||
| 393 | if (!strncmp(str, "BL", 2)) | ||
| 394 | return _edge_bl; | ||
| 395 | if (!strncmp(str, "BR", 2)) | ||
| 396 | return _edge_br; | ||
| 397 | |||
| 398 | return _error; | ||
| 399 | } | ||
| 400 | |||
| 401 | static uint64_t | ||
| 402 | readeo(char *str) | ||
| 403 | { | ||
| 404 | if (*str == '0') | ||
| 405 | return 0ULL; | ||
| 406 | if (*str == '1') | ||
| 407 | return 1ULL; | ||
| 408 | |||
| 409 | return _error; | ||
| 410 | } | ||
| 411 | |||
| 412 | static uint64_t | ||
| 413 | readcp(char *str) | ||
| 414 | { | ||
| 415 | if (!strncmp(str, "UFR", 3) || !strncmp(str, "URF", 3)) | ||
| 416 | return _corner_ufr; | ||
| 417 | if (!strncmp(str, "UFL", 3) || !strncmp(str, "ULF", 3)) | ||
| 418 | return _corner_ufl; | ||
| 419 | if (!strncmp(str, "UBL", 3) || !strncmp(str, "ULB", 3)) | ||
| 420 | return _corner_ubl; | ||
| 421 | if (!strncmp(str, "UBR", 3) || !strncmp(str, "URB", 3)) | ||
| 422 | return _corner_ubr; | ||
| 423 | if (!strncmp(str, "DFR", 3) || !strncmp(str, "DRF", 3)) | ||
| 424 | return _corner_dfr; | ||
| 425 | if (!strncmp(str, "DFL", 3) || !strncmp(str, "DLF", 3)) | ||
| 426 | return _corner_dfl; | ||
| 427 | if (!strncmp(str, "DBL", 3) || !strncmp(str, "DLB", 3)) | ||
| 428 | return _corner_dbl; | ||
| 429 | if (!strncmp(str, "DBR", 3) || !strncmp(str, "DRB", 3)) | ||
| 430 | return _corner_dbr; | ||
| 431 | |||
| 432 | return _error; | ||
| 433 | } | ||
| 434 | |||
| 435 | static uint64_t | ||
| 436 | readco(char *str) | ||
| 437 | { | ||
| 438 | if (*str == '0') | ||
| 439 | return 0ULL; | ||
| 440 | if (*str == '1') | ||
| 441 | return 1ULL; | ||
| 442 | if (*str == '2') | ||
| 443 | return 2ULL; | ||
| 444 | |||
| 445 | return _error; | ||
| 446 | } | ||
| 447 | |||
| 448 | cube_t | ||
| 449 | readcube(char *buf) | ||
| 450 | { | ||
| 451 | int i; | ||
| 452 | uint64_t piece, orient; | ||
| 453 | cube_t ret = {0}; | ||
| 454 | char *b = buf; | ||
| 455 | |||
| 456 | for (i = 0; i < 12; i++) { | ||
| 457 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 458 | b++; | ||
| 459 | if ((piece = readep(b)) == _error) | ||
| 460 | goto readcube_error; | ||
| 461 | b += 2; | ||
| 462 | if ((orient = readeo(b)) == _error) | ||
| 463 | goto readcube_error; | ||
| 464 | b++; | ||
| 465 | ret.e |= (piece << ESHIFT(i)) | (orient << EOSHIFT(i)); | ||
| 466 | } | ||
| 467 | for (i = 0; i < 8; i++) { | ||
| 468 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 469 | b++; | ||
| 470 | if ((piece = readcp(b)) == _error) | ||
| 471 | goto readcube_error; | ||
| 472 | b += 3; | ||
| 473 | if ((orient = readco(b)) == _error) | ||
| 474 | goto readcube_error; | ||
| 475 | b++; | ||
| 476 | ret.c |= (piece << CSHIFT(i)) | (orient << COSHIFT(i)); | ||
| 477 | } | ||
| 478 | |||
| 479 | return ret; | ||
| 480 | |||
| 481 | readcube_error: | ||
| 482 | return errorcube; | ||
| 483 | } | ||
| 484 | |||
| 485 | void | ||
| 486 | writecube(cube_t cube, char *buf) | ||
| 487 | { | ||
| 488 | char *errormsg; | ||
| 489 | uint64_t piece; | ||
| 490 | size_t len; | ||
| 491 | int i; | ||
| 492 | |||
| 493 | if (!isconsistent(cube)) { | ||
| 494 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 495 | goto writecube_error; | ||
| 496 | } | ||
| 497 | |||
| 498 | for (i = 0; i < 12; i++) { | ||
| 499 | piece = EPAT(cube.e, i); | ||
| 500 | buf[4*i ] = edgestr[piece][0]; | ||
| 501 | buf[4*i + 1] = edgestr[piece][1]; | ||
| 502 | buf[4*i + 2] = EOAT(cube.e, i) + '0'; | ||
| 503 | buf[4*i + 3] = ' '; | ||
| 504 | } | ||
| 505 | for (i = 0; i < 8; i++) { | ||
| 506 | piece = CPAT(cube.c, i); | ||
| 507 | buf[48 + 5*i ] = cornerstr[piece][0]; | ||
| 508 | buf[48 + 5*i + 1] = cornerstr[piece][1]; | ||
| 509 | buf[48 + 5*i + 2] = cornerstr[piece][2]; | ||
| 510 | buf[48 + 5*i + 3] = COAT(cube.c, i) + '0'; | ||
| 511 | buf[48 + 5*i + 4] = ' '; | ||
| 512 | } | ||
| 513 | |||
| 514 | buf[48+39] = '\0'; | ||
| 515 | |||
| 516 | return; | ||
| 517 | |||
| 518 | writecube_error: | ||
| 519 | len = strlen(errormsg); | ||
| 520 | strcpy(buf, errormsg); | ||
| 521 | buf[len] = '\n'; | ||
| 522 | buf[len+1] = '\0'; | ||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | static uint64_t | ||
| 527 | readmove(char c) | ||
| 528 | { | ||
| 529 | switch (c) { | ||
| 530 | case 'U': | ||
| 531 | return U; | ||
| 532 | case 'D': | ||
| 533 | return D; | ||
| 534 | case 'R': | ||
| 535 | return R; | ||
| 536 | case 'L': | ||
| 537 | return L; | ||
| 538 | case 'F': | ||
| 539 | return F; | ||
| 540 | case 'B': | ||
| 541 | return B; | ||
| 542 | default: | ||
| 543 | return _error; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | static uint64_t | ||
| 548 | readmodifier(char c) | ||
| 549 | { | ||
| 550 | switch (c) { | ||
| 551 | case '1': /* Fallthrough */ | ||
| 552 | case '2': /* Fallthrough */ | ||
| 553 | case '3': | ||
| 554 | return c - '0' - 1; | ||
| 555 | case '\'': | ||
| 556 | return 2; | ||
| 557 | default: | ||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | int | ||
| 563 | readmoves(char *buf, move_t *m) | ||
| 564 | { | ||
| 565 | int n; | ||
| 566 | uint64_t r; | ||
| 567 | char *b; | ||
| 568 | |||
| 569 | for (b = buf, n = 0; *b != '\0'; b++) { | ||
| 570 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 571 | b++; | ||
| 572 | if ((r = readmove(*b)) == _error) | ||
| 573 | return -1; | ||
| 574 | m[n] = (move_t)r; | ||
| 575 | if ((r = readmodifier(*(b+1))) != 0) { | ||
| 576 | b++; | ||
| 577 | m[n] += r; | ||
| 578 | } | ||
| 579 | n++; | ||
| 580 | } | ||
| 581 | |||
| 582 | return n; | ||
| 583 | } | ||
| 584 | |||
| 585 | void | ||
| 586 | writemoves(move_t *m, int n, char *buf) | ||
| 587 | { | ||
| 588 | int i; | ||
| 589 | char *b, *s; | ||
| 590 | |||
| 591 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 592 | s = movestr[m[i]]; | ||
| 593 | strcpy(b, s); | ||
| 594 | b += strlen(s); | ||
| 595 | *b = ' '; | ||
| 596 | } | ||
| 597 | *b = '\0'; | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | cube_t | ||
| 602 | move(move_t m, cube_t c) | ||
| 603 | { | ||
| 604 | /* TODO - not implemented yet */ | ||
| 605 | |||
| 606 | cube_t ret = {0}; | ||
| 607 | |||
| 608 | switch (m) { | ||
| 609 | case U: | ||
| 610 | return ret; | ||
| 611 | default: | ||
| 612 | return ret; | ||
| 613 | } | ||
| 614 | } | ||
diff --git a/src/cube.sync-conflict-20230524-182143-JOKKFPA.h b/src/cube.sync-conflict-20230524-182143-JOKKFPA.h new file mode 100644 index 0000000..2763d16 --- /dev/null +++ b/src/cube.sync-conflict-20230524-182143-JOKKFPA.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | typedef enum { | ||
| 2 | U, U2, U3, D, D2, D3, | ||
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | typedef struct { | ||
| 7 | uint64_t e; | ||
| 8 | uint64_t c; | ||
| 9 | } cube_t; | ||
| 10 | |||
| 11 | extern cube_t solvedcube; | ||
| 12 | extern cube_t errorcube; | ||
| 13 | |||
| 14 | bool isconsistent(cube_t); | ||
| 15 | bool issolved(cube_t); | ||
| 16 | |||
| 17 | cube_t readcube(char *); | ||
| 18 | void writecube(cube_t, char *); | ||
| 19 | |||
| 20 | int readmoves(char *, move_t *); | ||
| 21 | void writemoves(move_t *, int, char *); | ||
| 22 | |||
| 23 | /* | ||
| 24 | cube_t move(move_t, cube_t); | ||
| 25 | cube_t inverse(cube_t); | ||
| 26 | |||
| 27 | */ | ||
diff --git a/src/cube.sync-conflict-20230524-182146-JOKKFPA.c b/src/cube.sync-conflict-20230524-182146-JOKKFPA.c new file mode 100644 index 0000000..e64acca --- /dev/null +++ b/src/cube.sync-conflict-20230524-182146-JOKKFPA.c | |||
| @@ -0,0 +1,614 @@ | |||
| 1 | /* | ||
| 2 | # Cube representation, moves, transformations and indexing | ||
| 3 | |||
| 4 | ## String description | ||
| 5 | |||
| 6 | The functions readcube() and writecube() use the following format. | ||
| 7 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 8 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 9 | Similarly, each corner is represented by three letters and a number | ||
| 10 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 11 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 12 | relative to the U / D axis. | ||
| 13 | |||
| 14 | The correct order of the pieces is the same as that defined in the | ||
| 15 | section "Internal cube representation", except that pieces are read | ||
| 16 | left-to-right. Pieces are divided by slices, so the ordering is not the | ||
| 17 | most intuitive, but it is more convenient for the internal representation. | ||
| 18 | |||
| 19 | Whitespaces between pieces are ignored when reading the cube, and a | ||
| 20 | single whitespace character is added between pieces when writing. | ||
| 21 | |||
| 22 | For example, the solved cube looks like this: | ||
| 23 | |||
| 24 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 25 | |||
| 26 | The cube after the moves R'U'F looks like this: | ||
| 27 | |||
| 28 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 29 | |||
| 30 | More formats might be supported in the future. | ||
| 31 | |||
| 32 | ## Internal cube representation | ||
| 33 | |||
| 34 | The cube_t data structure implemented in this file is designed to | ||
| 35 | efficiently perform common operations on a 3x3x3 Rubik's cube when | ||
| 36 | solving it with an iterative-deepening DFS search. It is not the most | ||
| 37 | general, complete, easy to read or compact one. Since the cube can | ||
| 38 | be trivially reoriented before the search, we only encode permutations | ||
| 39 | of the cube that keep the center pieces in a fixed position (that is, | ||
| 40 | we do not encode the position of the centers). | ||
| 41 | |||
| 42 | The cube state is encoded in two 64-bit integers, one for edges and one | ||
| 43 | for centers. We explain how edges are encoded first, and the highlight | ||
| 44 | the few differences with corners afterwards. | ||
| 45 | |||
| 46 | For encoding edges, only the 60 least-significant bits are used. Each | ||
| 47 | edge described by 5 bits. The position of a 5-bit block in the 64-bit | ||
| 48 | integer determine the position of the edge piece in the cube, according | ||
| 49 | to the following table (least-significant bits on the right): | ||
| 50 | |||
| 51 | 55-59 50-54 45-49 40-44 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 52 | BR BL FL FR DR DL UL UR DF DB UB UF | ||
| 53 | ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee | ||
| 54 | |||
| 55 | For each edge, the 4 least-significant bits ('ssee' in the table) | ||
| 56 | determine the piece. The two bits marked with 'ss' determine the internal | ||
| 57 | slice the piece belongs to, i.e. they are either '00' for M, '01' for | ||
| 58 | S or '10' for E. The other two bits (marked with 'ee') determine the | ||
| 59 | actual edge piece among the 4 in the same slice, and they are assigned | ||
| 60 | somewhat arbitarily. Using this representation and the ordering defined | ||
| 61 | in the table above, the edges are correctly permuted when these 4 bits | ||
| 62 | for each represent the numbers 0 to 11 in the correct order. | ||
| 63 | |||
| 64 | The last bit determines the orientation. The orientation of an edge | ||
| 65 | depends on its position, and it is defined being 0 if the edge can be | ||
| 66 | moved to its place in the solved orientation by permutations in the | ||
| 67 | subgroup <U, D, R, L, F2, B2>. | ||
| 68 | |||
| 69 | Corners are encoded in the 48 least-significant bits, and are described | ||
| 70 | by 6 bits each, their position being defined by the following table: | ||
| 71 | |||
| 72 | 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 73 | DBL DFL UBR UFL DBR DFL UBL UFR | ||
| 74 | oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc | ||
| 75 | |||
| 76 | The bit marked with an 'x' describes the axis the corner belongs to. | ||
| 77 | The 0 axis consists of the corners UFR, UBL, DFL and DBR, and the other | ||
| 78 | four corners form the axis marked with 1. Then two bits are needed to | ||
| 79 | identify the corner among the four of the same axis. The last three bits | ||
| 80 | determine the orientation, where one corner is defined to be oriented | ||
| 81 | (marked with '000') if its top or bottom sticker faces the top or bottom | ||
| 82 | side. A corner a clockwise turn away from being oriented, thus requiring | ||
| 83 | a counter-clockwise turn to be oriented correctly, is marked with '001', | ||
| 84 | and a corner a counter-clockwise turn away is marked with '010'. The most | ||
| 85 | significant bit is not used to determine the corner orientation, but it | ||
| 86 | must always be set to '0' to simplify the moving operations (see below). | ||
| 87 | |||
| 88 | ## Basic moves | ||
| 89 | |||
| 90 | The 18 basic moves of the cube could be performed by applying a suitable | ||
| 91 | general permutation (see below), but they have instead been manually | ||
| 92 | implemented with a few simple operations each, to improve performance. | ||
| 93 | |||
| 94 | For each move we first permute the pieces. This amounts to shifting | ||
| 95 | around 4 blocks of bits for edges and 4 for corners. Since in some cases | ||
| 96 | adjacent pieces on the cube are also adjacent in the bit representation we | ||
| 97 | use, we can save some operations by shifting multiple blocks together. | ||
| 98 | For example, for the move U for edges we shift a block of 15 bits 5 | ||
| 99 | positions to the left and a block of 5 bits 15 positions to the right. | ||
| 100 | |||
| 101 | There are some moves that change the orientation of the pieces. Namely, | ||
| 102 | the moves F, F', B and B' change the orientation of the edges and those | ||
| 103 | moves as well as R, R', L and L' change the orientation of the corners. | ||
| 104 | Edge orientation is easy to address: we simply xor the edge representation | ||
| 105 | by a bit mask with zeroes everywhere except for the 4 edges that need | ||
| 106 | to be flipped (i.e. the ones on the twisted face). | ||
| 107 | |||
| 108 | Corner orientation is harder to reproduce efficiently working only with | ||
| 109 | bitwise operations, as it involves performing operations modulo 3. | ||
| 110 | However, with the help of the extra bit we reserved, we are able to | ||
| 111 | do this using only two additions and 3 bitwise operations, without | ||
| 112 | any multiplication, division or modulo operation. The trick is | ||
| 113 | to use the following formula to sum two numbers x, y in {0,1,2}: | ||
| 114 | |||
| 115 | ((x+y) + (x+y+1)/4) % 4 | ||
| 116 | |||
| 117 | The thrid bit is needed because x+y and x+y+1 can exceed 3. | ||
| 118 | See below (in the code) for the details. | ||
| 119 | |||
| 120 | ## Inverting the cube | ||
| 121 | |||
| 122 | TODO | ||
| 123 | |||
| 124 | ## Transformations (conjugations by full-cube rotations) | ||
| 125 | |||
| 126 | TODO | ||
| 127 | |||
| 128 | ## Indexing | ||
| 129 | |||
| 130 | TODO - subgroup description etc | ||
| 131 | |||
| 132 | Ideas for pruning (for another file?): | ||
| 133 | - Use corner separation + CO as main coordinate (~150k states) | ||
| 134 | - for huge tables, htr corners can be used (6 times larger) | ||
| 135 | - Symmetry table, one entry or each main coordinate value with the | ||
| 136 | following info: | ||
| 137 | - index of the corresponding main symcoord (13 bits) | ||
| 138 | - transtorep (6 bits) | ||
| 139 | - base value for pruning table (5 bits, probably 4) | ||
| 140 | - pruning value for only main coord, i.e. fallback (4 bits) | ||
| 141 | - To get a full coord for the cube: | ||
| 142 | - get first coord c, get the transtorep | ||
| 143 | - transform edges with transtorep | ||
| 144 | - get second coordinate e | ||
| 145 | - return c * MAXE + e | ||
| 146 | - This is still too big, so divide by a power of 2 to get the hashed index | ||
| 147 | - second coordinate: ep always, + varible number of eo bit (0 to 11) | ||
| 148 | - Generate table: | ||
| 149 | - first probe for base value: | ||
| 150 | - solve coord using fallback table for pruning for 10k random states or so | ||
| 151 | - loop over all possible values (even if going for smaller table) | ||
| 152 | - with inverse-index strategy or what? | ||
| 153 | - use 1 bit per entry (more than base value or not) | ||
| 154 | */ | ||
| 155 | |||
| 156 | #include <stdbool.h> | ||
| 157 | #include <stdint.h> | ||
| 158 | #include <string.h> | ||
| 159 | |||
| 160 | #ifdef DEBUG | ||
| 161 | #include <stdio.h> | ||
| 162 | #endif | ||
| 163 | |||
| 164 | #include "cube.h" | ||
| 165 | |||
| 166 | #define _error 0xFFFFFFFF | ||
| 167 | |||
| 168 | #define _esize 5ULL | ||
| 169 | #define _eoblock 0x10ULL /* 10000 */ | ||
| 170 | #define _epblock 0x0FULL /* 01111 */ | ||
| 171 | #define _eblock 0x1FULL /* 11111 */ | ||
| 172 | |||
| 173 | #define _csize 6ULL | ||
| 174 | #define _coblock 0x18ULL /* 011000 */ | ||
| 175 | #define _cpblock 0x07ULL /* 000111 */ | ||
| 176 | #define _cblock 0x3FULL /* 111111 */ | ||
| 177 | |||
| 178 | #define _edge_uf 0ULL /* 00 00 */ | ||
| 179 | #define _edge_ub 1ULL /* 00 01 */ | ||
| 180 | #define _edge_db 2ULL /* 00 10 */ | ||
| 181 | #define _edge_df 3ULL /* 00 11 */ | ||
| 182 | #define _edge_ur 4ULL /* 01 00 */ | ||
| 183 | #define _edge_ul 5ULL /* 01 01 */ | ||
| 184 | #define _edge_dl 6ULL /* 01 10 */ | ||
| 185 | #define _edge_dr 7ULL /* 01 11 */ | ||
| 186 | #define _edge_fr 8ULL /* 10 00 */ | ||
| 187 | #define _edge_fl 9ULL /* 10 01 */ | ||
| 188 | #define _edge_bl 10ULL /* 10 10 */ | ||
| 189 | #define _edge_br 11ULL /* 10 11 */ | ||
| 190 | |||
| 191 | #define _corner_ufr 0ULL /* 0 00 */ | ||
| 192 | #define _corner_ubl 1ULL /* 0 01 */ | ||
| 193 | #define _corner_dfl 2ULL /* 0 10 */ | ||
| 194 | #define _corner_dbr 3ULL /* 0 11 */ | ||
| 195 | #define _corner_ufl 4ULL /* 1 00 */ | ||
| 196 | #define _corner_ubr 5ULL /* 1 01 */ | ||
| 197 | #define _corner_dfr 6ULL /* 1 10 */ | ||
| 198 | #define _corner_dbl 7ULL /* 1 11 */ | ||
| 199 | |||
| 200 | #define ESHIFT(i) ((i) * _esize) | ||
| 201 | #define EOSHIFT(i) (4ULL + (i) * _esize) | ||
| 202 | #define EMASK(i) (_eblock << ESHIFT(i)) | ||
| 203 | #define EOMASK(i) (_eoblock << ESHIFT(i)) | ||
| 204 | #define EPMASK(i) (_epblock << ESHIFT(i)) | ||
| 205 | #define ESOLVED(ee) (_edge_##ee << (_edge_##ee * _esize)) | ||
| 206 | #define EDGEAT(e, i) (((e) & EMASK(i)) >> ESHIFT(i)) | ||
| 207 | #define EOAT(e, i) (((e) & EOMASK(i)) >> EOSHIFT(i)) | ||
| 208 | #define EPAT(e, i) (((e) & EPMASK(i)) >> ESHIFT(i)) | ||
| 209 | |||
| 210 | #define CSHIFT(i) ((i) * _csize) | ||
| 211 | #define COSHIFT(i) (3ULL + (i) * _csize) | ||
| 212 | #define CMASK(i) (_cblock << CSHIFT(i)) | ||
| 213 | #define COMASK(i) (_coblock << CSHIFT(i)) | ||
| 214 | #define CPMASK(i) (_cpblock << CSHIFT(i)) | ||
| 215 | #define CSOLVED(ccc) (_corner_##ccc << (_corner_##ccc * _csize)) | ||
| 216 | #define CORNERAT(c, i) (((c) & CMASK(i)) >> CSHIFT(i)) | ||
| 217 | #define COAT(c, i) (((c) & COMASK(i)) >> COSHIFT(i)) | ||
| 218 | #define CPAT(c, i) (((c) & CPMASK(i)) >> CSHIFT(i)) | ||
| 219 | |||
| 220 | #define _emask_u (EMASK(uf) | EMASK(ul) | EMASK(ub) | EMASK(ur)) | ||
| 221 | #define _emask_d (EMASK(df) | EMASK(dl) | EMASK(db) | EMASK(dr)) | ||
| 222 | #define _emask_r (EMASK(ur) | EMASK(dr) | EMASK(fr) | EMASK(br)) | ||
| 223 | #define _emask_l (EMASK(ul) | EMASK(dl) | EMASK(fl) | EMASK(bl)) | ||
| 224 | #define _emask_f (EMASK(uf) | EMASK(df) | EMASK(fr) | EMASK(fl)) | ||
| 225 | #define _emask_b (EMASK(ub) | EMASK(db) | EMASK(br) | EMASK(bl)) | ||
| 226 | |||
| 227 | #define _cmask_u (CMASK(ufr) | CMASK(ufl) | CMASK(ubl) | CMASK(ubr)) | ||
| 228 | #define _cmask_d (CMASK(dfr) | CMASK(dfl) | CMASK(dbl) | CMASK(dbr)) | ||
| 229 | #define _cmask_r (CMASK(ufr) | CMASK(dfr) | CMASK(ubr) | CMASK(dbr)) | ||
| 230 | #define _cmask_l (CMASK(ufl) | CMASK(dfl) | CMASK(ubl) | CMASK(dbl)) | ||
| 231 | #define _cmask_f (CMASK(ufr) | CMASK(ufl) | CMASK(dfr) | CMASK(dfl)) | ||
| 232 | #define _cmask_b (CMASK(ubr) | CMASK(ubl) | CMASK(dbr) | CMASK(dbl)) | ||
| 233 | |||
| 234 | #define _eomask (EOMASK(uf) | EOMASK(ul) | EOMASK(ub) | EOMASK(ur) \ | ||
| 235 | EOMASK(df) | EOMASK(dl) | EOMASK(db) | EOMASK(dr) \ | ||
| 236 | EOMASK(fr) | EOMASK(fl) | EOMASK(bl) | EOMASK(br)) | ||
| 237 | #define _comask (COMASK(ufr) | COMASK(ufl) | COMASK(ubl) | COMASK(ubr) \ | ||
| 238 | COMASK(dfr) | COMASK(dfl) | COMASK(dbl) | COMASK(dbr)) | ||
| 239 | |||
| 240 | static uint64_t permsign(uint64_t *, int); | ||
| 241 | static uint64_t readep(char *); | ||
| 242 | static uint64_t readeo(char *); | ||
| 243 | static uint64_t readcp(char *); | ||
| 244 | static uint64_t readco(char *); | ||
| 245 | static uint64_t readmove(char); | ||
| 246 | static uint64_t readmodifier(char); | ||
| 247 | |||
| 248 | static char *edgestr[] = { | ||
| 249 | [_edge_uf] = "UF", | ||
| 250 | [_edge_ub] = "UB", | ||
| 251 | [_edge_db] = "DB", | ||
| 252 | [_edge_df] = "DF", | ||
| 253 | [_edge_ur] = "UR", | ||
| 254 | [_edge_ul] = "UL", | ||
| 255 | [_edge_dl] = "DL", | ||
| 256 | [_edge_dr] = "DR", | ||
| 257 | [_edge_fr] = "FR", | ||
| 258 | [_edge_fl] = "FL", | ||
| 259 | [_edge_bl] = "BL", | ||
| 260 | [_edge_br] = "BR" | ||
| 261 | }; | ||
| 262 | static char *cornerstr[] = { | ||
| 263 | [_corner_ufr] = "UFR", | ||
| 264 | [_corner_ubl] = "UBL", | ||
| 265 | [_corner_dfl] = "DFL", | ||
| 266 | [_corner_dbr] = "DBR", | ||
| 267 | [_corner_ufl] = "UFL", | ||
| 268 | [_corner_ubr] = "UBR", | ||
| 269 | [_corner_dfr] = "DFR", | ||
| 270 | [_corner_dbl] = "DBL" | ||
| 271 | }; | ||
| 272 | static char *movestr[] = { | ||
| 273 | [U] = "U", [U2] = "U2", [U3] = "U'", | ||
| 274 | [D] = "D", [D2] = "D2", [D3] = "D'", | ||
| 275 | [R] = "R", [R2] = "R2", [R3] = "R'", | ||
| 276 | [L] = "L", [L2] = "L2", [L3] = "L'", | ||
| 277 | [F] = "F", [F2] = "F2", [F3] = "F'", | ||
| 278 | [B] = "B", [B2] = "B2", [B3] = "B'", | ||
| 279 | }; | ||
| 280 | |||
| 281 | cube_t solvedcube = { | ||
| 282 | .e = ESOLVED(uf) | ESOLVED(ul) | ESOLVED(ub) | ESOLVED(ur) | | ||
| 283 | ESOLVED(df) | ESOLVED(dl) | ESOLVED(db) | ESOLVED(dr) | | ||
| 284 | ESOLVED(fr) | ESOLVED(fl) | ESOLVED(bl) | ESOLVED(br), | ||
| 285 | .c = CSOLVED(ufr) | CSOLVED(ufl) | CSOLVED(ubl) | CSOLVED(ubr) | | ||
| 286 | CSOLVED(dfr) | CSOLVED(dfl) | CSOLVED(dbl) | CSOLVED(dbr), | ||
| 287 | }; | ||
| 288 | cube_t errorcube = { .e = _error, .c = _error }; | ||
| 289 | |||
| 290 | |||
| 291 | static uint64_t | ||
| 292 | permsign(uint64_t *a, int n) | ||
| 293 | { | ||
| 294 | int i, j; | ||
| 295 | uint64_t ret; | ||
| 296 | |||
| 297 | ret = 0; | ||
| 298 | |||
| 299 | for (i = 0; i < n; i++) | ||
| 300 | for (j = i+1; j < n; j++) | ||
| 301 | ret += a[i] > a[j] ? 1 : 0; | ||
| 302 | |||
| 303 | return ret % 2; | ||
| 304 | } | ||
| 305 | |||
| 306 | bool | ||
| 307 | isconsistent(cube_t cube) | ||
| 308 | { | ||
| 309 | uint64_t p[12], sum; | ||
| 310 | bool found[12]; | ||
| 311 | int i; | ||
| 312 | |||
| 313 | sum = 0; | ||
| 314 | |||
| 315 | /* Check for EP consistency */ | ||
| 316 | for (i = 0; i < 12; i++) | ||
| 317 | found[i] = false; | ||
| 318 | for (i = 0; i < 12; i++) { | ||
| 319 | p[i] = EPAT(cube.e, i); | ||
| 320 | found[p[i]] = true; | ||
| 321 | } | ||
| 322 | for (i = 0; i < 12; i++) | ||
| 323 | if (!found[i]) | ||
| 324 | return false; | ||
| 325 | sum = permsign(p, 12); | ||
| 326 | |||
| 327 | /* Check for CP consistency */ | ||
| 328 | for (i = 0; i < 8; i++) | ||
| 329 | found[i] = false; | ||
| 330 | for (i = 0; i < 8; i++) { | ||
| 331 | p[i] = CPAT(cube.c, i); | ||
| 332 | found[p[i]] = true; | ||
| 333 | } | ||
| 334 | for (i = 0; i < 8; i++) | ||
| 335 | if (!found[i]) | ||
| 336 | return false; | ||
| 337 | sum += permsign(p, 8); | ||
| 338 | |||
| 339 | /* Check permutation parity */ | ||
| 340 | if (sum % 2 != 0) | ||
| 341 | return false; | ||
| 342 | |||
| 343 | /* Check for EO parity */ | ||
| 344 | for (i = 0, sum = 0; i < 12; i++) | ||
| 345 | sum += EOAT(cube.e, i); | ||
| 346 | if (sum % 2 != 0) | ||
| 347 | return false; | ||
| 348 | |||
| 349 | /* Check for CO parity */ | ||
| 350 | for (i = 0, sum = 0; i < 8; i++) | ||
| 351 | sum += COAT(cube.c, i); | ||
| 352 | if (sum % 3 != 0) | ||
| 353 | return false; | ||
| 354 | |||
| 355 | /* Check that CO extra bit is zero */ | ||
| 356 | for (i = 0; i < 8; i++) | ||
| 357 | if (cube.c & (1ULL << (5 + i * _csize))) | ||
| 358 | return false; | ||
| 359 | |||
| 360 | return true; | ||
| 361 | } | ||
| 362 | |||
| 363 | bool | ||
| 364 | issolved(cube_t cube) | ||
| 365 | { | ||
| 366 | return cube.c == solvedcube.c && cube.e == solvedcube.e; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | static uint64_t | ||
| 371 | readep(char *str) | ||
| 372 | { | ||
| 373 | if (!strncmp(str, "UF", 2)) | ||
| 374 | return _edge_uf; | ||
| 375 | if (!strncmp(str, "UL", 2)) | ||
| 376 | return _edge_ul; | ||
| 377 | if (!strncmp(str, "UB", 2)) | ||
| 378 | return _edge_ub; | ||
| 379 | if (!strncmp(str, "UR", 2)) | ||
| 380 | return _edge_ur; | ||
| 381 | if (!strncmp(str, "DF", 2)) | ||
| 382 | return _edge_df; | ||
| 383 | if (!strncmp(str, "DL", 2)) | ||
| 384 | return _edge_dl; | ||
| 385 | if (!strncmp(str, "DB", 2)) | ||
| 386 | return _edge_db; | ||
| 387 | if (!strncmp(str, "DR", 2)) | ||
| 388 | return _edge_dr; | ||
| 389 | if (!strncmp(str, "FR", 2)) | ||
| 390 | return _edge_fr; | ||
| 391 | if (!strncmp(str, "FL", 2)) | ||
| 392 | return _edge_fl; | ||
| 393 | if (!strncmp(str, "BL", 2)) | ||
| 394 | return _edge_bl; | ||
| 395 | if (!strncmp(str, "BR", 2)) | ||
| 396 | return _edge_br; | ||
| 397 | |||
| 398 | return _error; | ||
| 399 | } | ||
| 400 | |||
| 401 | static uint64_t | ||
| 402 | readeo(char *str) | ||
| 403 | { | ||
| 404 | if (*str == '0') | ||
| 405 | return 0ULL; | ||
| 406 | if (*str == '1') | ||
| 407 | return 1ULL; | ||
| 408 | |||
| 409 | return _error; | ||
| 410 | } | ||
| 411 | |||
| 412 | static uint64_t | ||
| 413 | readcp(char *str) | ||
| 414 | { | ||
| 415 | if (!strncmp(str, "UFR", 3) || !strncmp(str, "URF", 3)) | ||
| 416 | return _corner_ufr; | ||
| 417 | if (!strncmp(str, "UFL", 3) || !strncmp(str, "ULF", 3)) | ||
| 418 | return _corner_ufl; | ||
| 419 | if (!strncmp(str, "UBL", 3) || !strncmp(str, "ULB", 3)) | ||
| 420 | return _corner_ubl; | ||
| 421 | if (!strncmp(str, "UBR", 3) || !strncmp(str, "URB", 3)) | ||
| 422 | return _corner_ubr; | ||
| 423 | if (!strncmp(str, "DFR", 3) || !strncmp(str, "DRF", 3)) | ||
| 424 | return _corner_dfr; | ||
| 425 | if (!strncmp(str, "DFL", 3) || !strncmp(str, "DLF", 3)) | ||
| 426 | return _corner_dfl; | ||
| 427 | if (!strncmp(str, "DBL", 3) || !strncmp(str, "DLB", 3)) | ||
| 428 | return _corner_dbl; | ||
| 429 | if (!strncmp(str, "DBR", 3) || !strncmp(str, "DRB", 3)) | ||
| 430 | return _corner_dbr; | ||
| 431 | |||
| 432 | return _error; | ||
| 433 | } | ||
| 434 | |||
| 435 | static uint64_t | ||
| 436 | readco(char *str) | ||
| 437 | { | ||
| 438 | if (*str == '0') | ||
| 439 | return 0ULL; | ||
| 440 | if (*str == '1') | ||
| 441 | return 1ULL; | ||
| 442 | if (*str == '2') | ||
| 443 | return 2ULL; | ||
| 444 | |||
| 445 | return _error; | ||
| 446 | } | ||
| 447 | |||
| 448 | cube_t | ||
| 449 | readcube(char *buf) | ||
| 450 | { | ||
| 451 | int i; | ||
| 452 | uint64_t piece, orient; | ||
| 453 | cube_t ret = {0}; | ||
| 454 | char *b = buf; | ||
| 455 | |||
| 456 | for (i = 0; i < 12; i++) { | ||
| 457 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 458 | b++; | ||
| 459 | if ((piece = readep(b)) == _error) | ||
| 460 | goto readcube_error; | ||
| 461 | b += 2; | ||
| 462 | if ((orient = readeo(b)) == _error) | ||
| 463 | goto readcube_error; | ||
| 464 | b++; | ||
| 465 | ret.e |= (piece << ESHIFT(i)) | (orient << EOSHIFT(i)); | ||
| 466 | } | ||
| 467 | for (i = 0; i < 8; i++) { | ||
| 468 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 469 | b++; | ||
| 470 | if ((piece = readcp(b)) == _error) | ||
| 471 | goto readcube_error; | ||
| 472 | b += 3; | ||
| 473 | if ((orient = readco(b)) == _error) | ||
| 474 | goto readcube_error; | ||
| 475 | b++; | ||
| 476 | ret.c |= (piece << CSHIFT(i)) | (orient << COSHIFT(i)); | ||
| 477 | } | ||
| 478 | |||
| 479 | return ret; | ||
| 480 | |||
| 481 | readcube_error: | ||
| 482 | return errorcube; | ||
| 483 | } | ||
| 484 | |||
| 485 | void | ||
| 486 | writecube(cube_t cube, char *buf) | ||
| 487 | { | ||
| 488 | char *errormsg; | ||
| 489 | uint64_t piece; | ||
| 490 | size_t len; | ||
| 491 | int i; | ||
| 492 | |||
| 493 | if (!isconsistent(cube)) { | ||
| 494 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 495 | goto writecube_error; | ||
| 496 | } | ||
| 497 | |||
| 498 | for (i = 0; i < 12; i++) { | ||
| 499 | piece = EPAT(cube.e, i); | ||
| 500 | buf[4*i ] = edgestr[piece][0]; | ||
| 501 | buf[4*i + 1] = edgestr[piece][1]; | ||
| 502 | buf[4*i + 2] = EOAT(cube.e, i) + '0'; | ||
| 503 | buf[4*i + 3] = ' '; | ||
| 504 | } | ||
| 505 | for (i = 0; i < 8; i++) { | ||
| 506 | piece = CPAT(cube.c, i); | ||
| 507 | buf[48 + 5*i ] = cornerstr[piece][0]; | ||
| 508 | buf[48 + 5*i + 1] = cornerstr[piece][1]; | ||
| 509 | buf[48 + 5*i + 2] = cornerstr[piece][2]; | ||
| 510 | buf[48 + 5*i + 3] = COAT(cube.c, i) + '0'; | ||
| 511 | buf[48 + 5*i + 4] = ' '; | ||
| 512 | } | ||
| 513 | |||
| 514 | buf[48+39] = '\0'; | ||
| 515 | |||
| 516 | return; | ||
| 517 | |||
| 518 | writecube_error: | ||
| 519 | len = strlen(errormsg); | ||
| 520 | strcpy(buf, errormsg); | ||
| 521 | buf[len] = '\n'; | ||
| 522 | buf[len+1] = '\0'; | ||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | static uint64_t | ||
| 527 | readmove(char c) | ||
| 528 | { | ||
| 529 | switch (c) { | ||
| 530 | case 'U': | ||
| 531 | return U; | ||
| 532 | case 'D': | ||
| 533 | return D; | ||
| 534 | case 'R': | ||
| 535 | return R; | ||
| 536 | case 'L': | ||
| 537 | return L; | ||
| 538 | case 'F': | ||
| 539 | return F; | ||
| 540 | case 'B': | ||
| 541 | return B; | ||
| 542 | default: | ||
| 543 | return _error; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | static uint64_t | ||
| 548 | readmodifier(char c) | ||
| 549 | { | ||
| 550 | switch (c) { | ||
| 551 | case '1': /* Fallthrough */ | ||
| 552 | case '2': /* Fallthrough */ | ||
| 553 | case '3': | ||
| 554 | return c - '0' - 1; | ||
| 555 | case '\'': | ||
| 556 | return 2; | ||
| 557 | default: | ||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | int | ||
| 563 | readmoves(char *buf, move_t *m) | ||
| 564 | { | ||
| 565 | int n; | ||
| 566 | uint64_t r; | ||
| 567 | char *b; | ||
| 568 | |||
| 569 | for (b = buf, n = 0; *b != '\0'; b++) { | ||
| 570 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 571 | b++; | ||
| 572 | if ((r = readmove(*b)) == _error) | ||
| 573 | return -1; | ||
| 574 | m[n] = (move_t)r; | ||
| 575 | if ((r = readmodifier(*(b+1))) != 0) { | ||
| 576 | b++; | ||
| 577 | m[n] += r; | ||
| 578 | } | ||
| 579 | n++; | ||
| 580 | } | ||
| 581 | |||
| 582 | return n; | ||
| 583 | } | ||
| 584 | |||
| 585 | void | ||
| 586 | writemoves(move_t *m, int n, char *buf) | ||
| 587 | { | ||
| 588 | int i; | ||
| 589 | char *b, *s; | ||
| 590 | |||
| 591 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 592 | s = movestr[m[i]]; | ||
| 593 | strcpy(b, s); | ||
| 594 | b += strlen(s); | ||
| 595 | *b = ' '; | ||
| 596 | } | ||
| 597 | *b = '\0'; | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | cube_t | ||
| 602 | move(move_t m, cube_t c) | ||
| 603 | { | ||
| 604 | /* TODO - not implemented yet */ | ||
| 605 | |||
| 606 | cube_t ret = {0}; | ||
| 607 | |||
| 608 | switch (m) { | ||
| 609 | case U: | ||
| 610 | return ret; | ||
| 611 | default: | ||
| 612 | return ret; | ||
| 613 | } | ||
| 614 | } | ||
diff --git a/src/cube.sync-conflict-20230524-182146-JOKKFPA.h b/src/cube.sync-conflict-20230524-182146-JOKKFPA.h new file mode 100644 index 0000000..2763d16 --- /dev/null +++ b/src/cube.sync-conflict-20230524-182146-JOKKFPA.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | typedef enum { | ||
| 2 | U, U2, U3, D, D2, D3, | ||
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | typedef struct { | ||
| 7 | uint64_t e; | ||
| 8 | uint64_t c; | ||
| 9 | } cube_t; | ||
| 10 | |||
| 11 | extern cube_t solvedcube; | ||
| 12 | extern cube_t errorcube; | ||
| 13 | |||
| 14 | bool isconsistent(cube_t); | ||
| 15 | bool issolved(cube_t); | ||
| 16 | |||
| 17 | cube_t readcube(char *); | ||
| 18 | void writecube(cube_t, char *); | ||
| 19 | |||
| 20 | int readmoves(char *, move_t *); | ||
| 21 | void writemoves(move_t *, int, char *); | ||
| 22 | |||
| 23 | /* | ||
| 24 | cube_t move(move_t, cube_t); | ||
| 25 | cube_t inverse(cube_t); | ||
| 26 | |||
| 27 | */ | ||
diff --git a/test/basic/all.in b/test/basic/all.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/basic/all.in | |||
diff --git a/test/basic/all.out b/test/basic/all.out new file mode 100644 index 0000000..d77f7f4 --- /dev/null +++ b/test/basic/all.out | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | Solved is consistent | ||
| 2 | Solved is solved | ||
| 3 | Zero is NOT consistent | ||
| 4 | Zero is NOT solved | ||
diff --git a/test/basic/basic_tests.c b/test/basic/basic_tests.c new file mode 100644 index 0000000..cc2a55c --- /dev/null +++ b/test/basic/basic_tests.c | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | #include "../../src/cube.h" | ||
| 6 | |||
| 7 | int main() { | ||
| 8 | cube_t z, s; | ||
| 9 | |||
| 10 | s = solvedcube; | ||
| 11 | printf("Solved %s consistent\n", isconsistent(s) ? "is" : "is NOT"); | ||
| 12 | printf("Solved %s solved\n", issolved(s) ? "is" : "is NOT"); | ||
| 13 | |||
| 14 | z = (cube_t){0}; | ||
| 15 | printf("Zero %s consistent\n", isconsistent(z) ? "is" : "is NOT"); | ||
| 16 | printf("Zero %s solved\n", issolved(z) ? "is" : "is NOT"); | ||
| 17 | |||
| 18 | return 0; | ||
| 19 | } | ||
diff --git a/test/io/00_garbage.in b/test/io/00_garbage.in new file mode 100644 index 0000000..9ac203a --- /dev/null +++ b/test/io/00_garbage.in | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | FUF0 UBBBR2 k4 | ||
| 2 | hello | ||
| 3 | garbage | ||
diff --git a/test/io/00_garbage.out b/test/io/00_garbage.out new file mode 100644 index 0000000..5b9114e --- /dev/null +++ b/test/io/00_garbage.out | |||
| @@ -0,0 +1 @@ | |||
| Error reading cube | |||
diff --git a/test/io/01_solved_oneline.in b/test/io/01_solved_oneline.in new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/io/01_solved_oneline.in | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/01_solved_oneline.out b/test/io/01_solved_oneline.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/io/01_solved_oneline.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/02_solved_oneline_whitespace.in b/test/io/02_solved_oneline_whitespace.in new file mode 100644 index 0000000..5d10368 --- /dev/null +++ b/test/io/02_solved_oneline_whitespace.in | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/02_solved_oneline_whitespace.out b/test/io/02_solved_oneline_whitespace.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/io/02_solved_oneline_whitespace.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/03_solved_multiline.in b/test/io/03_solved_multiline.in new file mode 100644 index 0000000..9f9652b --- /dev/null +++ b/test/io/03_solved_multiline.in | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | UF0 UB0 | ||
| 2 | DB0 DF0 UR0 UL0 DL0 | ||
| 3 | DR0 FR0 FL0 BL0 BR0 UFR0 | ||
| 4 | |||
| 5 | UBL0 DFL0 DBR0 | ||
| 6 | |||
| 7 | |||
| 8 | UFL0 UBR0 DFR0 | ||
| 9 | DBL0 | ||
diff --git a/test/io/03_solved_multiline.out b/test/io/03_solved_multiline.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/io/03_solved_multiline.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/04_inconsistent_ep.in b/test/io/04_inconsistent_ep.in new file mode 100644 index 0000000..8483962 --- /dev/null +++ b/test/io/04_inconsistent_ep.in | |||
| @@ -0,0 +1 @@ | |||
| UB0 UF0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/04_inconsistent_ep.out b/test/io/04_inconsistent_ep.out new file mode 100644 index 0000000..cb70902 --- /dev/null +++ b/test/io/04_inconsistent_ep.out | |||
| @@ -0,0 +1 @@ | |||
| Cube is inconsistent | |||
diff --git a/test/io/05_inconsistent_eo.in b/test/io/05_inconsistent_eo.in new file mode 100644 index 0000000..a92089d --- /dev/null +++ b/test/io/05_inconsistent_eo.in | |||
| @@ -0,0 +1 @@ | |||
| UF1 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/05_inconsistent_eo.out b/test/io/05_inconsistent_eo.out new file mode 100644 index 0000000..cb70902 --- /dev/null +++ b/test/io/05_inconsistent_eo.out | |||
| @@ -0,0 +1 @@ | |||
| Cube is inconsistent | |||
diff --git a/test/io/06_inconsistent_cp.in b/test/io/06_inconsistent_cp.in new file mode 100644 index 0000000..2085dd5 --- /dev/null +++ b/test/io/06_inconsistent_cp.in | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UBL0 UFR0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/06_inconsistent_cp.out b/test/io/06_inconsistent_cp.out new file mode 100644 index 0000000..cb70902 --- /dev/null +++ b/test/io/06_inconsistent_cp.out | |||
| @@ -0,0 +1 @@ | |||
| Cube is inconsistent | |||
diff --git a/test/io/07_inconsistent_co.in b/test/io/07_inconsistent_co.in new file mode 100644 index 0000000..abd74e2 --- /dev/null +++ b/test/io/07_inconsistent_co.in | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR1 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/io/07_inconsistent_co.out b/test/io/07_inconsistent_co.out new file mode 100644 index 0000000..cb70902 --- /dev/null +++ b/test/io/07_inconsistent_co.out | |||
| @@ -0,0 +1 @@ | |||
| Cube is inconsistent | |||
diff --git a/test/io/08_unsolved.in b/test/io/08_unsolved.in new file mode 100644 index 0000000..c992ce7 --- /dev/null +++ b/test/io/08_unsolved.in | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | UL1 DR0 UB0 DL1 | ||
| 2 | UR0 FR1 DB0 BR0 | ||
| 3 | UF1 DF0 BL0 FL0 | ||
| 4 | |||
| 5 | UBL0 DLF1 DBR2 UFR1 | ||
| 6 | DFR1 UBR1 UFL0 DBL0 | ||
diff --git a/test/io/08_unsolved.out b/test/io/08_unsolved.out new file mode 100644 index 0000000..8c132cf --- /dev/null +++ b/test/io/08_unsolved.out | |||
| @@ -0,0 +1 @@ | |||
| UL1 DR0 UB0 DL1 UR0 FR1 DB0 BR0 UF1 DF0 BL0 FL0 UBL0 DFL1 DBR2 UFR1 DFR1 UBR1 UFL0 DBL0 | |||
diff --git a/test/io/io_tests.c b/test/io/io_tests.c new file mode 100644 index 0000000..1ef99d0 --- /dev/null +++ b/test/io/io_tests.c | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | #include "../../src/cube.h" | ||
| 6 | |||
| 7 | #define STRLENMAX 10000 | ||
| 8 | |||
| 9 | int main() { | ||
| 10 | char *c, str[STRLENMAX]; | ||
| 11 | cube_t cube; | ||
| 12 | |||
| 13 | for (c = str; (*c = getchar()) != EOF; c++) ; | ||
| 14 | *c = '\0'; | ||
| 15 | |||
| 16 | cube = readcube(str); | ||
| 17 | |||
| 18 | if (cube.e == errorcube.e && cube.c == errorcube.c) { | ||
| 19 | printf("Error reading cube\n"); | ||
| 20 | } else if (!isconsistent(cube)) { | ||
| 21 | printf("Cube is inconsistent\n"); | ||
| 22 | } else { | ||
| 23 | writecube(cube, str); | ||
| 24 | printf("%s\n", str); | ||
| 25 | } | ||
| 26 | |||
| 27 | return 0; | ||
| 28 | } | ||
diff --git a/test/move/000_nomove_solved.in b/test/move/000_nomove_solved.in new file mode 100644 index 0000000..07cf178 --- /dev/null +++ b/test/move/000_nomove_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | |||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/000_nomove_solved.out b/test/move/000_nomove_solved.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/move/000_nomove_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/move/001_nomove_scrambled.in b/test/move/001_nomove_scrambled.in new file mode 100644 index 0000000..fd1d23c --- /dev/null +++ b/test/move/001_nomove_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | |||
| 2 | BR0 DF1 DL1 FR0 UB1 DR1 UF0 UR0 UL0 FL1 BL0 DB1 UBL0 DBR1 UFL1 DFR2 UFR0 UBR1 DBL1 DFL0 | ||
diff --git a/test/move/001_nomove_scrambled.out b/test/move/001_nomove_scrambled.out new file mode 100644 index 0000000..bd2cca0 --- /dev/null +++ b/test/move/001_nomove_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| BR0 DF1 DL1 FR0 UB1 DR1 UF0 UR0 UL0 FL1 BL0 DB1 UBL0 DBR1 UFL1 DFR2 UFR0 UBR1 DBL1 DFL0 | |||
diff --git a/test/move/010_U_solved.in b/test/move/010_U_solved.in new file mode 100644 index 0000000..62495cb --- /dev/null +++ b/test/move/010_U_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/010_U_solved.out b/test/move/010_U_solved.out new file mode 100644 index 0000000..b5b36ad --- /dev/null +++ b/test/move/010_U_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UR0 UL0 DB0 DF0 UB0 UF0 DL0 DR0 FR0 FL0 BL0 BR0 UBR0 UFL0 DFL0 DBR0 UFR0 UBL0 DFR0 DBL0 | |||
diff --git a/test/move/011_U_scrambled.in b/test/move/011_U_scrambled.in new file mode 100644 index 0000000..012faaf --- /dev/null +++ b/test/move/011_U_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U | ||
| 2 | BR0 DF1 DL1 FR0 UB1 DR1 UF0 UR0 UL0 FL1 BL0 DB1 UBL0 DBR1 UFL1 DFR2 UFR0 UBR1 DBL1 DFL0 | ||
diff --git a/test/move/011_U_scrambled.out b/test/move/011_U_scrambled.out new file mode 100644 index 0000000..46d2ef7 --- /dev/null +++ b/test/move/011_U_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| UB1 DR1 DL1 FR0 DF1 BR0 UF0 UR0 UL0 FL1 BL0 DB1 UBR1 UFR0 UFL1 DFR2 UBL0 DBR1 DBL1 DFL0 | |||
diff --git a/test/move/012_U_inverse.in b/test/move/012_U_inverse.in new file mode 100644 index 0000000..2aa7ffa --- /dev/null +++ b/test/move/012_U_inverse.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U | ||
| 2 | UL0 UR0 DB0 DF0 UF0 UB0 DL0 DR0 FR0 FL0 BL0 BR0 UFL0 UBR0 DFL0 DBR0 UBL0 UFR0 DFR0 DBL0 | ||
diff --git a/test/move/012_U_inverse.out b/test/move/012_U_inverse.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/move/012_U_inverse.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/move/020_U2_solved.in b/test/move/020_U2_solved.in new file mode 100644 index 0000000..ccc84ef --- /dev/null +++ b/test/move/020_U2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/020_U2_solved.out b/test/move/020_U2_solved.out new file mode 100644 index 0000000..316ad57 --- /dev/null +++ b/test/move/020_U2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UB0 UF0 DB0 DF0 UL0 UR0 DL0 DR0 FR0 FL0 BL0 BR0 UBL0 UFR0 DFL0 DBR0 UBR0 UFL0 DFR0 DBL0 | |||
diff --git a/test/move/021_U2_scrambled.in b/test/move/021_U2_scrambled.in new file mode 100644 index 0000000..bdc034e --- /dev/null +++ b/test/move/021_U2_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U2 | ||
| 2 | BR0 DF1 DL1 FR0 UB1 DR1 UF0 UR0 UL0 FL1 BL0 DB1 UBL0 DBR1 UFL1 DFR2 UFR0 UBR1 DBL1 DFL0 | ||
diff --git a/test/move/021_U2_scrambled.out b/test/move/021_U2_scrambled.out new file mode 100644 index 0000000..c89ca1f --- /dev/null +++ b/test/move/021_U2_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| DF1 BR0 DL1 FR0 DR1 UB1 UF0 UR0 UL0 FL1 BL0 DB1 DBR1 UBL0 UFL1 DFR2 UBR1 UFR0 DBL1 DFL0 | |||
diff --git a/test/move/022_U2_inverse.in b/test/move/022_U2_inverse.in new file mode 100644 index 0000000..8ace160 --- /dev/null +++ b/test/move/022_U2_inverse.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U2 | ||
| 2 | UB0 UF0 DB0 DF0 UL0 UR0 DL0 DR0 FR0 FL0 BL0 BR0 UBL0 UFR0 DFL0 DBR0 UBR0 UFL0 DFR0 DBL0 | ||
diff --git a/test/move/022_U2_inverse.out b/test/move/022_U2_inverse.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/move/022_U2_inverse.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/move/030_U3_solved.in b/test/move/030_U3_solved.in new file mode 100644 index 0000000..49a8847 --- /dev/null +++ b/test/move/030_U3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/030_U3_solved.out b/test/move/030_U3_solved.out new file mode 100644 index 0000000..7721ab5 --- /dev/null +++ b/test/move/030_U3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UL0 UR0 DB0 DF0 UF0 UB0 DL0 DR0 FR0 FL0 BL0 BR0 UFL0 UBR0 DFL0 DBR0 UBL0 UFR0 DFR0 DBL0 | |||
diff --git a/test/move/031_U3_inverse.in b/test/move/031_U3_inverse.in new file mode 100644 index 0000000..44b6ce3 --- /dev/null +++ b/test/move/031_U3_inverse.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U3 | ||
| 2 | UR0 UL0 DB0 DF0 UB0 UF0 DL0 DR0 FR0 FL0 BL0 BR0 UBR0 UFL0 DFL0 DBR0 UFR0 UBL0 DFR0 DBL0 | ||
diff --git a/test/move/031_U3_inverse.out b/test/move/031_U3_inverse.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/move/031_U3_inverse.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/move/032_U3_scrambled.in b/test/move/032_U3_scrambled.in new file mode 100644 index 0000000..a00608c --- /dev/null +++ b/test/move/032_U3_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U3 | ||
| 2 | UF1 BR0 DL1 UR1 FR1 FL0 UL0 BL1 DR0 DF1 DB0 UB0 UBR2 DFL1 UFL0 DBR1 UBL2 DFR0 DBL2 UFR1 | ||
diff --git a/test/move/032_U3_scrambled.out b/test/move/032_U3_scrambled.out new file mode 100644 index 0000000..7d1e8cd --- /dev/null +++ b/test/move/032_U3_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| FL0 FR1 DL1 UR1 UF1 BR0 UL0 BL1 DR0 DF1 DB0 UB0 UBL2 DFR0 UFL0 DBR1 DFL1 UBR2 DBL2 UFR1 | |||
diff --git a/test/move/033_U3_with_prime.in b/test/move/033_U3_with_prime.in new file mode 100644 index 0000000..95e5405 --- /dev/null +++ b/test/move/033_U3_with_prime.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | U' | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/033_U3_with_prime.out b/test/move/033_U3_with_prime.out new file mode 100644 index 0000000..7721ab5 --- /dev/null +++ b/test/move/033_U3_with_prime.out | |||
| @@ -0,0 +1 @@ | |||
| UL0 UR0 DB0 DF0 UF0 UB0 DL0 DR0 FR0 FL0 BL0 BR0 UFL0 UBR0 DFL0 DBR0 UBL0 UFR0 DFR0 DBL0 | |||
diff --git a/test/move/040_D_solved.in b/test/move/040_D_solved.in new file mode 100644 index 0000000..cb5edb7 --- /dev/null +++ b/test/move/040_D_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | D | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/040_D_solved.out b/test/move/040_D_solved.out new file mode 100644 index 0000000..cf4f816 --- /dev/null +++ b/test/move/040_D_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DR0 DL0 UR0 UL0 DB0 DF0 FR0 FL0 BL0 BR0 UFR0 UBL0 DBL0 DFR0 UFL0 UBR0 DFL0 DBR0 | |||
diff --git a/test/move/050_D2_solved.in b/test/move/050_D2_solved.in new file mode 100644 index 0000000..56f47cd --- /dev/null +++ b/test/move/050_D2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | D2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/050_D2_solved.out b/test/move/050_D2_solved.out new file mode 100644 index 0000000..13b229e --- /dev/null +++ b/test/move/050_D2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DF0 DB0 UR0 UL0 DR0 DL0 FR0 FL0 BL0 BR0 UFR0 UBL0 DBR0 DFL0 UFL0 UBR0 DBL0 DFR0 | |||
diff --git a/test/move/060_D3_solved.in b/test/move/060_D3_solved.in new file mode 100644 index 0000000..64d1dd8 --- /dev/null +++ b/test/move/060_D3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | D3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/060_D3_solved.out b/test/move/060_D3_solved.out new file mode 100644 index 0000000..2864f5b --- /dev/null +++ b/test/move/060_D3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DL0 DR0 UR0 UL0 DF0 DB0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFR0 DBL0 UFL0 UBR0 DBR0 DFL0 | |||
diff --git a/test/move/070_R_solved.in b/test/move/070_R_solved.in new file mode 100644 index 0000000..108538a --- /dev/null +++ b/test/move/070_R_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | R | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/070_R_solved.out b/test/move/070_R_solved.out new file mode 100644 index 0000000..8c8fcb3 --- /dev/null +++ b/test/move/070_R_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 FR0 UL0 DL0 BR0 DR0 FL0 BL0 UR0 DFR2 UBL0 DFL0 UBR2 UFL0 UFR1 DBR1 DBL0 | |||
diff --git a/test/move/071_R_scrambled.in b/test/move/071_R_scrambled.in new file mode 100644 index 0000000..0c9dcaf --- /dev/null +++ b/test/move/071_R_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | R | ||
| 2 | UF1 BR0 DL1 UR1 FR1 FL0 UL0 BL1 DR0 DF1 DB0 UB0 UBR2 DFL1 UFL0 DBR1 UBL2 DFR0 DBL2 UFR1 | ||
diff --git a/test/move/071_R_scrambled.out b/test/move/071_R_scrambled.out new file mode 100644 index 0000000..650bcc8 --- /dev/null +++ b/test/move/071_R_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| UF1 BR0 DL1 UR1 DR0 FL0 UL0 UB0 BL1 DF1 DB0 FR1 DBL1 DFL1 UFL0 DFR2 UBL2 UBR0 DBR2 UFR1 | |||
diff --git a/test/move/080_R2_solved.in b/test/move/080_R2_solved.in new file mode 100644 index 0000000..554a084 --- /dev/null +++ b/test/move/080_R2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | R2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/080_R2_solved.out b/test/move/080_R2_solved.out new file mode 100644 index 0000000..90765e2 --- /dev/null +++ b/test/move/080_R2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 DR0 UL0 DL0 UR0 BR0 FL0 BL0 FR0 DBR0 UBL0 DFL0 UFR0 UFL0 DFR0 UBR0 DBL0 | |||
diff --git a/test/move/090_R3_solved.in b/test/move/090_R3_solved.in new file mode 100644 index 0000000..ca6cfd8 --- /dev/null +++ b/test/move/090_R3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | R3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/090_R3_solved.out b/test/move/090_R3_solved.out new file mode 100644 index 0000000..d4abffc --- /dev/null +++ b/test/move/090_R3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 BR0 UL0 DL0 FR0 UR0 FL0 BL0 DR0 UBR2 UBL0 DFL0 DFR2 UFL0 DBR1 UFR1 DBL0 | |||
diff --git a/test/move/100_L_solved.in b/test/move/100_L_solved.in new file mode 100644 index 0000000..14ebb5f --- /dev/null +++ b/test/move/100_L_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | L | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/100_L_solved.out b/test/move/100_L_solved.out new file mode 100644 index 0000000..0b0565c --- /dev/null +++ b/test/move/100_L_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 BL0 FL0 DR0 FR0 UL0 DL0 BR0 UFR0 DBL2 UFL2 DBR0 UBL1 UBR0 DFR0 DFL1 | |||
diff --git a/test/move/110_L2_solved.in b/test/move/110_L2_solved.in new file mode 100644 index 0000000..8224bcd --- /dev/null +++ b/test/move/110_L2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | L2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/110_L2_solved.out b/test/move/110_L2_solved.out new file mode 100644 index 0000000..2498c87 --- /dev/null +++ b/test/move/110_L2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 DL0 UL0 DR0 FR0 BL0 FL0 BR0 UFR0 DFL0 UBL0 DBR0 DBL0 UBR0 DFR0 UFL0 | |||
diff --git a/test/move/120_L3_solved.in b/test/move/120_L3_solved.in new file mode 100644 index 0000000..552d319 --- /dev/null +++ b/test/move/120_L3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | L3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/120_L3_solved.out b/test/move/120_L3_solved.out new file mode 100644 index 0000000..2ac2912 --- /dev/null +++ b/test/move/120_L3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 FL0 BL0 DR0 FR0 DL0 UL0 BR0 UFR0 UFL2 DBL2 DBR0 DFL1 UBR0 DFR0 UBL1 | |||
diff --git a/test/move/130_F_solved.in b/test/move/130_F_solved.in new file mode 100644 index 0000000..abdbd35 --- /dev/null +++ b/test/move/130_F_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/130_F_solved.out b/test/move/130_F_solved.out new file mode 100644 index 0000000..e805af8 --- /dev/null +++ b/test/move/130_F_solved.out | |||
| @@ -0,0 +1 @@ | |||
| FL1 UB0 DB0 FR1 UR0 UL0 DL0 DR0 UF1 DF1 BL0 BR0 UFL1 UBL0 DFR1 DBR0 DFL2 UBR0 UFR2 DBL0 | |||
diff --git a/test/move/132_F_scrambled.in b/test/move/132_F_scrambled.in new file mode 100644 index 0000000..8b15575 --- /dev/null +++ b/test/move/132_F_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F | ||
| 2 | DL1 BR0 DR0 UR1 DF0 FL1 BL0 UL0 FR0 UF0 DB1 UB0 UFR0 DBL1 DBR0 UFL1 DFR1 DFL1 UBL2 UBR0 | ||
diff --git a/test/move/132_F_scrambled.out b/test/move/132_F_scrambled.out new file mode 100644 index 0000000..3aea663 --- /dev/null +++ b/test/move/132_F_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| UF1 BR0 DR0 FR1 DF0 FL1 BL0 UL0 DL0 UR0 DB1 UB0 DFR2 DBL1 UBL0 UFL1 DBR2 DFL1 UFR2 UBR0 | |||
diff --git a/test/move/133_F_scrambled_2.in b/test/move/133_F_scrambled_2.in new file mode 100644 index 0000000..fdd5139 --- /dev/null +++ b/test/move/133_F_scrambled_2.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F | ||
| 2 | BL1 DB0 UL1 DF0 BR1 UF1 DL0 FL1 UB0 DR1 FR1 UR1 UBR2 UBL1 DFR2 DBL2 DBR0 DFL0 UFR0 UFL2 | ||
diff --git a/test/move/133_F_scrambled_2.out b/test/move/133_F_scrambled_2.out new file mode 100644 index 0000000..3546894 --- /dev/null +++ b/test/move/133_F_scrambled_2.out | |||
| @@ -0,0 +1 @@ | |||
| DR0 DB0 UL1 UB1 BR1 UF1 DL0 FL1 BL0 DF1 FR1 UR1 DBR1 UBL1 UFR1 DBL2 DFR1 DFL0 UBR1 UFL2 | |||
diff --git a/test/move/140_F2_solved.in b/test/move/140_F2_solved.in new file mode 100644 index 0000000..83aab30 --- /dev/null +++ b/test/move/140_F2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/140_F2_solved.out b/test/move/140_F2_solved.out new file mode 100644 index 0000000..8aa701f --- /dev/null +++ b/test/move/140_F2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| DF0 UB0 DB0 UF0 UR0 UL0 DL0 DR0 FL0 FR0 BL0 BR0 DFL0 UBL0 UFR0 DBR0 DFR0 UBR0 UFL0 DBL0 | |||
diff --git a/test/move/150_F3_solved.in b/test/move/150_F3_solved.in new file mode 100644 index 0000000..2c766c1 --- /dev/null +++ b/test/move/150_F3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/150_F3_solved.out b/test/move/150_F3_solved.out new file mode 100644 index 0000000..40f1260 --- /dev/null +++ b/test/move/150_F3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| FR1 UB0 DB0 FL1 UR0 UL0 DL0 DR0 DF1 UF1 BL0 BR0 DFR1 UBL0 UFL1 DBR0 UFR2 UBR0 DFL2 DBL0 | |||
diff --git a/test/move/160_B_solved.in b/test/move/160_B_solved.in new file mode 100644 index 0000000..6062e45 --- /dev/null +++ b/test/move/160_B_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | B | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/160_B_solved.out b/test/move/160_B_solved.out new file mode 100644 index 0000000..f7fb13c --- /dev/null +++ b/test/move/160_B_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 BR1 BL1 DF0 UR0 UL0 DL0 DR0 FR0 FL0 UB1 DB1 UFR0 UBR1 DFL0 DBL1 UFL0 DBR2 DFR0 UBL2 | |||
diff --git a/test/move/170_B2_solved.in b/test/move/170_B2_solved.in new file mode 100644 index 0000000..0ff1190 --- /dev/null +++ b/test/move/170_B2_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | B2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/170_B2_solved.out b/test/move/170_B2_solved.out new file mode 100644 index 0000000..9b33e35 --- /dev/null +++ b/test/move/170_B2_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 DB0 UB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BR0 BL0 UFR0 DBR0 DFL0 UBL0 UFL0 DBL0 DFR0 UBR0 | |||
diff --git a/test/move/180_B3_solved.in b/test/move/180_B3_solved.in new file mode 100644 index 0000000..c59636f --- /dev/null +++ b/test/move/180_B3_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | B3 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/180_B3_solved.out b/test/move/180_B3_solved.out new file mode 100644 index 0000000..1367517 --- /dev/null +++ b/test/move/180_B3_solved.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 BL1 BR1 DF0 UR0 UL0 DL0 DR0 FR0 FL0 DB1 UB1 UFR0 DBL1 DFL0 UBR1 UFL0 UBL2 DFR0 DBR2 | |||
diff --git a/test/move/200_scramble_solved.in b/test/move/200_scramble_solved.in new file mode 100644 index 0000000..939d876 --- /dev/null +++ b/test/move/200_scramble_solved.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | B' D' B' R2 B' R F D F2 U2 B D2 F R2 F2 L2 F L2 U2 | ||
| 2 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
diff --git a/test/move/200_scramble_solved.out b/test/move/200_scramble_solved.out new file mode 100644 index 0000000..690ff6b --- /dev/null +++ b/test/move/200_scramble_solved.out | |||
| @@ -0,0 +1 @@ | |||
| BR0 DF0 FL1 UB0 BL0 FR1 UR1 UL0 DR1 DB0 UF1 DL1 DFL0 DFR0 UFL1 UBL2 DBR1 DBL0 UFR1 UBR1 | |||
diff --git a/test/move/201_solution_scrambled.in b/test/move/201_solution_scrambled.in new file mode 100644 index 0000000..a838b4c --- /dev/null +++ b/test/move/201_solution_scrambled.in | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | F' B' D2 U2 F2 U' L' U D B D' R2 B' L' R' D' R D L2 U' L' B L L U F U' F' U F U' F2 L' F U F U' F' U2 F2 R2 L2 B2 | ||
| 2 | BR0 DF0 FL1 UB0 BL0 FR1 UR1 UL0 DR1 DB0 UF1 DL1 DFL0 DFR0 UFL1 UBL2 DBR1 DBL0 UFR1 UBR1 | ||
diff --git a/test/move/201_solution_scrambled.out b/test/move/201_solution_scrambled.out new file mode 100644 index 0000000..dff224d --- /dev/null +++ b/test/move/201_solution_scrambled.out | |||
| @@ -0,0 +1 @@ | |||
| UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | |||
diff --git a/test/move/move_tests.c b/test/move/move_tests.c new file mode 100644 index 0000000..23fc45e --- /dev/null +++ b/test/move/move_tests.c | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | #include "../../src/cube.h" | ||
| 6 | |||
| 7 | #define STRLENMAX 10000 | ||
| 8 | #define MOVESMAX 1000 | ||
| 9 | |||
| 10 | int main() { | ||
| 11 | char str[STRLENMAX]; | ||
| 12 | int i, n; | ||
| 13 | move_t moves[MOVESMAX]; | ||
| 14 | cube_t cube; | ||
| 15 | |||
| 16 | fgets(str, STRLENMAX, stdin); | ||
| 17 | n = readmoves(str, moves); | ||
| 18 | |||
| 19 | if (n == -1) { | ||
| 20 | printf("Error reading moves\n"); | ||
| 21 | return 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | fgets(str, STRLENMAX, stdin); | ||
| 25 | cube = readcube(str); | ||
| 26 | |||
| 27 | for (i = 0; i < n; i++) | ||
| 28 | cube = move(cube, moves[i]); | ||
| 29 | |||
| 30 | if (cube.e == errorcube.e && cube.c == errorcube.c) { | ||
| 31 | printf("Error moving cube\n"); | ||
| 32 | } else if (!isconsistent(cube)) { | ||
| 33 | printf("Moved cube is inconsistent\n"); | ||
| 34 | } else { | ||
| 35 | writecube(cube, str); | ||
| 36 | printf("%s\n", str); | ||
| 37 | } | ||
| 38 | |||
| 39 | return 0; | ||
| 40 | } | ||
diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..d59270b --- /dev/null +++ b/test/test.sh | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | CC="cc -DDEBUG -std=c99 -pthread -pedantic -Wall -Wextra \ | ||
| 4 | -Wno-unused-parameter -Wno-unused-function -g3" | ||
| 5 | if [ $(uname) != "OpenBSD" ]; then | ||
| 6 | CC="$CC -fsanitize=address -fsanitize=undefined" | ||
| 7 | fi | ||
| 8 | SRC="src/cube.c" | ||
| 9 | TESTBIN="test/run" | ||
| 10 | TESTOUT="test/last.out" | ||
| 11 | TESTERR="test/last.err" | ||
| 12 | |||
| 13 | for t in test/*; do | ||
| 14 | if [ ! -d $t ]; then continue; fi | ||
| 15 | $CC -o $TESTBIN $t/*.c $SRC || exit 1; | ||
| 16 | for cin in $t/*.in; do | ||
| 17 | c=$(echo "$cin" | sed 's/\.in//') | ||
| 18 | cout=$c.out | ||
| 19 | printf "$c: " | ||
| 20 | $TESTBIN < "$cin" > $TESTOUT 2> $TESTERR | ||
| 21 | if diff $cout $TESTOUT; then | ||
| 22 | printf "OK\n" | ||
| 23 | else | ||
| 24 | printf "Test failed! stderr:\n" | ||
| 25 | cat $TESTERR | ||
| 26 | exit 1 | ||
| 27 | fi | ||
| 28 | done | ||
| 29 | done | ||
| 30 | |||
| 31 | echo "All tests passed!" | ||
| 32 | rm -rf $TESTBIN $TESTOUT $TESTERR | ||
