C,273-20 = 253字节
#include<stdio.h>
#include<string.h>
int main(int c,char**v){char*p=v[1],*q=v[2],*s=" ABCDE",r[]={0,0,0};if(strspn(p,s+1)!=30||p[30]||strspn(q,s)!=30||q[30])puts("Invalid test");else{for(;*p;++q)++r[(*p++!=*q)+(*q==' ')];printf("%d (%dR %dB %dW)",4**r-r[1],*r,r[2],r[1]);}}
尽管已花了23个字节来打印,但我还是获得了奖金。:-(
说明
#include <stdio.h>
#include <string.h>
int main(int c,char**v)
{
char *p=v[1], *q=v[2], /* arguments */
*s=" ABCDE", /* valid chars */
r[]={0,0,0}; /* results - right, wrong, blank */
if (strspn(p,s+1) != 30 /* validity check - answer key begins with [A-E]{30} */
|| p[30] /* and ends there */
|| strspn(q,s) != 30 /* same for answers, but allow space, too */
|| q[30])
{
puts("Invalid test");
} else {
for ( ; *p; ++q) /* for each answer */
++r[(*p++!=*q)+(*q==' ')]; /* increment the appropriate counter */
printf("%d (%dR %dB %dW)",4**r-r[1],*r,r[2],r[1]); /* print result */
}
}
检查无效输入的代码是计算答案的代码的两倍-真正的挑战在于for
循环即将结束。实际上,这是一个假定输入始终有效的版本,以163-20 = 143字节表示:
#include<stdio.h>
int main(int c,char**v){char*p=v[1],*q=v[2],r[]={0,0,0};for(;*p;++q)++r[(*p++!=*q)+(*q==' ')];printf("%d (%dR %dB %dW)",4**r-r[1],*r,r[2],r[1]);}
做出相同假设并仅打印分数的字节(133字节):
#include<stdio.h>
int main(int c,char**v){char*p=v[1],*q=v[2],r[]={4,-1,0};for(c=0;*p;++q)c+=r[(*p++!=*q)+(*q==' ')];printf("%d",c);}