34
\$\begingroup\$

Create a program or function to unjumble a 9x9x9 cube of digits by rotating individual 3x3 cubes within it.

This is similar to (but, I suspect, a bit harder than) my Flippin' Squares question.

Input

Input will be a 9x9x9 cube. I'm not going to be as strict over the input as I was last time, 3d arrays are allowed, arrays of strings are allowed, arrays of arrays of characters are allowed, etc.

I will be strict on what you can input into your program though - only the digits that make up the cube. No additional information like dimensions or anything else can be given as input.

Here is an example cube:

123456789   234567891   345678912
234567891   345678912   456789123
345678912   456789123   567891234
456789123   567891234   678912345
567891234   678912345   789123456
678912345   789123456   891234567
789123456   891234567   912345678
891234567   912345678   123456789
912345678   123456789   234567891

456789123   567891234   678912345
567891234   678912345   789123456
678912345   789123456   891234567
789123456   891234567   912345678
891234567   912345678   123456789
912345678   123456789   234567891
123456789   234567891   345678912
234567891   345678912   456789123
345678912   456789123   567891234

789123456   891234567   912345678
891234567   912345678   123456789
912345678   123456789   234567891
123456789   234567891   345678912
234567891   345678912   456789123
345678912   456789123   567891234
456789123   567891234   678912345
567891234   678912345   789123456
678912345   789123456   891234567

The 9x9 square in the top left is the cross-section of the bottom layer of the cube. The square in the top-middle is the next layer up, then the top-right, the middle-left and so on through to the bottom-right, which is the top layer.

This cube is the target state. As you can see the digits increase in the positive x, y and z directions, looping back to 1 when they pass 9.

Sub-cubes

You solve this puzzle by manipulating 3x3 sub-cubes within the main cube.
Each sub-cube is identified by the X Y Z position of its centre within the main cube. Because each sub-cube must be a 3x3 cube, X, Y and Z can only be between 1 and 7 (inclusive). Point 0 0 0 is at the back-bottom-left and the values increase to the right, upwards and toward the front.

Example sub-cubes from the target state (all other values set to 0 to highlight the cube in question):

1 1 1:

123000000   234000000   345000000
234000000   345000000   456000000
345000000   456000000   567000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

7 1 1:

000000789   000000891   000000912
000000891   000000912   000000123
000000912   000000123   000000234
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

1 7 1:

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

789000000   891000000   912000000
891000000   912000000   123000000
912000000   123000000   234000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

1 1 7:

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
789000000   891000000   912000000
891000000   912000000   123000000
912000000   123000000   234000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000
000000000   000000000   000000000

Manipulation

Each move you make will be a rotation of one of the sub-cubes. You will be able to rotate a cube around any one of its axes; 1, 2 or 3 quarter turns.
Rotation will be specified by first naming the axis you wish to rotate around, then the number of quarter turns you wish to rotate.

Example rotations (using unique values for each item here to make the rotations more obvious):

ABC   JKL   STU
DEF   MNO   VWX
GHI   PQR   YZ*

X1

STU   VWX   YZ*
JKL   MNO   PQR
ABC   DEF   GHI

X rotations rotate towards the back (or anti-clockwise as viewed from the left-hand side).

ABC   JKL   STU
DEF   MNO   VWX
GHI   PQR   YZ*

Y1

CFI   LOR   UX*
BEH   KNQ   TWZ
ADG   JMP   SVY

Y rotations rotate anti-clockwise as viewed from above.

ABC   JKL   STU
DEF   MNO   VWX
GHI   PQR   YZ*

Z1

SJA   TKB   ULC
VMD   WNE   XOF
YPG   ZQH   *RI

Z rotations rotate anti-clockwise as viewed from the front.

Output

Your output should be a list of moves required to take the given input back to the target state.
Each move should name the sub-cube and how it should be rotated:

111X1

Block at X=1, Y=1, Z=1, rotated 1 quarter turn around the X axis.

426Y3

Block at X=4, Y=2, Z=6, rotated 3 quarter turns around the Y axis.

789Z2

Block at X=7, Y=8, Z=9, rotated 2 quarter turn around the Z axis.

So an example output might be something like:

111X1
789Z2
426Y3

This is my preferred output - a list of newline separated moves where each line describes one move - and this is the kind of output my verification program will expect. That said, I'll be somewhat flexible on the output, so a long string of moves (for example) is okay (it'll just take me a little longer to verify).

I think this is probably enough of a challenge as it is, but let's make it just to spice it up a bit more.

Utilities

This is a program that will generate a random scrambling of the cube. It outputs the scrambled cube, the sequence of moves that generated it and those moves reversed as a possible solution.
Alternatively, it has a function that will take a scrambled function and a sequence of moves as input and then verify that it returns the target cube.

The program, along with a couple of files containing a test input solution for verification, the target cube and a cube I used to test the rotate functions, can be found at: https://github.com/gazrogers/CodegolfCubularRandomiser

import random

target = [[['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8']], [['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9']], [['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1']], [['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2']], [['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3']], [['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4']], [['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5']], [['8', '9', '1', '2', '3', '4', '5', '6', '7'], ['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6']], [['9', '1', '2', '3', '4', '5', '6', '7', '8'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9', '1'], ['3', '4', '5', '6', '7', '8', '9', '1', '2'], ['4', '5', '6', '7', '8', '9', '1', '2', '3'], ['5', '6', '7', '8', '9', '1', '2', '3', '4'], ['6', '7', '8', '9', '1', '2', '3', '4', '5'], ['7', '8', '9', '1', '2', '3', '4', '5', '6'], ['8', '9', '1', '2', '3', '4', '5', '6', '7']]]

# Because of how we read the data in,
# the first index into the 3D array is the y-index (bottom layer first, top layer last),
# then the z-index (back first, front last),
# then the x-index (left first, right last)
def getinput(filename):
    f = open(filename)
    string = f.read().split('\n\n')
    cube = '\n\n'.join(string[:-1])
    solution = string[-1]
    lists = [list(z) for y in [x.split('\n') for x in cube.split('\n\n')] for z in y]
    return [[lists[i:i + 9] for i in xrange(0, len(lists), 9)], solution]

def output(data):
    return '\n'.join([''.join(row) for layer in data for row in layer+[[]]])

def getsubcube(cube, x, y, z):
    x = clamp(x, 1, 7)
    y = clamp(y, 1, 7)
    z = clamp(z, 1, 7)
    return [[[char for char in row[x-1:x+2]] for row in layer[z-1:z+2]] for layer in cube[y-1:y+2]]

def replacesubcube(cube, subcube, x, y, z):
    newcube = cube
    for ypos in range(y-1, y+2):
        for zpos in range(z-1, z+2):
            for xpos in range(x-1, x+2):
                newcube[ypos][zpos][xpos] = subcube[ypos-y+1][zpos-z+1][xpos-x+1]
    return newcube

def clamp(n, minn, maxn):
    return max(min(n, maxn), minn)

def rotateX(cube, n):
    newcube = cube
    for _ in range(n):
        newcube = [
            [newcube[2][0], newcube[1][0], newcube[0][0]],
            [newcube[2][1], newcube[1][1], newcube[0][1]],
            [newcube[2][2], newcube[1][2], newcube[0][2]]
        ]
    return newcube

# algorithm shamelessly stolen from http://stackoverflow.com/a/496056/618347
def rotateY(cube, n):
    newcube = cube
    for _ in range(n):
        newcube = [zip(*x)[::-1] for x in newcube]
    return newcube

# Easier to define in terms of rotateX and rotateY
def rotateZ(cube, n):
    newcube = cube
    for _ in range(n):
        newcube = rotateY(rotateX(rotateY(newcube, 1), 3), 3)
    return newcube

def randomisecube(cube):
    newcube = cube
    generator = ""
    solution = ""
    funclist = [[rotateX, "X"], [rotateY, "Y"], [rotateZ, "Z"]]
    for _ in range(random.randint(10, 100)):
        x = random.randint(1, 7)
        y = random.randint(1, 7)
        z = random.randint(1, 7)
        rotateFunc = random.choice(funclist)
        numRots = random.randint(1,3)
        subcube = getsubcube(newcube, x, y, z)
        subcube = rotateFunc[0](subcube, numRots)
        newcube = replacesubcube(newcube, subcube, x, y, z)
        generator = generator + str(x) + str(y) + str(z) + rotateFunc[1] + str(numRots) + '\n'
        solution = str(x) + str(y) + str(z) + rotateFunc[1] + str(4-numRots) +'\n' + solution
    return [generator, solution, newcube]

def verifysoltuion(cube, solution):
    newcube = cube
    funclist = {"X": rotateX, "Y": rotateY, "Z": rotateZ}
    for move in solution.split('\n'):
        movelist = list(move)
        subcube = getsubcube(newcube, int(movelist[0]), int(movelist[1]), int(movelist[2]))
        subcube = funclist[movelist[3]](subcube, int(movelist[4]))
        newcube = replacesubcube(newcube, subcube, int(movelist[0]), int(movelist[1]), int(movelist[2]))
    return newcube == target

# Code to generaterandom cubes - Uncomment/Comment as necessary
# answer = randomisecube(target)
# print "Cube:"
# print output(answer[2])
# print "Generated by:"
# print answer[0]
# print "Possible solution:"
# print answer[1]

# Code to verify solution - Uncomment/Comment as necessary
[cube, solution] = getinput('testsolution.txt')
print verifysoltuion(cube, solution)

Tests

These tests were generated by the above randomiser, and so were scrambled by at most 100 moves. Your solution can be as long as it needs to be, but maybe the 100 move limit will help optimise your solution?

Please provide the solutions to these 3 test input in your answer.

Test input 1:
123456789
234567891
345678912
456789123
789891234
876912567
452123456
891234345
912345678

234567343
567878432
494519521
389891234
678212563
787823898
876934747
932145456
143256789

367178219
656269321
585491432
473539887
567643478
841298787
999145658
341256567
569367891

671784748
589855639
476966521
932129776
343778327
234145696
583991565
492312891
121498912

782395434
678214325
219761216
121656732
654283676
147858989
454789143
543678762
432565123

893989123
767391214
188896395
932595488
823214582
982397151
323654737
456562673
567456234

782193876
843218765
952931654
145859915
954823491
165758963
212517631
567651264
678912345

893214987
936727876
145326765
234941691
365634212
476732473
565696714
678789145
789123456

912345678
121478789
232345891
321834912
474787123
585498214
678919195
789123456
891234567

Test input 2:
185636789
678739121
519278992
141447183
589128734
633462125
784723456
891234567
912345678

298549121
267393832
455767823
567871244
867759645
269367236
178936767
912349478
123474589

369458912
654952363
543641434
432916575
732127236
821952912
939567891
367876789
414545691

476757893
562797914
678584189
782123678
697416367
714369554
821896365
258525219
323656712

517897654
698914565
282863873
563618741
236599478
188638387
934513698
145691383
256345123

645954345
762323456
158232167
732595252
743256541
452348691
349698262
414719873
593828934

787489436
878954347
989147298
193981213
288582371
585432832
678817943
789923554
698734145

891298567
914981178
115494319
652149626
145671787
476352173
569121634
678732645
787643556

912345678
123452789
214265178
395226739
494989628
167211984
454915125
523121236
891234567

Test input 3:
123451989
234569191
345328252
456234741
237343128
126936765
713843412
828754587
912345678

234262871
367173982
623782193
512398873
571216319
277388484
619218545
986145456
126436789

567671782
178986783
955897676
544226619
969891856
881294145
592347934
153289345
235341671

678567523
745167854
363476523
465341976
168816567
979317432
524498132
452368119
355244782

789823434
654758929
914871838
846936797
967127256
828876952
652985148
586792767
257965893

678934345
712597456
163996745
967168692
823779385
784193941
565874315
476963894
345158234

789165456
895276195
214325294
891861983
936319871
125176912
454789123
367128954
691219765

891254367
912387678
345476589
932157991
143498162
276987493
789456912
678725843
745854654

912343278
123476589
456565491
123914312
234165123
567328234
678934321
767123432
856212543
\$\endgroup\$
3
  • 15
    \$\begingroup\$ Cubular hells \$\endgroup\$
    – Luis Mendo
    Jan 25, 2017 at 22:53
  • \$\begingroup\$ Sorry if I missed it, but - what's the target final state; is it specified here? I'm assuming it's 9 squares of all 1s, then 2s, then 3s, etc, but I must've missed it. \$\endgroup\$
    – hyper-neutrino
    Sep 27, 2020 at 15:44
  • 2
    \$\begingroup\$ @HyperNeutrino "This cube is the target state. As you can see the digits increase in the positive x, y and z directions, looping back to 1 when they pass 9." underneath the first example cube \$\endgroup\$ Sep 27, 2020 at 15:51

1 Answer 1

46
+2000
\$\begingroup\$

Rust, 38250 14716 bytes

use std::{io::{stdin,Read},ops::{Add,Index,IndexMut,Sub},collections::HashSet,fmt::{self,Write}};use lazy_static::lazy_static;use regex::Regex;use tap::Tap;use hashbag::HashBag;trait Ta:l+Sized{fn V(&mut self,d(A,o,D):d){let mut ha=A;if A[o]>1{ha[o]-=1;}else{ha[o]+=1;}let Da=if o==c::X{c::Y}else{c::X};self.u(d(ha,o,D));self.u(d(A,Da,2));self.u(d(ha,o,-D));self.u(d(A,Da,2));self.u(d(A,o,-D));}fn Bb(&mut self,q:Vec<d>){for m in q{self.V(m);}}}impl<T:l>Ta for T{}trait l{fn get(&self,e:b)->B;fn K(&self,e:b)->B;fn u(&mut self,m:d);fn j(&self)->b;fn ia(&mut self,q:Vec<d>){for m in q{self.u(m);}}}impl<T:l+?Sized>l for&mut T{fn get(&self,e:b)->B{(**self).get(e)}fn K(&self,e:b)->B{(**self).K(e)}fn u(&mut self,m:d){(**self).u(m)}fn j(&self)->b{(**self).j()}}lazy_static!{static ref Ea:[[[B;9];9];9]={#[inline(never)]fn pa<F:Fn((u8,I))->T,I:Copy,T>(N:F)->impl Fn(I)->[T;9]{move|i|{[N((0,i)),N((1,i)),N((2,i)),N((3,i)),N((4,i)),N((5,i)),N((6,i)),N((7,i)),N((8,i)),]}}pa(pa(pa(|(x,(y,(z,_)))|B((x+y+z)%18))))(())};}impl l for[[[B;9];9];9]{fn get(&self,e:b)->B{self[e.0][e.1][e.2]}fn K(&self,e:b)->B{Ea[e.0][e.1][e.2]}fn u(&mut self,mut m:d){m.2=m.2.rem_euclid(4);if m.2==0{return;}let Fa=m.0-b(1,1,1);let Ua:Vec<_>=self.slice(Fa,b(3,3,3)).iter().map(|(p,v)|(p.M(m.1,m.2,3)+Fa,v)).collect();for(e,ja)in Ua{self[e.0][e.1][e.2]=ja;}}fn j(&self)->b{b(9,9,9)}}struct qa<'a,C:l>{g:&'a C,e:b,}impl<'a,C:l>Iterator for qa<'a,C>{type Item=(b,B);fn next(&mut self)->Option<Self::Item>{let j=self.g.j();if self.e.2==j.2{return None;}let Va=Some((self.e,self.g.get(self.e)));self.e.0+=1;if self.e.0>=j.0{self.e.0=0;self.e.1+=1;if self.e.1>=j.1{self.e.1=0;self.e.2+=1;}}Va}fn size_hint(&self)->(usize,Option<usize>){let j=self.g.j();let Ga=j.0*j.1*j.2-self.e.0-self.e.1*j.0-self.e.2*j.0*j.1;(Ga,Some(Ga))}}trait Wa:l+Sized{fn iter<'a>(&'a self)->qa<'a,Self>{qa{g:self,e:b(0,0,0),}}}impl<T:l>Wa for T{}struct ka<C:l>{g:C,o:c,}impl<C:l>ka<C>{fn sa(&self,e:b)->b{e.P(self.o,self.j()[self.o])}}impl<C:l>l for ka<C>{fn get(&self,e:b)->B{self.g.get(self.sa(e))}fn K(&self,e:b)->B{self.g.K(self.sa(e))}fn u(&mut self,mut m:d){m.0=self.sa(m.0);if m.1!=self.o{m.2=-m.2;}self.g.u(m);}fn j(&self)->b{self.g.j()}}trait Xa:l+Sized{fn P(self,o:c)->ka<Self>{ka{g:self,o}}}impl<T:l>Xa for T{}trait Ya:l{fn Cb(&mut self)->bool{self.iter().all(|(e,ja)|ja==self.K(e))}}impl<T:l>Ya for T{}fn main(){let mut Ha=String::new();stdin().read_to_string(&mut Ha).unwrap();let mut g=Ia(Zb(&Ha[..]).unwrap());g.ab();}struct Ia([[[B;9];9];9]);impl l for Ia{fn get(&self,e:b)->B{self.0.get(e)}fn K(&self,e:b)->B{self.0.K(e)}fn j(&self)->b{self.0.j()}fn u(&mut self,m:d){println!("{}",db(&m));self.0.u(m);}}#[derive(Clone)]struct d(b,c,i8);trait eb{fn ba(self)->Self;}impl eb for Vec<d>{fn ba(mut self)->Self{self.reverse();for m in self.iter_mut(){m.2=-m.2;}self}}fn db(d(b(x,y,z),o,D):&d)->String{format!("{}{}{}{}{}",x,y,z,match o{c::X=>"X",c::Y=>"Y",c::Z=>"Z",},D.rem_euclid(4).to_string())}fn Zb(str:&str)->Result<[[[B;9];9];9],String>{let str=str.to_lowercase();let str=str.trim();let charset=if str.contains('a'){"0a1b2c3d4e5f6g7h8i"}else if str.contains('0'){"012345678"}else{"123456789"};let mut z=0;let mut g:[[[B;9];9];9]=Default::default();lazy_static!{static ref fb:Regex=Regex::new(r"\s*\n\s*\n\s*").unwrap();}for ua in fb.split(str){let ua=ua.trim();let mut O=z;for(y,la)in ua.split("\n").enumerate(){let la=la.trim();O=z;let gb=if la.chars().nth(1)==Some(' '){""}else{""};for ra in la.split(gb).filter(|x|*x!=""){let ra=ra.trim();for(x,char)in ra.chars().filter(|x|*x!=' ').enumerate(){if x>=9||y>=9||O>=9{dbg!((x,y,O));return Err(format!("Invalid g j{:?}",(x,y,O)));}let mut ta=if let Some(index)=charset.find(char){index as u8}else{return Err(format!("Invalid character{:?}",char));};if ta%2!=((x+y+O)%2)as u8{if charset.len()==9{ta+=9}else{return Err(format!("Invalid parity at position{:?}",(x,y,O)));}};g[x][y][O].0=ta;}O+=1;}}z=O;}if<[_]>::iter(&g).flatten().flatten().collect::<HashBag<_>>()!=<[_]>::iter(&*Ea).flatten().flatten().collect(){return Err(format!("Invalid piece counts"));}Ok(g)}#[derive(Clone,Copy,PartialEq,Eq,Hash)]struct b(usize,usize,usize);impl b{fn H(mut self,h:c,k:c)->b{let f=self[h];self[h]=self[k];self[k]=f;self}fn P(mut self,o:c,E:usize)->b{self[o]=E-1-self[o];self}fn M(self,o:c,D:i8,E:usize)->b{let mut ca=self;for _ in 0..(D.rem_euclid(4)){ca=match o{c::X=>ca.H(c::Y,c::Z).P(c::Y,E),c::Y=>ca.H(c::Z,c::X).P(c::Z,E),c::Z=>ca.H(c::X,c::Y).P(c::X,E),};}ca}fn parity(self)->usize{(self.0+self.1+self.2)%2}fn va(self,min:b,E:b)->bool{true&&self.0>=min.0&&self.0<=E.0&&self.1>=min.1&&self.1<=E.1&&self.2>=min.2&&self.2<=E.2}}impl Add for b{type Output=b;fn add(self,Q:Self)->Self::Output{b(self.0+Q.0,self.1+Q.1,self.2+Q.2)}}impl Sub for b{type Output=b;fn sub(self,Q:Self)->Self::Output{b(self.0-Q.0,self.1-Q.1,self.2-Q.2)}}#[derive(Copy,Clone,PartialEq,Eq)]enum c{X=0,Y=1,Z=2,}impl Index<c>for b{type Output=usize;fn index(&self,index:c)->&Self::Output{match index{c::X=>&self.0,c::Y=>&self.1,c::Z=>&self.2,}}}impl IndexMut<c>for b{fn index_mut(&mut self,index:c)->&mut Self::Output{match index{c::X=>&mut self.0,c::Y=>&mut self.1,c::Z=>&mut self.2,}}}struct wa<C:l>{g:C,min:b,j:b,}impl<C:l>l for wa<C>{fn get(&self,e:b)->B{self.g.get(e+self.min)}fn K(&self,e:b)->B{self.g.K(e+self.min)}fn u(&mut self,mut m:d){m.0=m.0+self.min;self.g.u(m)}fn j(&self)->b{self.j}}trait hb:l+Sized{fn slice(self,min:b,j:b)->wa<Self>{wa{g:self,min,j,}}}impl<T:l>hb for T{}trait ib:l+Sized{fn ab(&mut self){let j=self.j();let mut min=b(0,0,0);let mut E=j;for&o in[c::Z,c::Y,c::X].iter(){for _ in 0..((j[o]-5+1/*round Eb*/)/2){self.jb(o,min,E);min[o]+=1;}for _ in 0..((j[o]-5)/2){self.kb(o,min,E);E[o]-=1;}}self.slice(min,b(5,5,5)).W(Ja).W(Ka).W(La).W(Ma);}}trait lb:l{fn jb(&mut self,o:c,min:b,E:b){self.slice(min,E-min).H(c::Z,o).W(xa);}fn kb(&mut self,o:c,min:b,E:b){self.slice(min,E-min).H(c::Z,o).P(c::X).P(c::Y).P(c::Z).W(xa);}}impl<T:l>ib for T{}impl<T:l>lb for T{}struct xa;impl aa for xa{fn L<C:l>(&self,g:&C,e:b)->t{let j=g.j();let b(x,y,z)=e;if z!=0{return t::da;};let index=y*j.0+x;if y+2>=j.1{if x+2>=j.0{t::G{index,w:b(j.0-4+j.1-y,j.1-1,5+x-j.0),q:vec![d(b(j.0-2,j.1-2,1),c::X,1),d(b(j.0-2,j.1-2,1),c::Y,-1),d(b(j.0+j.1-5-y,j.1-2,4+x-j.0),c::Y,1,),d(b(j.0-2,j.1-2,1),c::Y,1),d(b(j.0-2,j.1-2,1),c::X,-1),],}}else{t::G{index,w:b(x,j.1-1,5+y-j.1),q:vec![d(b(x+1,j.1-2,1),c::X,1),d(b(x+1,j.1-2,4+y-j.1),c::X,-1),d(b(x+1,j.1-2,1),c::X,-1),],}}}else{if x+2>=j.0{t::G{index,w:b(j.0-1,y,5+x-j.0),q:vec![d(b(j.0-2,y+1,1),c::Y,-1),d(b(j.0-2,y+1,4+x-j.0),c::Y,1),d(b(j.0-2,y+1,1),c::Y,1),],}}else{t::G{index,w:e+b(0,0,2),q:vec![d(e+b(1,1,1),c::Y,-1)],}}}}fn J<C:l>(&self,g:&mut C,h:b,k:b){mb(g.slice(b(0,0,1),g.j()-b(0,0,1)),h-b(0,0,1),k-b(0,0,1),);}}fn mb<C:l>(mut g:C,mut h:b,k:b){if h.parity()!=k.parity(){panic!("Cannot move between positions of different parities");}while h!=k{ya(&mut g,c::X,&mut h,k);ya(&mut g,c::Y,&mut h,k);ya(&mut g,c::Z,&mut h,k);}}fn ya<C:l>(g:C,o:c,h:&mut b,k:b){let nb=g.H(c::X,o);let mut Na=h.H(c::X,o);ob(nb,&mut Na,k.H(c::X,o));*h=Na.H(c::X,o)}fn ob<C:l>(mut g:C,h:&mut b,k:b){let ja=g.get(*h);let j=g.j();while h.0!=k.0{let mut A=b(0,0,0);let D;if h.0<k.0{A.0=h.0+1}else{A.0=h.0-1}if(h.0+1==k.0||k.0+1==h.0)&&h.0!=0&&h.0!=j.0-1{A.0=h.0}if h.1<=1{A.1=h.1+1;D=if h.0<k.0{1}else{3}}else{A.1=h.1-1;D=if h.0<k.0{3}else{1}}if h.2<=1{A.2=1}else{A.2=h.2-1}g.u(d(A,c::Z,D));*h=(*h-(A-b(1,1,1))).M(c::Z,D,3)+(A-b(1,1,1));}}struct Ma;impl aa for Ma{fn J<C:l>(&self,ma:&mut C,h:b,k:b){}fn L<C:l>(&self,ma:&C,e:b)->t{let ea=true&&e.0>=1&&e.0<=3&&e.1>=1&&e.1<=3&&e.2>=1&&e.2<=3&&e.parity()==1&&e.0!=2&&e.1!=2&&e.2!=2;if!ea{return t::na;}let fa=match e{b(1,1,1)=>return t::da,b(3,3,1)=>vec![],b(1,3,1)=>vec![d(b(2,3,2),c::Y,-1)],b(3,1,1)=>vec![d(b(3,2,2),c::X,1)],b(x,y,3)=>vec![d(b(2,2,3),c::Z,if x==1{if y==1{2}else{-1}}else{if y==1{1}else{0}},),d(b(2,3,2),c::Y,1),],_=>unreachable!(),};let mut q=fa.clone();q.extend_from_slice(&pb);q.extend(fa.ba());t::G{index:if e==b(3,2,1){1}else{0},w:b(1,1,1),q,}}fn u<C:l>(&self,g:&mut C,m:d){g.V(m);}}static pb:[d;17]=[d(b(2,3,2),c::Y,-1),d(b(3,2,2),c::X,-1),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,1),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,-1),d(b(2,2,1),c::Z,1),d(b(3,2,2),c::X,1),d(b(2,3,2),c::Y,1),d(b(3,2,2),c::X,-1),d(b(2,2,1),c::Z,1),d(b(3,2,2),c::X,1),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,1),d(b(2,3,2),c::Y,-1),d(b(3,2,2),c::X,-1),d(b(2,3,2),c::Y,1),];struct Ka;impl aa for Ka{fn J<C:l>(&self,g:&mut C,h:b,k:b){fn f(e:b)->i8{if e.0==1{if e.1==1{0}else{1}}else{if e.1==1{3}else{2}}}g.V(d(b(2,2,h.2),c::Z,f(h)-f(k)));if k.2!=h.2{g.V(d(b(2,k.1,2),c::Y,-((k.0+k.2-1)as i8)));}}fn L<C:l>(&self,ma:&C,e:b)->t{let ea=e.va(b(1,1,1),b(3,3,3))&&e.parity()==1;if!ea{return t::na;}match e{b(x,2,2)=>t::G{index:0,w:b(x,1,1),q:vec![d(b(x,2,1),c::X,1),d(b(x,1,2),c::X,-1),d(b(x,2,1),c::X,-1),d(b(x,1,2),c::X,1),],},b(2,y,2)=>t::G{index:0,w:b(1,y,1),q:vec![d(b(1,y,2),c::Y,1),d(b(2,y,1),c::Y,-1),d(b(1,y,2),c::Y,-1),d(b(2,y,1),c::Y,1),],},b(2,2,z)=>t::G{index:0,w:b(1,1,z),q:vec![d(b(2,1,z),c::Z,1),d(b(1,2,z),c::Z,-1),d(b(2,1,z),c::Z,-1),d(b(1,2,z),c::Z,1),],},_=>t::da,}}fn u<C:l>(&self,g:&mut C,m:d){g.V(m);}}struct La;impl aa for La{fn J<C:l>(&self,ma:&mut C,h:b,k:b){}fn L<C:l>(&self,ma:&C,e:b)->t{let ea=e.va(b(1,1,1),b(3,3,3))&&e.parity()==0&&e!=b(2,2,2);if!ea{return t::na;}let fa=match e{b(3,2,1)=>return t::da,b(1,2,1)=>vec![],b(x,y,3)=>vec![d(b(2,2,3),c::Z,if x==3{2}else if y==3{1}else if y==1{-1}else{0},),d(b(1,2,2),c::X,2),],b(x,y,2)=>vec![d(b(2,2,2),c::Z,if x==3{if y==3{1}else{2}}else{if y==3{0}else{-1}},),d(b(1,2,2),c::X,-1),],b(2,y,1)=>vec![d(b(2,2,2),c::X,2),d(b(2,2,3),c::Z,if y==3{-1}else{1}),d(b(1,2,2),c::X,2),],_=>unreachable!(),};let mut q=fa.clone();q.extend_from_slice(&qb);q.extend(fa.ba());t::G{index:if e==b(3,2,1){1}else{0},w:b(3,2,1),q,}}fn u<C:l>(&self,g:&mut C,m:d){g.V(m);}}static qb:[d;14]=[d(b(3,2,2),c::X,-1),d(b(2,2,1),c::Z,1),d(b(3,2,2),c::X,1),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,1),d(b(2,3,2),c::Y,-1),d(b(3,2,2),c::X,2),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,1),d(b(2,2,1),c::Z,-1),d(b(3,2,2),c::X,-1),d(b(2,2,1),c::Z,1),d(b(3,2,2),c::X,1),d(b(2,3,2),c::Y,1),];lazy_static!{static ref za:Vec<d>=vec![d(b(1,2,2),c::Z,1),d(b(2,3,2),c::Z,-1),d(b(1,2,2),c::Z,-1),d(b(2,3,2),c::Z,1),];static ref Oa:Vec<d>=za.clone().ba();}struct Ja;impl aa for Ja{fn J<C:l>(&self,g:&mut C,h:b,k:b){let A=b(2,2,2);if h.parity()!=k.parity(){panic!("Cannot move between positions of different parities");}if h==k{return;}if h.parity()==1{fn Aa(e:b)->bool{((e.0==2)as u8+(e.1==2)as u8+(e.2==2)as u8)==2}if Aa(h){if Aa(k){fn Pa(e:b)->(c,i8){let o=if e.0!=2{c::X}else if e.1!=2{c::Y}else{c::Z};(o,if e[o]==3{1}else{-1})}let(Qa,rb)=Pa(h);let(Ba,sb)=Pa(k);if Qa==Ba{g.u(d(A,if Ba==c::X{c::Y}else{c::X},2,))}else{let(tb,ub)=match(Qa,Ba){(c::X,c::Y)=>(c::Z,1),(c::Y,c::X)=>(c::Z,-1),(c::Z,c::X)=>(c::Y,1),(c::X,c::Z)=>(c::Y,-1),(c::Y,c::Z)=>(c::X,1),(c::Z,c::Y)=>(c::X,-1),_=>unreachable!(),};g.u(d(A,tb,rb*sb*ub));}}else{self.J(g,h,b(2,2,1));g.ia(Oa.clone());self.J(g,b(1,3,1),k);}}else{if Aa(k){self.J(g,h,b(1,3,1));g.ia(za.clone());self.J(g,b(2,2,1),k);}else{fn f(e:b)->i8{if e.0==1{if e.1==1{0}else{1}}else{if e.1==1{3}else{2}}}g.u(d(A,c::Z,f(h)-f(k)));if k.2!=h.2{g.u(d(A,c::Y,-((k.0+k.2-1)as i8)));}}}}else{if h==A{g.ia(Oa.clone());self.J(g,b(1,3,2),k);}else if k==A{self.J(g,h,b(1,3,2));g.ia(za.clone());}else if h.2!=k.2{let mut h=h;while h.2!=k.2{let o=if h.0==2{c::Y}else{c::X};let ga=if o==c::Y{-1}else{1};g.u(d(A,o,ga));h=h.M(o,ga,5);}self.J(g,h,k);}else{fn f(e:b)->i8{if e.0<=2&&e.1==1{3}else if e.0==3&&e.1<=2{2}else if e.0>=2&&e.1==3{1}else{0}}g.u(d(A,c::Z,f(h)-f(k)));}}}fn L<C:l>(&self,g:&C,e:b)->t{match e{b(2,2,2)=>t::G{index:100,w:b(2,2,2),q:vec![],},p if vb(p)=>t::da,_=>match e{p if p.0>=3||p.1>=3=>self.L(g,e.M(c::Z,1,5)).tap_mut(|x|x.M(c::Z,-1,5)),p if p.2>=1=>{let(o,ga)=if p.1==0{(c::X,1)}else{(c::Y,-1)};self.L(g,e.M(o,ga,5)).tap_mut(|x|x.M(o,-ga,5))}p if p.1>p.0=>self.L(g,e.H(c::X,c::Y)).tap_mut(|x|x.H(c::X,c::Y)),b(0,0,0)=>t::G{index:0,w:b(2,2,2),q:vec![d(b(1,1,1),c::Z,2),d(b(1,1,1),c::X,1),],},b(1,0,0)=>t::G{index:1,w:b(2,3,2),q:vec![d(b(1,1,1),c::Z,1),d(b(1,1,1),c::Y,-1),d(b(2,2,2),c::Z,2),d(b(1,1,1),c::Y,1),d(b(1,1,1),c::Z,-1),],},b(2,0,0)=>t::G{index:2,w:b(2,2,2),q:vec![d(b(2,3,2),c::Z,-1),d(b(1,1,1),c::Z,1),d(b(1,1,1),c::Y,-1),d(b(2,3,2),c::Z,1),d(b(1,1,1),c::Y,1),d(b(1,1,1),c::Z,-1),],},b(1,1,0)=>t::G{index:3,w:b(3,3,2),q:vec![d(b(1,1,1),c::Z,1),d(b(1,1,1),c::Y,2),d(b(2,2,2),c::Z,2),d(b(1,1,1),c::Y,2),d(b(1,1,1),c::Z,-1),],},b(2,1,0)=>t::G{index:4,w:b(1,2,2),q:vec![d(b(2,3,2),c::Z,-1),d(b(1,1,1),c::Z,-1),d(b(1,1,1),c::X,2),d(b(2,3,2),c::Z,1),d(b(1,1,1),c::X,2),d(b(1,1,1),c::Z,1),],},b(2,2,0)=>t::G{index:5,w:b(2,3,3),q:vec![d(b(1,1,1),c::Z,1),d(b(1,1,1),c::Y,2),d(b(3,3,2),c::X,1),d(b(1,1,1),c::Y,2),d(b(1,1,1),c::Z,-1),d(b(3,3,2),c::X,-1),],},_=>unreachable!(),},}}}fn vb(p:b)->bool{p.va(b(1,1,1),b(3,3,3))}trait aa{fn L<C:l>(&self,g:&C,e:b)->t;fn J<C:l>(&self,g:&mut C,h:b,k:b);fn u<C:l>(&self,g:&mut C,m:d){g.u(m);}fn wb<C:l>(&self,g:&mut C){let mut oa=<HashSet<b>>::new();let mut Ra:Vec<_>=g.iter().filter_map(|(e,_)|match self.L(g,e){t::G{index,w,q,}=>Some((e,index,w,q)),_=>None,}).collect();Ra.sort_by_key(|x|x.1);for(e,_,w,q)in Ra{oa.insert(e);let Sa=g.K(e);if g.get(e)==Sa{continue;}let h=g.iter().find(|&(p,v)|{v==Sa&&!matches!(self.L(g,p),t::na)&&!oa.contains(&p)}).unwrap().0;if let t::G{q:xb,w:yb,..}=self.L(g,h){for m in xb.ba(){self.u(g,m);}self.J(g,yb,w);}else{self.J(g,h,w);}for m in q{self.u(g,m);}}}}trait zb:l+Sized{fn W<S:aa>(&mut self,bb:S)->&mut Self{bb.wb(self);self}}impl<T:l>zb for T{}enum t{G{index:usize,w:b,q:Vec<d>,},da,na,}impl t{fn H(&mut self,h:c,k:c){match self{Self::G{w,q,..}=>{*w=w.H(h,k);for m in q.iter_mut(){m.2=-m.2;m.0=m.0.H(h,k);if m.1==h{m.1=k;}else if m.1==k{m.1=h;}else{m.1;}}}_=>{}}}fn M(&mut self,o:c,D:i8,E:usize){let D=D.rem_euclid(4);if D==0{return;}match self{Self::G{w,q,..}=>{*w=w.M(o,D,E);for m in q.iter_mut(){m.0=m.0.M(o,D,E);if match(o,D){(c::X,1)|(c::Y,3)=>m.1==c::Z,(c::Y,1)|(c::Z,3)=>m.1==c::X,(c::Z,1)|(c::X,3)=>m.1==c::Y,(_,_)=>m.1!=o,}{m.2=-m.2;}if D!=2{m.1=match(o,m.1){(c::Y,c::Z)|(c::Z,c::Y)=>c::X,(c::Z,c::X)|(c::X,c::Z)=>c::Y,(c::X,c::Y)|(c::Y,c::X)=>c::Z,_=>o,};}}}_=>{}}}}struct Ca<C:l>{g:C,h:c,k:c,}impl<C:l>l for Ca<C>{fn get(&self,e:b)->B{self.g.get(e.H(self.h,self.k))}fn K(&self,e:b)->B{self.g.K(e.H(self.h,self.k))}fn u(&mut self,mut m:d){if self.h!=self.k{m.0=m.0.H(self.h,self.k);m.2=-m.2;if m.1==self.h{m.1=self.k;}else if m.1==self.k{m.1=self.h;}}self.g.u(m)}fn j(&self)->b{self.g.j().H(self.h,self.k)}}trait Ab:l+Sized{fn H(self,h:c,k:c)->Ca<Self>{Ca{g:self,h,k,}}}impl<T:l>Ab for T{}#[derive(PartialEq,Eq,Copy,Clone,Hash,Default)]struct B(u8);

The solution is hosted on GitHub. Running cargo run in the root directory will take the cube from stdin and output a list of moves that solves the inputted cube. Input can be in any of the forms from the question and this answer. The output will use a modified coordinate system by default, but can be changed to use the question's coordinate system by changing line 27.

If you'd like to play with the cube and see how it's solved, I've created an interactive visualization.

If you'd like to read about how it's solved, I've written a long detailed explanation with interactive illustrations of the process.

Explanation

Coordinate System

This explanation will use a slightly different coordinate system than the challenge.

X is left-to-right, Y is top-to-bottom, and Z is front-to-back.

Here are 3 different visualizations of the axes:

   z
  / E F
 /  G H
* - - x
| A B
| C D
y

A B  E F
C D  G H
0 x  z ·
y ·  · ·
Positions as 'xyz':
000 100   001 101
010 110   011 111

Rotations are clockwise looking down the axis (right-hand rule).

Here is a visualization of the 3 unit rotations:

cube:       X1(cube):
0 x  z ·    z ·  · ·
y ·  · ·    0 x  y ·

cube:       Y1(cube):
0 x  z ·    x ·  0 z
y ·  · ·    · ·  y ·

cube:       Z1(cube):
0 x  z ·    y 0  · z
y ·  · ·    · x  · ·

0-indexing

The original challenge 1-indexes the pieces, which complicates the modulo arithmetic. This explanation will use 0-indexing, where the pieces are numbered 0-8.

1-indexing
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
...

0-indexing
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
...

Parity

Pieces are locked in to a checkerboard pattern; pieces from odd places can only go to odd places, and likewise for even.

This means that there are actually 18 kinds of pieces.

0 in an even position -->  0 1 2 3 4 5 6 7 8
                           1 2 3 4 5 6 7 8 0 <-- 0 in an odd position
                           ...

This brings us to...

Alternate Notation

Because there are 18 kinds of pieces, it's helpful to have 18 distinct symbols. From now on, this explanation will use an adapted, alpha-numeric notation, where all even-parity pieces are numbers, and all odd-parity pieces are letters.

0-indexed
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 8 0 1
3 4 5 6 7 8 0 1 2
...

Alpha-numeric
0 a 1 b 2 c 3 d 4
a 1 b 2 c 3 d 4 e
1 b 2 c 3 d 4 e 5
b 2 c 3 d 4 e 5 f
...

Links

Throughout this explanation, links to cube states will be used to illustrate the process. Here's a scrambled cube, and a solved cube.

Some links will illustrate a set of moves. First, it will show the goal state, followed by the start state of the cube. Then, for each move, it will first highlight the positions that will be affected, and then show the result of the move. Here's an example of a small algorithm.

Additionally, some links with moves will emphasize certain pieces. The bordered pieces are the focus, and the highlighted pieces are pieces that will/must remain unchanged. Here's an example of the previous algorithm with highlights. Sometimes, only a subset of the unchanged pieces will be highlighted, to make it easier to track the rotations.

Approach to Solving

To solve the cube, we iterate through the positions in some order and solve each of them without unsolving the previous positions.

Positions can be in two states: solved & unsolved. When solving a position, pieces in solved positions may be moved but must be restored to ensure the solved positions are ultimately unchanged.

A position is solved by the following process:

  1. Locate a piece of the correct value in any unsolved position.
  2. Move the piece to the correct position without ultimately changing any solved position.

#1 is fairly self-explanatory, so the rest of this explanation focuses on #2.

At various times throughout solving the cube, we'll denote an unsolved section of the cube as the "pool". We'll then divide #2 into 3 parts:

  1. Move the piece into the pool.
  2. Move the piece around in the pool.
  3. Move the piece out of the pool into the correct position.

To accomplish this, we'll need to define two helpers:

  1. We'll figure out how to move a piece from any position in the pool to any other position of the same parity in the pool.
  2. Then, for each unsolved position outside of the pool, we'll choose a convenient "source" position in the pool and figure out a list of moves that will move a piece from the source position to the position we're solving.

Using these two helpers, we can refine the above process:

  1. Move the piece into the pool.
    • Execute the reverse of the moves supplied by helper #2.
  2. Move the piece around in the pool.
    • Use helper #1.
  3. Move the piece out of the pool into the correct position.
    • Execute the moves supplied by helper #2.

We will now formalize and define this logic.

Overview

At a high level, the solving method is broken into three stages.

  1. Solving the outer shell (everything but the center 5×5×5)
  2. Solving the inner shell (the center 5×5×5 excluding the center 3×3×3)
  3. Solving the center 3×3×3

Each stage consists of a few steps.

Step Structure

In each step, the puzzle positions are paritioned into 3 classifications:

  1. 'active': unsolved positions that will be solved by the step
  2. 'done': previously solved positions
  3. 'pool': unsolved positions

A step is executed by iterating through the 'active' positions and solving each position by executing the following prodecure:

  1. Locate a piece of the correct value in any unsolved position.
  2. Move the piece into the pool.
  3. Move the piece within the pool to a 'source' position in the pool.
  4. Move the piece from the 'source' position to the target position.

All of the above steps must be performed without ultimately changing any piece in a previously solved 'active' position or any piece in a 'done' position.

Step Definition

Thus, a step is defined by:

  1. A partition of the puzzle pieces
  2. A way to move a piece from any 'pool' position to any other 'pool' position of the same parity (without ultimately changing any piece in an 'active' or 'done' position)
  3. For each 'active' position:
    • An index indicating when to solve this position
    • A convenient source 'pool' position
    • A list of moves that will move a piece from the specified 'pool' position to this position (without ultimately changing any piece in an 'active' position with a lower index or any piece in a 'done' position)

Stage 1: Solving the outer shell

In this stage, we'll solve everything but the center 5×5×5.

We'll accomplish this with 12 steps, each step solving a face of the unsolved portion of the 9×9×9. We could go in any order; the implementation follows this ordering (as it looked nice):

  1. The Z=0 plane (after)
  2. The Z=1 plane (after)
  3. The Z=8 plane (after)
  4. The Z=7 plane (after)
  5. The Y=0 plane (after)
  6. The Y=1 plane (after)
  7. The Y=8 plane (after)
  8. ...
  9. ...
  10. ...
  11. ...
  12. The X=7 plane (after)

Stage 1, Step 1: Solving the Z=0 face

All positions with Z=0 are 'active', and all other positions are 'pool'.

The most interesting part of this step involves the 'active' positions. The 'active' positions fall into 3 categories based on their location:

A A A A A A A A B B
A A A A A A A A B B
A A A A A A A A B B
A A A A A A A A B B
A A A A A A A A B B
A A A A A A A A B B
A A A A A A A A B B
B B B B B B B B C C
B B B B B B B B C C

We'll solve each 'active' position from left-to-right, then top-to-bottom.

The examples that follow focus solely on the moves after the piece is moved into the 'source' position.

A positions are the easiest to solve, as they only require one rotation; here's an example for one A position.

B positions are a little more complicated. Because they're near the edge, we can't use the same technique as in A. This means that we'll have to move some of the pieces we've previously solved, but we also need to make sure to restore them to the correct position afterwards. Here's an example of how this is accomplished for one B position.

If you've played with puzzle cubes before, the technique used in that algorithm may be familiar. Essentially, we're rotating the target position to a more convenient place, moving the piece in, and then undoing the rotation of the target position.

Last and certainly not least, we have C positions, which are surrounded on all sides. Solving them is similar to B positions, but a little more complicated. Here's an example for one C position.

Moving pieces within the pool is rather simple, as there's a lot of room, and the pool is a rectangular prism; here is an example for one case. A full explanation of the algorithm is omitted, as it is not very novel.

Stage 1, Steps 2-12

These steps follow the same pattern as Step 1, just on different faces within the cube.

Stage 2: Solving the inner shell

At this point, everything is solved except for the inner 5×5×5. After this stage, everything except the inner 3×3×3 will be solved.

This stage is not part of Stage 1 because some of the algorithms used in Stage 1 require more space than is available in the 5×5×5.

This stage consists of a single step.

All positions outside of in center 5×5×5 are 'done' and will not be changed (even temporarily). Positions in the center 3×3×3 are 'pool', and the remainder are 'active'.

Stage 2, Moving between 'pool' positions

Moving pieces between 'pool' positions is more complex in this step, because of the limited space. To describe how pieces are moved, we'll first divide the pool into 4 sections, pictured below.

P T P  T Q T  P T P
T Q T  Q U Q  T Q T
P T P  T Q T  P T P

P positions are the corners, Q positions are the face centers, T positions are the edges, and the singular U position is the cube center.

P and Q positions all have odd parity, and T and U positions all have even. Thus, pieces can be moved freely between P and Q positions, and T and U positions, but not between P and T positions, P and U positions, etc.

Thus, there are 8 possible cases:

  • P: From a P position to another P position
  • Q: From a Q position to another Q position
  • T: From a T position to another T position
  • U: From the U position to itself
  • PQ: From a P position to a Q position
  • QP: From a Q position to a P position
  • TU: From a T position to the U position
  • UT: From the U position to a T position

Case U is very trivial, since it is accomplished by doing nothing. Cases P, Q, T are fairly trivial, and can be accomplished simply by rotating the center 3×3×3.

Cases PQ, QP, TU, UT are where things get interesting — remember that these must be accomplished without ultimately changing any piece outside of the pool, the center 3×3×3.

Cases QP & UT are the reverse of cases PQ & TU, respectively, so we will focus on the later two cases.

Cases PQ & TU are both based off of the following algorithm that we'll call INTO_CENTER, which moves a piece from position X into position U whilst preserving everything outside of the center 3×3×3.

· · ·  · · ·  · · ·
· · ·  · U ·  · · ·
· · ·  X · ·  · · ·

Here is INTO_CENTER in action.

First, Case TU. We can accomplish it with the follwing procedure:

  1. Move the piece from the original T position to position V using Case T.
  2. Use INTO_CENTER to move the piece from position V to position U.
· · ·  · · ·  · T ·
· · ·  · U ·  · · ·
· · ·  V · ·  · · ·

Onto Case PQ. If you look at the moves in INTO_CENTER (linked above), you might notice two things:

  1. All of the moves rotate around the Z axis
  2. All of the moves are at Z=4

These two properties mean that INTO_CENTER has the same effect on the Z=3 and Z=5 layers as it does on the Z=4 layer we focused on.

This means that we can accomplish Case PQ with the following procedure:

  1. Move the piece from the original P position to position R, using Case P.
  2. Use INTO_CENTER to move the piece from position R to position S.
  3. Move the piece from position S to the destination Q position, using Case Q.
· · ·  · · ·  · · P
· S ·  · · ·  · · ·
R · ·  · Q ·  · · ·

Stage 2, Moving pieces to 'active' positions

Now that we have a method to move pieces between 'pool' positions, we can move on to describing the 'active' positions.

First, we'll break up the 'active' positions into a few categories, and then we'll describe each category.

We'll label the categories A-F; the partition is pictured below.

A B C B A  B D E D B  C E F E C  B D E D B  A B C B A
B D E D B  D · · · D  E · · · E  D · · · D  B D E D B
C E F E C  E · · · E  F · · · F  E · · · E  C E F E C
B D E D B  D · · · D  E · · · E  D · · · D  B D E D B
A B C B A  B D E D B  C E F E C  B D E D B  A B C B A

The categories will be solved in alphabetical order; all A positions are solved, then all B positions, etc.

For each category, we'll focus on the behavior of only one position; all of the others follow by symmetry. Specifically, we'll focus on the following positions:

A B C · ·  · · · · ·  · · · · ·  · · · · ·  · · · · ·
· D E · ·  · · · · ·  · · · · ·  · · · · ·  · · · · ·
· · F · ·  · · · · ·  · · · · ·  · · · · ·  · · · · ·
· · · · ·  · · · · ·  · · · · ·  · · · · ·  · · · · ·
· · · · ·  · · · · ·  · · · · ·  · · · · ·  · · · · ·

When solving a position, care must be taken to not ultimately change any other position that is outside of the 5×5×5 or in an earlier or current category.

The below diagrams are maps of the 5×5×5. # represents a position that must be ultimately unchanged, + represents a 'pool' position, and X is the 'pool' position that we'll later establish as our source 'pool' position.

First up are A positions.

A · · · #  · · · · ·  · · · · ·  · · · · ·  # · · · #
· · · · ·  · + + + ·  · + + + ·  · + + + ·  · · · · ·
· · · · ·  · + + + ·  · + X + ·  · + + + ·  · · · · ·
· · · · ·  · + + + ·  · + + + ·  · + + + ·  · · · · ·
# · · · #  · · · · ·  · · · · ·  · · · · ·  # · · · #

Solving this position is rather trivial. We'll set our source 'pool' position to be in the center, and to solve the A position we'll simply rotate the piece in.

Let's move on to B positions.

# B · # #  # · · · #  · · · · ·  # · · · #  # # · # #
# · · · #  · + + + ·  · + + + ·  · + + + ·  # · · · #
· · · · ·  · + + + ·  · + + + ·  · + + + ·  · · · · ·
# · · · #  · + + + ·  · + X + ·  · + + + ·  # · · · #
# # · # #  # · · · #  · · · · ·  # · · · #  # # · # #

Here, there are more locked-in pieces, meaning that we can't just simply rotate the piece in. Thus, we'll have to do a little bit of maneuvering.

It's getting a bit tighter, but C positions are rather similar to B positions.

# # C # #  # · · · #  # · · · #  # · · · #  # # # # #
# · · · #  · + + + ·  · + + + ·  · + + + ·  # · · · #
# · · · #  · + + + ·  · + X + ·  · + + + ·  # · · · #
# · · · #  · + + + ·  · + + + ·  · + + + ·  # · · · #
# # # # #  # · · · #  # · · · #  # · · · #  # # # # #

Onto D positions.

# # # # #  # # · # #  # · · · #  # # · # #  # # # # #
# D · # #  # + + + #  · + + + ·  # + + + #  # # · # #
# · · · #  · + + + ·  · + + + ·  · + + + ·  # · · · #
# # · # #  # + + + #  · + + + ·  # + + X #  # # · # #
# # # # #  # # · # #  # · · · #  # # · # #  # # # # #

It's getting cramped! Time for E positions.

# # # # #  # # # # #  # # · # #  # # # # #  # # # # #
# # E # #  # + + + #  # + + + #  # + + + #  # # # # #
# # · # #  # + + + #  · X + + ·  # + + + #  # # · # #
# # # # #  # + + + #  # + + + #  # + + + #  # # # # #
# # # # #  # # # # #  # # · # #  # # # # #  # # # # #

Finally, F positions. There is almost no room to work with here, but we can squeeze the piece in by rotating the corner cube and then one of the edge cubes.

# # # # #  # # # # #  # # # # #  # # # # #  # # # # #
# # # # #  # + + + #  # + + + #  # + + + #  # # # # #
# # F # #  # + + + #  # + + + #  # + + + #  # # # # #
# # # # #  # + + + #  # + + + #  # + X + #  # # # # #
# # # # #  # # # # #  # # # # #  # # # # #  # # # # #

Stage 3: Solving the center 3×3×3

At this point, everything but the center 3×3×3 is solved. At the end of this stage, it will be entirely solved.

Stage 3 will be broken into 3 steps:

  1. Solving the center cross
  2. Solving the edges
  3. Solving the corners

Thin Moves

Before moving on to Step 1, we're going to introduce a very useful set of algorithms: thin moves. These allow us to rotate "flat" 1×3×3 sections of the cube, without affecting other pieces.

In subsequent examples, thin moves are shown as lines labeled with a t — this is a shorthand for the sequence of regular moves that comprise the thin move. Here's an example of the shorthand for a quarter-turn rotation on Z.

Here are the moves that comprise the previous thin move.

Stage 3, Step 1: Solving the center cross

Again, the center cross is this set of pieces.

As a precursor, we're going to solve the center-most piece first, because we can simply use the 'pool'-to-'pool' algorithm from Stage 2, Step 1. Afterwards, everything but the outer shell of the 3×3×3 will be solved and we can proceed to the novel parts of this step.

The partition for this step is as follows:

  • 'active': all pieces in the center cross other than the center piece that we just solved.
  • 'done': all pieces outside of the center 3×3×3, along with the center piece.
  • 'pool': all of the corners and edges.

Here's a diagram of the positions like in the previous stage.

X · +   · # ·   + · +
· A ·   # # #   · # ·
+ · +   · # ·   + · +

Moving pieces between 'pool' positions is rather simple. We can use thin moves to rotate the corners and edges of a face of the 3×3×3 without affecting the center cross. Here's an example of moving one of the corners.

To move pieces from a 'pool' position to an 'active' position, we use a planar variation of INTO_CENTER that uses thin moves (THINTO_CENTER).

Similarity to a Rubik's Cube

At this point, all positions except the corners and edges of the 3×3×3 are solved.

The center 3×3×3 is now rather similar to a Rubik's Cube:

  • The center cross is solved, and the unsolved pieces are either edges or corners; in a Rubik's Cube, the center cross is fixed and is therefore always solved.
  • Because corners and edges are of different parities, corners cannot swap with edges. Likewise, in a Rubik's Cube, corners cannot swap with edges.
  • We can either use thin moves to rotate 1×3×3 slices, or regular moves to rotate the whole 3×3×3. These are the same moves you can make in a Rubik's Cube.

In fact, the rest of the puzzle is simpler than a Rubik's Cube:

  • In a Rubik's Cube, every piece in unique, but in this, some of the pieces are interchangable.
  • In a Rubik's Cube, corners can be in one of three orientations, and edges can be in one of two; in this, pieces don't have orientations.

To solve the rest of the cube, we'll use thin moves to solve it like a Rubik's Cube, using the Old Pochmann (1, 2) method.

Stage 3, Step 2

In this step, the remainder of the cube will be divided as follows, where A and B represent 'active' positions, and C represents the singular 'pool' position.

· A ·   A · A   · A ·
B · C   · · ·   A · A
· A ·   A · A   · A ·

First, we'll solve all A positions. Then, we'll solve the B position. Nothing needs to be done to solve the C position, as all other pieces of the same parity will have been solved.

We'll focus on this A position; other A positions are solved similarly:

· # ·   # # #   · # ·
# # +   # # #   # # #
· A ·   # # #   · # ·

Since the C position is the only 'pool' position, our source position will always be C.

To solve this A position, we'll execute the following procedure:

  1. Use thin moves to rotate the piece in this A position to the B position.
  2. Swap the pieces in the B and C positions.
  3. Reverse the moves in #1.

To accomplish #2, we'll use an algortihm known as the T Permutation, which swaps the pieces in the B and C positions, and swaps the pieces in the P and Q positions:

· · P   · · ·   · · ·
B · C   · · ·   · · ·
· · Q   · · ·   · · ·

Thus, the above procedure will swap the pieces in the B and C positions and swap the pieces in the P and Q positions. Note that we must be careful not to disturb the pieces in the P and Q positions in #1, as otherwise #2 will have undesirable effects on the rest of the cube.

As we solve all of the B positions, the pieces in the P and Q positions keep swapping back and forth, but we don't care, as they are both unsolved.

Finally, to solve the B and C positions, if they're not already solved, we'll apply the T Permutation to swap them.

Note: usually, Old Pochmann will involve two additional algorithms for this step, the Ja and Jb permutations. These are used to simplify some of the swaps, but are not strictly necessary.

Stage 3, Step 3

This step is rather similar to Step 2.

In this step, the remainder of the cube will be divided as follows, where A and B represent 'active' positions, and C represents the singular 'pool' position.

C · A   · · ·   A · A
· · ·   · · ·   · · ·
A · B   · · ·   A · A

Like the previous step, we'll first solve all A positions, and then solve the B position. As before, nothing needs to be done to solve the C position, as all other pieces of the same parity will have been solved.

We'll focus on this A position; other A positions are solved similarly:

+ # #   # # #   # # #
# # #   # # #   # # #
A # #   # # #   # # #

To solve this A position, we'll execute the following procedure:

  1. Use thin moves to rotate the piece in this A position to the B position.
  2. Swap the pieces in the B and C positions.
  3. Reverse the moves in #1.

To accomplish #2, we'll use an algorithm known as the Y Permutation, which swaps the pieces in theB and C positions, and swaps the pieces in the P and Q positions:

C P ·   · · ·   · · ·
Q · ·   · · ·   · · ·
· · B   · · ·   · · ·

Thus, the above procedure will swap the pieces in the B and C positions and swap the pieces in the P and Q positions. The P and Q positions were previously solved and have the same solved value, so swapping them does nothing.

Finally, to solve the B and C positions, if they're not already solved, we'll apply the Y Permutation to swap them.

The puzzle is now entirely solved.

Note: in the Old Pochmann method, there is usually a step in between solving the edges and corners: possibly using the Ra Permutation to resolve parity. However, because the edges that the Y Permutation swaps are of equal value, this is unnecessary in our case.

Conclusion

We did it!

Thanks for reading the explanation; I hope you enjoyed it! If you haven't already, I highly suggest checking out the interactive visualization. If you have any questions, comments, or feedback, please leave a comment or ping me in chat!

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Wow. After so long, I seriously doubted anyone would ever answer this. I need to read the answer properly, and check the test cases, but it looks like the big green tick (and the extra 1000 bounty I promised) are yours. \$\endgroup\$
    – Gareth
    Sep 14, 2021 at 9:01
  • \$\begingroup\$ I'm having a hard time verifying this. It looks (based on what I'm seeing in your excellent visualization) like your 'X' instruction behaves the same as mine, but the 'Y' and 'Z' are swapped and (I think) your 'Y' works clockwise instead of anti-clockwise. I ran my first test case through your tool at the command line (cargo run as given above) and sent the output to a file. I prepended the test case to the file and then input it into my verification program (as given in the question) and it outputs 'false'. I think it's either the axis swap thing, or the 0-8/1-9 difference causing the fail. \$\endgroup\$
    – Gareth
    Sep 14, 2021 at 14:47
  • 1
    \$\begingroup\$ Excellent, thanks. The bounty will have to be awarded in installments, I'm afraid - the limit is 500 per bounty. And I can't award the bounty for 24 hours after setting it... \$\endgroup\$
    – Gareth
    Sep 14, 2021 at 17:33
  • 1
    \$\begingroup\$ is it possible for you to post the ungolfed version? i dont understand anything :P \$\endgroup\$
    – DialFrost
    Feb 4, 2022 at 7:57
  • 1
    \$\begingroup\$ @DialFrost The ungolfed version is hosted on GitHub \$\endgroup\$
    – tjjfvi
    Feb 4, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.