Code Golf: Fractran

Fractran – 1779 fractions (Edit: fixed) (I hope people are still following this thread, because this took a while!) It appears SO won’t let me post something as long as this, so I posted the Fractran source here. Input is specified as follows: First, we encode a fraction m/n = p_0^a0… p_k^ak by: Start with … 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

Convert a string into Morse code [closed]

C (131 characters) Yes, 131! main(c){for(;c=c?c:(c=toupper(getch())-32)? “•ƒŒKa`^ZRBCEIQiw#S#nx(37+$6-2&@/4)’18=,*%.:0;?5” [c-12]-34:-3;c/=2)putch(c/2?46-c%2:0);} I eeked out a few more characters by combining the logic from the while and for loops into a single for loop, and by moving the declaration of the c variable into the main definition as an input parameter. This latter technique I borrowed from strager’s answer … Read more

Code Golf: Playing Cubes

Perl 157 characters This entry was inspired by gnibbler’s Ruby entry, including the part about embedding the cube in the code. Also thanks to Kinopiko for schooling me on the 4 arg version of substr. @O=($/.$”x99)x99;map{for$n(0..$_-1,$_..-1){map{substr$O[50-2*$n+$_],$W+4/$_,6, (_,”__” ,”/__ /|”, “| | |”, “|___”https://stackoverflow.com/”)[$_]}1..4}$W+=4}@ARGV;print grep/\S/,@O Explanation: Line 1: Choose where cubes go Lines 2-5: Put cubes … Read more

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