I don't spoiler my program.
def nextperm(permo):
n = len(permo)
y = n - 1
while permo[y - 1] > permo[y]:
y -= 1
yy = n - 1
while permo[y - 1] > permo[yy]:
yy -= 1
permn = permo[0:y - 1] + [permo[yy]] + sorted(permo[y - 1:yy] + permo[yy + 1:n])
return permn
def perminv(perm):
num_sqsize = len(perm)
perm_inv = [0 for _ in range(num_sqsize)]
for y in range(num_sqsize):
x = perm[y]
perm_inv[x] = y
return perm_inv
ch = ['K','Q','R','R','B','B','N','N']
#01234567 = kqrrbbnn
perm = [_ for _ in range(8)]
ct = 0
ca = [0]*7
for i in range(40320):
ki = perm[0]
qu = perm[1]
lr = perm[2]
rr = perm[3]
lb = perm[4]
rb = perm[5]
ln = perm[6]
rn = perm[7]
if lr < ki < rr and ln < rn and lb < rb and (lb+rb)%2 == 1:
ct += 1
out = ''
permi = perminv(perm)
for j in range(8):
out += ch[permi[j]]
if ln < rn < lb < rb:
cas = 1
can = not (ki < lb)
elif ln < lb < rn < rb:
cas = 2
can = rb < ki
elif ln < lb < rb < rn:
cas = 3
can = lb < ki < rb and not (lr < lb < rb < rr)
elif lb < ln < rn < rb:
cas = 4
can = not (lb < ki < rb)
elif lb < ln < rb < rn:
cas = 5
can = ki < lb
elif lb < rb < ln < rn:
cas = 6
can = not (rb < ki)
else:
cas = 0
can = False
if can:
ca[cas] += 1
if cas == 1 and can and not lb < qu < rb:
print(ct,out,cas,can)
perm = nextperm(perm)
print(ca,sum(ca))
Call Benjamin's bishop components L,M,R and the placement of the NN LL...RR. The six cases amount to the following number of positions with castling rights. If you split further into the Q position, you can do it in your head...but need an aspirine afterwards.
60, 16, 24, 44, 16, 60