Advertisement
Guest User

Rubik LL bruteforcer

a guest
Apr 4th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. #define MAX_DEPTH 9
  6. #define PIECE_COUNT 52
  7. #define STATIC_COUNT 32
  8. #define VALID_MOVES 18
  9. #define VALID_MOVE_SET 6
  10.  
  11. using namespace std;
  12.  
  13. const char defaultState[] = {
  14.     0, 0, 0, 0, 0, 0, 0, 0,
  15.     1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4,
  16.     1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4,
  17.     1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4,
  18.     5, 5, 5, 5, 5, 5, 5, 5,
  19. };
  20.  
  21. struct level {
  22.     char moveId = 0;
  23.     bool tested = false;
  24.     char state[PIECE_COUNT] = { 0 };
  25. };
  26.  
  27. string moveIdName(char moveId) {
  28.     switch (moveId) {
  29.     case 0:
  30.         return "R";
  31.     case 1:
  32.         return "U";
  33.     case 2:
  34.         return "D";
  35.     case 3:
  36.         return "L";
  37.     case 4:
  38.         return "F";
  39.     case 5:
  40.         return "B";
  41.     case 6:
  42.         return "R2";
  43.     case 7:
  44.         return "U2";
  45.     case 8:
  46.         return "D2";
  47.     case 9:
  48.         return "L2";
  49.     case 10:
  50.         return "F2";
  51.     case 11:
  52.         return "B2";
  53.     case 12:
  54.         return "R'";
  55.     case 13:
  56.         return "U'";
  57.     case 14:
  58.         return "D'";
  59.     case 15:
  60.         return "L'";
  61.     case 16:
  62.         return "F'";
  63.     case 17:
  64.         return "B'";
  65.     default:
  66.         return "?";
  67.     }
  68. }
  69.  
  70. void printState(char (&actual)[PIECE_COUNT]) {    
  71.     cout << "WHITE:  ";
  72.     for (int i = 0; i < 8; i++) {
  73.         cout << (int)actual[i] << "; ";
  74.     }
  75.     for (int j = 0; j < 3; j++) {
  76.         cout << "\nSLICE" << j << ": ";
  77.         for (int i = 8 + j * 12; i < 20 + j * 12; i++) {
  78.             cout << (int)actual[i] << "; ";
  79.         }
  80.     }
  81.     cout << "\nYELLOW: ";
  82.     for (int i = 44; i < 52; i++) {
  83.         cout << (int)actual[i] << "; ";
  84.     }
  85.     cout << "\n";
  86. }
  87.  
  88. void applyMove(char (&source)[PIECE_COUNT], char (&target)[PIECE_COUNT], char moveId) {
  89.     char temp[PIECE_COUNT] = { 0 };
  90.     for (int i = 0; i < PIECE_COUNT; i++) {
  91.         temp[i] = source[i];
  92.     }
  93.     switch (moveId % VALID_MOVE_SET) {
  94.     case 0: // R
  95.         // d
  96.         temp[2] = source[34];
  97.         temp[3] = source[22];
  98.         temp[4] = source[10];
  99.         // f
  100.         temp[14] = source[2];
  101.         temp[26] = source[3];
  102.         temp[38] = source[4];
  103.         // u
  104.         temp[48] = source[14];
  105.         temp[47] = source[26];
  106.         temp[46] = source[38];
  107.         // b
  108.         temp[34] = source[48];
  109.         temp[22] = source[47];
  110.         temp[10] = source[46];
  111.         // r
  112.         temp[11] = source[35];
  113.         temp[12] = source[23];
  114.         temp[13] = source[11];
  115.         temp[25] = source[12];
  116.         temp[37] = source[13];
  117.         temp[36] = source[25];
  118.         temp[35] = source[37];
  119.         temp[23] = source[36];
  120.         break;
  121.     case 1: // U
  122.         // u (yellow)
  123.         temp[44] = source[50];
  124.         temp[45] = source[51];
  125.         temp[46] = source[44];
  126.         temp[47] = source[45];
  127.         temp[48] = source[46];
  128.         temp[49] = source[47];
  129.         temp[50] = source[48];
  130.         temp[51] = source[49];
  131.         // top slice
  132.         temp[32] = source[41];
  133.         temp[33] = source[42];
  134.         temp[34] = source[43];
  135.         temp[35] = source[32];
  136.         temp[36] = source[33];
  137.         temp[37] = source[34];
  138.         temp[38] = source[35];
  139.         temp[39] = source[36];
  140.         temp[40] = source[37];
  141.         temp[41] = source[38];
  142.         temp[42] = source[39];
  143.         temp[43] = source[40];
  144.         break;
  145.     case 2: // D
  146.         // d (white)
  147.         temp[0] = source[2];
  148.         temp[1] = source[3];
  149.         temp[2] = source[4];
  150.         temp[3] = source[5];
  151.         temp[4] = source[6];
  152.         temp[5] = source[7];
  153.         temp[6] = source[0];
  154.         temp[7] = source[1];
  155.         // top slice
  156.         temp[8] = source[11];
  157.         temp[9] = source[12];
  158.         temp[10] = source[13];
  159.         temp[11] = source[14];
  160.         temp[12] = source[15];
  161.         temp[13] = source[16];
  162.         temp[14] = source[17];
  163.         temp[15] = source[18];
  164.         temp[16] = source[19];
  165.         temp[17] = source[8];
  166.         temp[18] = source[9];
  167.         temp[19] = source[10];
  168.         break;
  169.     case 3: // L
  170.         // d
  171.         temp[6] = source[40];
  172.         temp[7] = source[28];
  173.         temp[0] = source[16];
  174.         // f
  175.         temp[40] = source[44];
  176.         temp[28] = source[51];
  177.         temp[16] = source[50];
  178.         // u
  179.         temp[44] = source[8];
  180.         temp[51] = source[20];
  181.         temp[50] = source[32];
  182.         // b
  183.         temp[8] = source[6];
  184.         temp[20] = source[7];
  185.         temp[32] = source[0];
  186.         // l
  187.         temp[17] = source[41];
  188.         temp[18] = source[29];
  189.         temp[19] = source[17];
  190.         temp[31] = source[18];
  191.         temp[43] = source[19];
  192.         temp[42] = source[31];
  193.         temp[41] = source[43];
  194.         temp[29] = source[42];
  195.         break;
  196.     case 4: // F
  197.         // d
  198.         temp[4] = source[37];
  199.         temp[5] = source[25];
  200.         temp[6] = source[13];
  201.         // r
  202.         temp[37] = source[50];
  203.         temp[25] = source[49];
  204.         temp[13] = source[48];
  205.         // u
  206.         temp[50] = source[17];
  207.         temp[49] = source[29];
  208.         temp[48] = source[41];
  209.         // l
  210.         temp[17] = source[4];
  211.         temp[29] = source[5];
  212.         temp[41] = source[6];
  213.         // f
  214.         temp[14] = source[38];
  215.         temp[15] = source[26];
  216.         temp[16] = source[14];
  217.         temp[28] = source[15];
  218.         temp[40] = source[16];
  219.         temp[39] = source[28];
  220.         temp[38] = source[40];
  221.         temp[26] = source[39];
  222.         break;
  223.     case 5: // B
  224.         // 0-7 - white face
  225.         // 8-19 - bottom slice
  226.         // 20-31 - middle slice
  227.         // 32-43 - top slice
  228.         // 44-51 - yellow face
  229.         // d
  230.         temp[0] = source[43];
  231.         temp[1] = source[31];
  232.         temp[2] = source[19];
  233.         // r
  234.         temp[43] = source[46];
  235.         temp[31] = source[45];
  236.         temp[19] = source[44];
  237.         // u
  238.         temp[46] = source[11];
  239.         temp[45] = source[23];
  240.         temp[44] = source[35];
  241.         // l
  242.         temp[11] = source[0];
  243.         temp[23] = source[1];
  244.         temp[35] = source[2];
  245.         // b
  246.         temp[8] = source[32];
  247.         temp[9] = source[20];
  248.         temp[10] = source[8];
  249.         temp[22] = source[9];
  250.         temp[34] = source[10];
  251.         temp[33] = source[22];
  252.         temp[32] = source[34];
  253.         temp[20] = source[33];
  254.         break;
  255.     default:
  256.         break;
  257.     }
  258.     if (moveId < VALID_MOVE_SET) {
  259.         for (int i = 0; i < PIECE_COUNT; i++) {
  260.             target[i] = temp[i];
  261.         }
  262.     }
  263.     else {
  264.         applyMove(temp, target, moveId - VALID_MOVE_SET);
  265.     }
  266. }
  267.  
  268. void findLL() {
  269.     unsigned char depth = 0;
  270.     level levels[MAX_DEPTH];
  271.     for (unsigned char i = 0; i < PIECE_COUNT; i++) {
  272.         levels[0].state[i] = defaultState[i];
  273.     }
  274.    
  275.     /*unsigned char toApply[] = {7, 255};
  276.     for (unsigned char i = 0; toApply[i] != 255; i++) {
  277.         applyMove(levels[0].state, levels[1].state, toApply[i]);
  278.         for (int j = 0; j < PIECE_COUNT; j++) {
  279.             levels[0].state[j] = levels[1].state[j];
  280.         }
  281.         printState(levels[0].state);
  282.     }
  283.     return;*/
  284.    
  285.     while (depth < 255) {
  286.         short moveId = levels[depth].moveId;
  287.         if ((depth > 0 && (moveId % VALID_MOVE_SET) == (levels[depth - 1].moveId % VALID_MOVE_SET))) {
  288.             levels[depth].moveId++;
  289.             moveId++;
  290.         }
  291.         if (moveId >= VALID_MOVES) {
  292.             depth--;
  293.             if (depth < 255) {
  294.                 levels[depth].moveId++;
  295.             }
  296.             continue;
  297.         }
  298.        
  299.         // check if LL alg
  300.         if (!levels[depth].tested) {
  301.             char (&actual)[PIECE_COUNT] = levels[depth].state;
  302.             bool isStatic = true;
  303.             for (int j = 0; j < STATIC_COUNT; j++) {
  304.                 if (actual[j] != defaultState[j]) {
  305.                     isStatic = false;
  306.                 }
  307.             }
  308.             if (isStatic) {
  309.                 bool llChanged = false;
  310.                 bool isPll = true;
  311.                 for (int j = 44; j < 51; j++) {
  312.                     if (actual[j] != 5) {
  313.                         llChanged = true;
  314.                         isPll = false;
  315.                     }
  316.                 }
  317.                 for (int j = 32; j < 44; j += 3) {
  318.                     if (actual[j] != actual[j + 1] || actual[j] != actual[j + 2]) {
  319.                         llChanged = true;
  320.                     }
  321.                 }
  322.                 if (llChanged) {
  323.                     if (isPll) {
  324.                         cout << "PLL: ";
  325.                     }
  326.                     for (int i = 0; i < depth; i++) {
  327.                         cout << moveIdName(levels[i].moveId) << ' ';
  328.                     }
  329.                     cout << "\n";
  330.                     printState(levels[depth].state);
  331.                 }
  332.             }
  333.             levels[depth].tested = true;
  334.         }
  335.        
  336.         if (depth >= MAX_DEPTH - 1) {
  337.             depth--;
  338.             levels[depth].moveId++;
  339.             continue;
  340.         }
  341.        
  342.         char (&source)[PIECE_COUNT] = levels[depth].state;
  343.         char (&target)[PIECE_COUNT] = levels[depth + 1].state;
  344.         applyMove(source, target, moveId);
  345.        
  346.         depth++;
  347.         levels[depth].tested = false;
  348.         levels[depth].moveId = 0;
  349.     }
  350. }
  351.  
  352. int main() {
  353.     findLL();
  354.     return 0;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement