# Program to convert a scramble to kociemba order string



## abunickabhi (May 17, 2021)

Kociemba string is just a long string that describes a NxN cube state.
The names of the facelet positions of the cube (letters stand for Up, Left, Front, Right, Back, and Down) are the string elements.

For a solved 3x3 cube the kociemba order is, UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB

For example, a 5x5 scramble B2 R2 B2 R2 U B2 D' R2 U F R2 U' B' R' U B D' U2 R F2 U' Lw2 Fw2 D B2 Fw2 Rw2 B2 L2 D Bw2 D' Lw2 B2 D Lw2 U2 R2 U' B' F2 Uw2 R2 Uw2 Fw2 Lw2 L2 U2 F D2 Lw2 L2 B R2 B U R' Bw2 L' Uw2 B R' B2 Rw2 F Rw' Bw2 Lw' B F Lw U' Rw' D' Lw U' Dw' Fw' Rw2 Dw2 Uw' Lw2 U2 Fw D' L

will have a kociemba string like, 

```
LBFFRLRFBRLRUBFULRLULRRBF
ULUUFBBULDFBRDLDBUDFBUDFD
UDUURLUUDLBLFRRRRBULLRULU
FBBDRDFFRFLLDLRRUFFLLBBRB
BUURFFFDDBBBLRRFFFBDBFDFD
DUDDUBUDRLFLBULDDDLRRBDUD
```

For a 5x5 scramble, it takes about a few minutes to describe this string.
Currently for a 17x17 scramble, I want to describe a kociemba string, and if I do it manually, it might take me the whole day, and I will be error prone.
I need the string as I want to solve the 17x17 scramble I have on my computer, using dwalton's NxN solver program.



So, I need a program, that will take scramble in WCA notation as input, and give me the N^3 long string, describing the cube state.


----------



## Alexander (May 21, 2021)

```
# -*- coding: utf-8 -*-
"""
Created on Thu May 21 2021
@author: Alexander & abhij
"""

import re
string = "LBFFRLRFBRLRUBFULRLULRRBFULUUFBBULDFBRDLDBUDFBUDFDUDUURLUUDLBLFRRRRBULLRULUFBBDRDFFRFLLDLRRUFFLLBBRBBUURFFFDDBBBLRRFFFBDBFDFDDUDDUBUDRLFLBULDDDLRRBDUD"
string = string.replace("UUU", "U'")
string = string.replace("DDD", "D'")
string = string.replace("LLL", "L'")
string = string.replace("RRR", "R'")
string = string.replace("BBB", "B'")
string = string.replace("FFF", "F'")
string = string.replace("UU", "U2")
string = string.replace("DD", "D2")
string = string.replace("LL", "L2")
string = string.replace("RR", "R2")
string = string.replace("BB", "B2")
string = string.replace("FF", "F2")

print(string)
```

Something like this??

The weird part is the RRRR in the string??? is that an error?


```
# -*- coding: utf-8 -*-
"""
Created on Thu May 21 2021
@author: Alexander & abhij
"""
import re
string = "LBFFRLRFBRLRUBFULRLULRRBFULUUFBBULDFBRDLDBUDFBUDFDUDUURLUUDLBLFRRRRBULLRULUFBBDRDFFRFLLDLRRUFFLLBBRBBUURFFFDDBBBLRRFFFBDBFDFDDUDDUBUDRLFLBULDDDLRRBDUD"
string = string.replace("UUUU", "")
string = string.replace("DDDD", "")
string = string.replace("LLLL", "")
string = string.replace("RRRR", "")
string = string.replace("FFFF", "")
string = string.replace("BBBB", "")
string = string.replace("U", "U ")
string = string.replace("D", "D ")
string = string.replace("L", "L ")
string = string.replace("R", "R ")
string = string.replace("B", "B ")
string = string.replace("F", "F ")
string = string.replace("U U U ", "U' ")
string = string.replace("D D D ", "D' ")
string = string.replace("L L L ", "L' ")
string = string.replace("R R R ", "R' ")
string = string.replace("B B B ", "B' ")
string = string.replace("F F F ", "F' ")
string = string.replace("U U ", "U2 ")
string = string.replace("D D ", "D2 ")
string = string.replace("L L ", "L2 ")
string = string.replace("R R ", "R2 ")
string = string.replace("B B ", "B2 ")
string = string.replace("F F ", "F2 ")

print(string)
```
Cleanup with space and RRRR etc. filter out. More weird moves in scamble like D2 U D2 U or D U D string can be made shorter to filter that out


----------



## abunickabhi (May 21, 2021)

Alexander said:


> The weird part is the RRRR in the string??? is that an error?


There can be 4 Rs in the string, it just means there is a red faced color bar on the cube. Since its 5x5 cube, the centers can be anything, and any sequence is possible.



Alexander said:


> ```
> # -*- coding: utf-8 -*-
> """
> Created on Thu May 21 2021
> ...


Hello Alexander,

I am more interested in the inverse problem.

Input scramble sequence: eg Fw' F Lw B2 R Fw2 F2 Uw2 Rw2 B Lw R2 B' D2 F2 Lw2 Dw' Fw2 L' Lw' Bw Lw Bw U2 L2 Rw Dw Lw2 Rw2 R Bw L D2 Lw' F' R' Fw Rw' R Bw2 Fw Uw2 L2 Rw' Bw D' Uw' B2 R2 Uw Fw Uw F' D' Rw2 Fw2 R2 D' Bw2 L

Output: kociemba string (One line of URFDLB having 150 characters describing the cube state).

The inverse problem is more difficult I think, hence I made this post for ideas.


----------



## Alexander (May 21, 2021)

Guess i missunderstood the string i was thinking it where turns then RRRR is just no turn on the R side


----------



## abunickabhi (May 21, 2021)

Alexander said:


> Guess i missunderstood the string i was thinking it where turns then RRRR is just no turn on the R side


The string literal R just means a R faced sticker. It does not mean the R face turn. 
It can be a bit confusing if you see it first without context.
So, RRRR in WG orientation will be four red stickers in the cube state on the face.


----------



## SpeedCMOS (May 22, 2021)

I have a general NxNxN Cube representation script with a drawing function that internally is able to convert a scramble into the sticker positions. It is quite easy to format the output to match the kociemba order.
Take this file and with nnn_representation included, run the following code:

```
//Change the scramble and cube size here
var alg = "B2 R2 B2 R2 U B2 D' R2 U F R2 U' B' R' U B D' U2 R F2 U' Lw2 Fw2 D B2 Fw2 Rw2 B2 L2 D Bw2 D' Lw2 B2 D Lw2 U2 R2 U' B' F2 Uw2 R2 Uw2 Fw2 Lw2 L2 U2 F D2 Lw2 L2 B R2 B U R' Bw2 L' Uw2 B R' B2 Rw2 F Rw' Bw2 Lw' B F Lw U' Rw' D' Lw U' Dw' Fw' Rw2 Dw2 Uw' Lw2 U2 Fw D' L"; //Put your scramble here
var cube_size = 5; //Change that for other cube sizes. Value of 17 means a 17x17x17 cube.

//Don't change any of the below unless you know what you're doing
alg=alg.split(" ");
var rep = nnn_representation;
var stickers = rep.moves(rep.init_cube(cube_size), rep.apply_alg(alg), cube_size);

for (var i = 0; i < 6; ++i){
            for (var j = 0; j < cube_size; ++j) {
                for (var k = 0; k < cube_size; ++k)
                    stickers[i][j][k] = ["R","U","F","D","B","L"][Math.floor(stickers[i][j][k] / cube_size / cube_size)]
            }
}

stickers = stickers.map(face => {
  return face.map(row => row.join("")).join("");
});

stickers = stickers[1]+stickers[0]+stickers[2]+stickers[3]+stickers[5]+stickers[4];
console.log(stickers);
```

In the first two lines you can change the cube size and the scramble. I've set it to your example 5x5 scramble and it outputs what you gave in your initial post.


----------



## abunickabhi (May 23, 2021)

SpeedCMOS said:


> I have a general NxNxN Cube representation script with a drawing function that internally is able to convert a scramble into the sticker positions. It is quite easy to format the output to match the kociemba order.
> Take this file and with nnn_representation included, run the following code:
> 
> ```
> ...


Thanks a lot for your js file and js project.

I was able to get the string that I wanted, and feed it to a NxN solver. Thanks for the code!


----------

