Chess960 position lookup

Python, 179 chars

i=input()
a=i%4*2+1
b=i/4%4*2
c=i/16%6
d=i/96*5
s='NNRKRNRNKRNRKNRNRKRNRNNKRRNKNRRNKRNRKNNRRKNRNRKRNN'[d:d+5]
s=s[:c]+'Q'+s[c:]
if a>b:a,b=b,a
print s[:a]+'B'+s[a:b-1]+'B'+s[b-1:]

Builds it backwards from the description order so we don't have to count the spaces that are already taken.


GolfScript (100 97 95 91 90 84 80 79 chars)

~:?4%2*)?4/4%2*].8,^?16/6%?96/4,{5,>({}+/}%2/=+{1$=.@^}/]'BBQNNRKR'+8/zip${1>}%

This approach follows the instructions forwards rather than backwards. Basically it takes an array of indices and repeatedly removes the selected offsets, leaving them further down the stack; at the end, it gathers them together with the letters and uses them to sort.


C, 221 chars

Explanations:

  1. The result is accumulated in r.
  2. g calculates division and remainder into n and x.
  3. p and P write a character in the n'th available slot.

Interesting points:

  1. x~-x is 2*x+1.
  2. In !s*!*m, one * serves as &&, the other is dereference.
  3. P uses s+0, so with an empty parameter you get +0.

The knights calculation is the hardest, and accounts for the longest line. Can probably be improved.

char*m,r[9],c;
n,x;
g(r){x=n%r;n/=r;}
p(s){!s*!*m?*m=c:p(s-!*m++);}
#define P(s,t);m=r;c=*#t;p(s+0);
main(){
    scanf("%d",&n);
    g(4)P(x-~x,B)
    g(4);r[x*2]=c;
    g(6)P(x,Q)
    P(n>3?(n-=3)>3?(n-=2)>3?n=3:2:1:0,N)
    P(n,N)
    P(,R)P(,K)P(,R)
    puts(r);
}