Code Golf: Morris Sequence

GolfScript – 41 (extra credit: 40) 1{.p`n+0:c:P;{:|P=c{c`P|:P!}if):c;}%~1}do {~.p`n+0:c:P;{:|P=c{c`P|:P!}if):c;}%1}do What? The procedure for getting the next number in the sequence: Convert the current number to a string, append a newline and loop over the characters. For each digit, if the previous digit P is the same, increment the counter c. Otherwise, add c and P to … Read more

Code Golf: Hourglass

C/C++, a dismal 945 characters… Takes input as parameters: a.out 5 52% #include<stdio.h> #include<memory.h> #include<stdlib.h> #define p printf int h,c,*l,i,w,j,*q,k;const char* z;int main(int argc,char**argv) {h=atoi(argv[1]);c=(h*h*atoi( argv[2])+99)/100;l=new int[ h*3];for(q=l,i=0,w=1;i<h; i++,c=(c-w)&~((c-w)>>31 ),w+=2)if(c>=w){*q++= 0;*q++ =0;* q++=w;} else {*q++=(c+1)/ 2;*q++=w-c;*q++ =c/2;}p(“_”); for(i=0;i<h ;i ++)p ( “__”);p (“\n” );q = l+h *3-1; for (i= –h;i>=0; i–){p(“%*” “s\\”,h-i,””) ; z= “x\0 … Read more

Code Golf: Running Water

C – 741 621 600 characters (but handles the new cases properly) $ gcc water.c && ./a.out < test6.txt __ ___ / \xxxxx/ / _______ ________ / \ / _____/ \xxx/__ \xxxx\_ ____ / \xxxx/__/xxxxx/ \xxxxxxxx/ \xxxxxxxxx/ \xxxxxx\ /xxxx/ #include<stdio.h> char d[99][99],*p,*e,*z,*s=d,c,S=’ ‘,D=’-‘,O=’.’,U=’_’;n,w,x,N=99,i; g(y){for(i=0;!i;p+=N,e+=N){i=*p==D;for(z=p;z!=e;z+=y){if(*z!=O&&*z!= D)break;*z=*z==O?S:U;}}}f(char*n,int r){if(*n==O||*n==D){*n=r>0?’x’: S;int k;for(k=0;k<9;k++)f(n+k/3*N-N+k%3-1,r+k/3-1);}}main(){for(p=s; gets(p);p+=N,n++){x=strlen(p)-1;w=x>w?x:w;}for(p=s,e=d[N];p<s+N;p++) {for(i=1,z=p;z<e;z+=N)c=*z,c==0?*z=c=S:0,i?c==S?*z=O:c==U?*z=D:0:0,( c==”https://stackoverflow.com/”&&z[1]!=U)||(c==’\\’&&z[-1]!=D)||c==U?i=1-i:0;}p=s;e=s+w;g(1); p=s+w;e=s;g(-1);for(p=s;p<s+w;p++){for(z=p;*z==S;z+=N);f(z,1);}for(i =0;i<n;i++)printf(“%.*s\n”,w+1,d[i]);}

Code Golf: Regex parser

C (interpreting), 630 622 617 615 582 576 573 572 558 554 544 538 529 504 502 500 499 chars This understands parentheses, and works on the regexp live (i.e. not parsed first) #define C char #define M m(s,o m(C*s,C*o,C*S,C*p,C*P,C T){C*n=P-1,*q=s,h=*P==41,c=1;for(;h*c;c-=*n–==40)c+=*n==41; c=*P-42;c=p-P?c-82?T&&c&~1&&c-21?h?2:*S==*P&s<S?M,S-1,p,n,2)||(T&4&&M,S-1,p,P,T|1)): 4:M,T?S:o,p,P-1,T?c&~1?3:7-c:0):T&&s==S||M,o,p,P-1,2):T&&s==S;if(c==2)for(c=4;q<=S;q ++)c|=m(q,S,S,n+h,P-h,2)?M,q,p,n,2)||q<S&T/4&&M,q,p,P,T&6):0;return c&4?c&1?:T&1&&M,S,p,n,2)||M,o,p,n,0):c;}main(C*w,C**v){C*u;for(w=*++v;*++v;)puts(m(*v -1,u,u=index(*v,0)-1,w-1,index(w,0)-1,2)?”true”:”false”);} compiling with -Wall complains about argc being char. Will … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)