Shop OBEX P1 Docs P2 Docs Learn Events
C Golf challenge? — Parallax Forums

C Golf challenge?

RossHRossH Posts: 5,519
edited 2010-02-17 10:24 in Propeller 1
@All,

I have been reading the SPIN Golf Challenge thread with interest (and some people have the gall to say that C is unreadable!)

This got me thinking - perhaps someone (someone cleverer than me) should come up with a C Golf Challenge? After all, C beats SPIN hollow in its potential for both brevity and obfuscation. Below is an example (not mine - it won the C obfuscation awards in 1990).

And (before you ask!) - Yes it runs unmodified under Catalina (of course!).

Here is the command I used to compile it:
catalina -lcx -x5 -M256k -D DRACBLADE dds.c

Before you either scroll down this post - or open the attachment - see if you can figure it out what it does:
#define O(b,f,u,s,c,a)b(){int o=f();switch(*p++){X u:_ o s b();X c:_ o a b();default[img]http://forums.parallax.com/images/smilies/tongue.gif[/img]--;_ o;}}
#define t(e,d,_,C)X e:f=fopen(B+d,_);C;fclose(f)
#define U(y,z)while(p=Q(s,y))*p++=z,*p=' '
#define N for(i=0;i<11*R;i++)m[noparse][[/noparse] i ]&&
#define I "%d %s\n",i,m[noparse][[/noparse] i ]
#define X ;break;case
#define _ return
#define R 999
typedef char*A;int*C,E[noparse][[/noparse] R ],L[noparse][[/noparse] R ],M[noparse][[/noparse] R ],P[noparse][[/noparse] R ],l,i,j;char B[noparse][[/noparse] R ],F[noparse][[/noparse] 2 ];A m[noparse][[/noparse] 12*R ],malloc
(),p,q,x,y,z,s,d,f,fopen();A Q(s,o)A s,o;{for(x=s;*x;x++){for(y=x,z=o;*z&&*y==
*z;y++)z++;if(z>o&&!*z)_ x;}_    0;}main(){m[noparse][[/noparse] 11*R ]="E";while(puts("Ok"),gets(B)
)switch(*B){X'R':C=E;l=1;for(i=0;i<R;P[noparse][[/noparse] i++ ]=0);while(l){while(!(s=m[noparse][[/noparse] l ]))l++;if
(!Q(s,"\"")){U("<>",'#');U("<=",'$');U(">=",'!');}d=B;while(*F=*s){*s=='"'&&j
++;if(j&1||!Q(" \t",F))*d++=*s;s++;}*d--=j=0;if(B[noparse][[/noparse] 1 ]!='=')switch(*B){X'E':l=-1
X'R':B[noparse][[/noparse] 2 ]!='M'&&(l=*--C)X'I':B[noparse][[/noparse] 1 ]=='N'?gets(p=B),P[noparse][[/noparse] *d ]=S():(*(q=Q(B,"TH"))=0,p
=B+2,S()&&(p=q+4,l=S()-1))X'P':B[noparse][[/noparse] 5 ]=='"'?*d=0,puts(B+6):(p=B+5,printf("%d\n",S
()))X'G'[img]http://forums.parallax.com/images/smilies/tongue.gif[/img]=B+4,B[noparse][[/noparse] 2 ]=='S'&&(*C++=l,p++),l=S()-1 X'F':*(q=Q(B,"TO"))=0;p=B+5;P[noparse][[/noparse] i
=B[noparse][[/noparse] 3 ] ]=S();p=q+2;M[noparse][[/noparse] i ]=S();L[noparse][[/noparse] i ]=l X'N':++P[noparse][[/noparse] *d ]<=M[noparse][[/noparse] *d ]&&(l=L[noparse][[/noparse] *d ]);}else p=B+2,P[noparse][[/noparse] 
*B ]=S();l++;}X'L':N printf(I)X'N':N free(m[noparse][[/noparse] i ]),m[noparse][[/noparse] i ]=0    X'B':_ 0 t('S',5,"w",N
fprintf(f,I))t('O',4,"r",while(fgets(B,R,f))(*Q(B,"\n")=0,G()))X 0:default:G()
;}_ 0;}G(){l=atoi(B);m[noparse][[/noparse] l ]&&free(m[noparse][[/noparse] l ]);(p=Q(B," "))?strcpy(m[noparse][[/noparse] l ]=malloc(strlen(p
)),p+1):(m[noparse][[/noparse] l ]=0,0);}O(S,J,'=',==,'#',!=)O(J,K,'<',<,'>',>)O(K,V,'$',<=,'!',>=)
O(V,W,'+',+,'-',-)O(W,Y,'*',*,'/',/)Y(){int o;_*p=='-'?p++,-Y():*p>='0'&&*p<=
'9'?strtol(p,&p,0):*p=='('?p++,o=S(),p++,o:P[noparse][[/noparse] *p++ ];}










What? Can't figure it out? Here's a BIG HINT - the above program will successfully execute the following (hello to all those PropBasic fans out there!):


10 REM Lunar Lander
20 REM By Diomidis Spinellis
30 PRINT "You aboard the Lunar Lander about to leave the spacecraft."
60 GOSUB 4000
70 GOSUB 1000
80 GOSUB 2000
90 GOSUB 3000
100 H = H - V
110 V = ((V + G) * 10 - U * 2) / 10
120 F = F - U
130 IF H > 0 THEN 80
135 H = 0
140 GOSUB 2000
150 IF V > 5 THEN 200
160 PRINT "Congratulations!  This was a very good landing."
170 GOSUB 5000
180 GOTO 10
200 PRINT "You have crashed."
210 GOTO 170
1000 REM Initialise
1010 V = 70
1020 F = 500
1030 H = 1000
1040 G = 2
1050 RETURN
2000 REM Print values
2010 PRINT "        Meter readings"
2015 PRINT "        --------------"
2020 PRINT "Fuel (gal):"
2030 PRINT F
2040 GOSUB 2100 + 100 * (H <> 0)
2050 PRINT V
2060 PRINT "Height (m):"
2070 PRINT H
2080 RETURN
2100 PRINT "Landing velocity (m/sec):"
2110 RETURN
2200 PRINT "Velocity (m/sec):"
2210 RETURN
3000 REM User input
3005 IF F = 0 THEN 3070
3010 PRINT "How much fuel will you use?"
3020 INPUT U
3025 IF U < 0 THEN 3090
3030 IF U <= F THEN 3060
3040 PRINT "Sorry, you have not got that much fuel!"
3050 GOTO 3010
3060 RETURN
3070 U = 0
3080 RETURN
3090 PRINT "No cheating please!  Fuel must be >= 0."
3100 GOTO 3010
4000 REM Detachment
4005 PRINT "Ready for detachment"
4007 PRINT "-- COUNTDOWN --"
4010 FOR I = 1 TO 11
4020   PRINT 11 - I
4025   GOSUB 4500
4030 NEXT I
4035 PRINT "You have left the spacecraft."
4037 PRINT "Try to land with velocity less than 5 m/sec."
4040 RETURN
4500 REM Delay
4510 FOR J = 1 TO 500
4520 NEXT J
4530 RETURN
5000 PRINT "Do you want to play again? (0 = no, 1 = yes)"
5010 INPUT Y
5020 IF Y = 0 THEN 5040
5030 RETURN
5040 PRINT "Have a nice day."



Don't believe me? Try it! You will need a Prop with 256Kb of XMM RAM and an SD card (such as the DracBlade). Use the command above to compile it - instructions on how to run it are in the attached file.

Ross.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Catalina - a FREE C compiler for the Propeller - see Catalina

Comments

  • heaterheater Posts: 3,370
    edited 2010-02-17 09:47
    What a nightmare !

    Strangely enough I cannot compile that with GCC v 4.3.2 under Debian. Or at least I have not found the right options to do so. Tried - ansi, -std=c89 etc with no luck. Some give more errors than others.
    $ gcc dds.c
    dds.c:9: warning: conflicting types for built-in function &#8216;malloc&#8217;
    dds.c: In function &#8216;main&#8217;:
    dds.c:16: warning: incompatible implicit declaration of built-in function &#8216;printf&#8217;
    dds.c:19: warning: incompatible implicit declaration of built-in function &#8216;printf&#8217;
    dds.c:19: warning: incompatible implicit declaration of built-in function &#8216;free&#8217;
    dds.c:19: error: void value not ignored as it ought to be
    dds.c:19: warning: incompatible implicit declaration of built-in function &#8216;fprintf&#8217;
    dds.c: In function &#8216;G&#8217;:
    dds.c:21: warning: incompatible implicit declaration of built-in function &#8216;free&#8217;
    dds.c:21: error: void value not ignored as it ought to be
    dds.c:21: warning: incompatible implicit declaration of built-in function &#8216;strcpy&#8217;
    dds.c:21: warning: incompatible implicit declaration of built-in function &#8216;strlen&#8217;
    dds.c:22: warning: pointer/integer type mismatch in conditional expression
    $
    
    



    However GCC for the ZPU is version 3.4.2 and compiles it just fine.
    $ install/bin/zpu-elf-gcc  -phi -Os  dds.c -o dds
    dds.c:10: warning: conflicting types for built-in function 'malloc'
    dds.c: In function `G':
    dds.c:22: warning: pointer/integer type mismatch in conditional expression
    $
    
    



    Sadly Zog is not quite up to running any C on the prop just yet.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • RossHRossH Posts: 5,519
    edited 2010-02-17 10:09
    heater,

    The "obfuscated C" pages do say that some compilers will be unable compile some of the competition entries - especially the older ones. I guess gcc 4.x.y is "stricter" than both Catalina, or gcc 3.x.y (it also compiles using 3.4.5 and 3.4.6).

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • heaterheater Posts: 3,370
    edited 2010-02-17 10:24
    Oh yeah. The good old days of C when you could throw any old "line noise" at it and it would compile to something.
    I have a hard time nowadays getting my C++ and Qt projects to crash [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
Sign In or Register to comment.