Programmatic Masochism

Status
Not open for further replies.

-Triple-

Ars Tribunus Angusticlavius
8,554
See if you can figure out what this code does before running it (shouldn't be too hard)...

I was messing around today with some code and I wrote a horrendously disgusting function that does an elementary task in the nastiest way imaginable. See the ugly code for yourself (written in VC++ 6.0):

code:<HR>
Code:
#include <iostream>
#include <map>
#include <math.h><P>using namespace std;<P>map<long,long> m;<P>long func(long a, long b);<P>int main()
{
    for(int x = 0;x <= 9;x++)
        m[x] = 9 - x;<P>    cout << func(0,1);<P>    return 0;
}<P>long func(long a, long b)
{
    long d   = (long)floor(log10((double)b)) + 1,
         val = 1 + a,
         t   = 1;
    for(long x = 0;x < d;x++,t*=10)
        val += (m[(b % (t * 10)) / t] * t);
    return val > t / 2 ? ((t ^ 0xffffffff) + 1) + val : val;
}

Ok, so I cheated with the two's complement there at the end, but for some reason there's something rather satisfying about such a gut-wrenching algorithm that does a zillion times work work than is necessary. I just wanted to share the agony :p

Anyone else have any submissions? Nauseating algorithms to execute simple tasks that you'd like others to try and figure out on their own? I'd like to see the revolting, miasmatic algorithms that we at Ars can conceive :cool:
 

Smeghead

Ars Praefectus
4,727
Subscriptor
If you want impenetrable code, then the IOCCC is your best bet. Some of that stuff is mind-boggling. :eek:

The source for the winners is available for download. It's truly horrifying what you can do with various programming languages when you put your mind to it. :D

This year's contest is open until December 1st, but I'm afraid the above wouldn't even get a look in. :D

www.ioccc.org
 

-Triple-

Ars Tribunus Angusticlavius
8,554
This year's contest is open until December 1st, but I'm afraid the above wouldn't even get a look in.

I think there's a way I can combine several of those functions of the same type and some bitwise ops to make it a lot sicker, but I'd need to read the rules... thanks for the link, this stuff makes me happy to be a programmer.
 

Smeghead

Ars Praefectus
4,727
Subscriptor
A few combinations of horrible functions and some bit twiddling probably won't cut it. The entries to that contest are several orders of magnitude sicker than anything I could dream up. For example:

anderson.c (see anderson.hint for info)
code:<HR>
Code:
#include <stdio.h><P>char
*T="IeJKLMaYQCE]jbZRskc[SldU^V\\X\\|/_<[<:90!\"$434-./2>]s",
K[3][1000],*F,x,A,*M[2],*J,r[4],*g,N,Y,*Q,W,*k,q,D;X(){r  [r
[r[3]=M[1-(x&1)][*r=W,1],2]=*Q+2,1]=x+1+Y,*g++=((((x&     7)
-1)>>1)-1)?*r:r[x>>3],(++x<*r)&&X();}E(){A||X(x=0,g       =J
),x=7&(*T>>A*3),J[(x[F]-W-x)^A*7]=Q[x&3]^A*(*M)[2         +(
x&1)],g=J+((x[k]-W)^A*7)-A,g[1]=(*M)[*g=M[T+=A            ,1
][x&1],x&1],(A^=1)&&(E(),J+=W);}l(){E(--q&&l              ()
);}B(){*J&&B((D=*J,Q[2]<D&&D<k[1]&&(*g++=1                ),
!(D-W&&D-9&&D-10&&D-13)&&(!*r&&(*g++=0)                   ,*
r=1)||64<D&&D<91&&(*r=0,*g++=D-63)||D                     >=
97&&D<123&&(*r=0,*g++=D-95)||!(D-k[                       3]
)&&(*r=0,*g++=12)||D>k[3]&&D<=k[                          1]
-1&&(*r=0,*g++=D-47),J++));}j(                            ){
putchar(A);}b(){(j(A=(*K)[D*                              W+
r[2]*Y+x]),++x<Y)&&b();}t                                 ()
{(j((b(D=q[g],x=0),A=W)                                   ),
++q<(*(r+1)<Y?*(r+1):                                     Y)
)&&t();}R(){(A=(t(                                        q=
0),'\n'),j(),++r                                          [2
]<N)&&R();}O()                                            {(
j((r[2]=0,R(                                              ))
),r[1]-=q)                                                &&
O(g-=-q)                                                  ;}
C(){(                                                     J=
gets                                                      (K
[1]))&&C((B(g=K[2]),*r=!(!*r&&(*g++=0)),(*r)[r]=g-K[2],g=K[2
],r[
1]&&
O())
);;}
main
(){C
((l(
(J=(
A=0)
[K],
A[M]
=(F=
(k=(
M[!A
]=(Q
=T+(
q=(Y
=(W=
32)-
(N=4
))))
+N)+
2)+7
)+7)
),Y=
N<<(
*r=!
-A))
);;}
 
Status
Not open for further replies.