Search code examples
algorithmcube

The Magic Cube Encryption


I am trying to solve a very nice problem and I found a solution but this is more of like static solution.

The problem.

Cube has 8 corners, each containing one character. For example, the string “ABCDEFGH” would be represented on the cube like in the image below:

enter image description here

Cube can be rotated to the left, right, up and down.

This rightwards rotation converted the initial string “ABCDEFGH” into “BFGCAEHD”.

I want to know is there any algorithm or formula are there to find out changes in the corner? I did solve it by taking the notes of all changes of corners. For example, if A is in position 1 then if you change to right it will always replace it's position with E which is in position 5. So this is a very static solution. This solutions works because cube rotation is also static and never change position. However, want to know if this can be solved with a specific algorithm. Thank you :)


Solution

  • Not sure if I understand correctly, but I think you just need to write down the permutations for each case? Like, if you consider the three possible axes of rotation, and clockwise and counter-clockwise rotation directions, you have the six possible ways in which you can rotate the cube. For example, in Python you could do it like this:

    def permute(s, perm):
        return ''.join(s[i] for i in perm)
    
    def rotate_frontback_cw(cube):
        return permute(cube, [1, 5, 6, 2, 0, 4, 7, 3])
    
    def rotate_frontback_ccw(cube):
        return permute(cube, [4, 0, 3, 7, 5, 1, 2, 6])
    
    def rotate_leftright_cw(cube):
        return permute(cube, [4, 5, 1, 0, 7, 6, 2, 3])
    
    def rotate_leftright_ccw(cube):
        return permute(cube, [3, 2, 6, 7, 0, 1, 5, 4])
    
    def rotate_updown_cw(cube):
        return permute(cube, [1, 2, 3, 0, 5, 6, 7, 4])
    
    def rotate_updown_ccw(cube):
        return permute(cube, [3, 0, 1, 2, 7, 4, 5, 6])
    
    def cube2str(cube):
        a, b, c, d, e, f, g, h = cube
        return (f'   {h}--------{g}\n'
                '  /|       /|\n'
                ' / |      / |\n'
                f'{e}--------{f}  |\n'
                f'|  {d}-----|--{c}\n'
                f'| /      | /\n'
                f'|/       |/\n'
                f'{a}--------{b}')
    
    cube = 'ABCDEFGH'
    print('cube')
    print(cube2str(cube))
    print('rotate_frontback_cw')
    print(cube2str(rotate_frontback_cw(cube)))
    print('rotate_frontback_ccw')
    print(cube2str(rotate_frontback_ccw(cube)))
    print('rotate_leftright_cw')
    print(cube2str(rotate_leftright_cw(cube)))
    print('rotate_leftright_ccw')
    print(cube2str(rotate_leftright_ccw(cube)))
    print('rotate_updown_cw')
    print(cube2str(rotate_updown_cw(cube)))
    print('rotate_updown_ccw')
    print(cube2str(rotate_updown_ccw(cube)))
    

    This would print:

    cube
       H--------G
      /|       /|
     / |      / |
    E--------F  |
    |  D-----|--C
    | /      | /
    |/       |/
    A--------B
    rotate_frontback_cw
       D--------H
      /|       /|
     / |      / |
    A--------E  |
    |  C-----|--G
    | /      | /
    |/       |/
    B--------F
    rotate_frontback_ccw
       G--------C
      /|       /|
     / |      / |
    F--------B  |
    |  H-----|--D
    | /      | /
    |/       |/
    E--------A
    rotate_leftright_cw
       D--------C
      /|       /|
     / |      / |
    H--------G  |
    |  A-----|--B
    | /      | /
    |/       |/
    E--------F
    rotate_leftright_ccw
       E--------F
      /|       /|
     / |      / |
    A--------B  |
    |  H-----|--G
    | /      | /
    |/       |/
    D--------C
    rotate_updown_cw
       E--------H
      /|       /|
     / |      / |
    F--------G  |
    |  A-----|--D
    | /      | /
    |/       |/
    B--------C
    rotate_updown_ccw
       G--------F
      /|       /|
     / |      / |
    H--------E  |
    |  C-----|--B
    | /      | /
    |/       |/
    D--------A