# -*- Mode: Python -*-

# trying to solve corners

solved = (0,1,2,3,4,5,6,7)

# view from top
# 0 1 \
# 2 3 / top face
# view from bottom
# 4 5 \
# 6 7 / bottom face

ops = [
    ([5,7,2,3,4,0,6,1], "u4+*u4-"),
    ([1,3,0,2,4,5,6,7], "u3-"),
    ([2,0,3,1,4,5,6,7], "u3+"),
    ([0,5,2,7,4,1,6,3], "u1+*u1-"),
    ([0,1,2,3,5,7,4,6], "b3-"),
    ([0,1,2,3,6,4,7,5], "b3+"),
    ]

# is that enough?  can those ops cover the whole space?

# how many possible perms?
# 8 * 7 * 6 * 5 * 4 * 3 * 2
# 40320

def permute (p, op):
    return tuple([p[i] for i in op])

class GotIt (Exception):
    pass

def _search (p, seen, d=0):
    moves = []
    for op, name in ops:
        p2 = permute (p, op)
        if p2 == solved:
            print p2, name
            raise GotIt
        elif not seen.has_key (p2):
            moves.append ((p2, name))
    # sort moves
    moves.sort()
    #print '%s%r [%d]' % (' ' * d, p, len(moves))
    if d < 15:
        for p2, name in moves:
            seen[p2] = None
            try:
                _search (p2, seen, d+1)
            except GotIt:
                print p2, name
                raise GotIt

def search (p):
    seen = {p:None}
    try:
        _search (p, seen)
    except GotIt, why:
        print 'len(seen)==%d' % len(seen)
        return why
    print 'len(seen)==%d' % len(seen)

# assign colors to solution
# white on top?
# 2 = wyr
# 3 = woy
# 0 = wrb
# 1 = wbo
# 4 = gry
# 5 = gyo
# 6 = gbr
# 7 = gob

# place 'square1' logo upright on the left.

# view from top
# 0 1 \
# 2 3 / top face
# view from bottom
# 4 5 \
# 6 7 / bottom face

t0 = (1,0,2,5,4,7,6,3)
t0 = (2,6,0,3,1,4,5,7)

if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        t0 = tuple (map (int, sys.argv[1]))
    print search (t0)
