c99 -- 252 characters
#include <stdio.h>
#define G for(i=0;i<6;i++)
char*n="Andy\0Barb\0Carl\0Didi\0Earl\0Fran",*p[7];int i,j;int main()
{FILE*r=fopen("/dev/random","r");G p[i]=n+5*i;G{j=fgetc(r)%6;p[7]=p[j]
;p[j]=p[i];p[i]=p[7];}G printf("%s buys for %s\n",p[i],p[(i+1)%6]);}
Slight improvement by taking advantage of the circular nature of the permutation. This version always builds a loop-like buying strategy, so it is less random than the previous (271 character) version, but I believe it still meets the spec.
Requires a platform that has a working /dev/random
. I ought to be able to knock about 8 off by omitting the \0
s in the big string, but my libc doesn't seem to be dealing with %4s
print specifiers the way the man page says.
The shuffle is bad, but doing it that way prevents me from having to check on "Foo buys for Foo" conditions.
Readable:
#include <stdio.h>
char *n="Andy\0Barb\0Carl\0Didi\0Earl\0Fran",
*p[7]; /* 7th cell for temp */
int i,j;
int main(){
FILE*r=fopen("/dev/random","r");
for(i=0;i<6;i++)
p[i]=n+5*i; /* Initialize the pointers */
for(i=0;i<6;i++){
j=fgetc(r)%6; /* Poor numeric properties. Cest le Code Golf */
p[7]=p[j];
p[j]=p[i];
p[i]=p[7];
}
for(i=0;i<6;i++)
printf("%s buys for %s\n",p[i],p[(i+1)%6]);
}