
Obfuscated Game of Life in 9 lines of C - jackhammer2022
https://github.com/duckythescientist/obfuscatedLife
======
Aardwolf
And to run this program in a _single line of bash_ on Unix systems (does
create a ./a.out file):

    
    
      echo 'int main(){int _[2048],O=(int)&O,__=~__+__;while((__=-~__)^2048)__[_]=(O=(O*0x41C64E6D+12345)&0x7fffffff)&1024?1<<5:0;while(usleep('"'"'d'"'"'<<'"'"'\n'"'"'),__+=~__){while((__=-~__)^8192){if(_[((__>>2)+2048+"\x01?@A"[__%4])%04000]&1<<5)*(_+(__>>2))=-~_[__>>2];if(_[((__>>2)+04000-"\x01?@A"[__%4])%2048]&1<<5)*(_+(__>>2))=-~_[__>>2];if(__%4==3)_[__>>2]|=_[__>>2]^'"'"'\"'"'"'&&_[__>>2]^'"'"'#'"'"'?((_[__>>2]^3)?0:1<<4):1<<4;};while((__=-~__)^10240)if(putchar((_[__%2048]=_[__%2048]<<1&1<<5)?'"'"'X'"'"':'"'"' '"'"'),!(63^__%0100))putchar(10);}};' | gcc -xc - && ./a.out
      
      .

------
babuskov
Nice explanation of each line is here:

[https://github.com/duckythescientist/obfuscatedLife/blob/mas...](https://github.com/duckythescientist/obfuscatedLife/blob/master/remarks.md)

Code is obfuscated more than needed to run. It starts with something simple
like this:

"I abuse 2's complement math to initialize it as -1"

Instead of: __ = -1 The code reads: __=~__+__

... and then gets more and more bizarre.

~~~
twoodfin
Isn't reading an uninitialized variable that could have been given a
"register" qualifier undefined behavior? That is, the program never takes the
address of __.

~~~
GhotiFish
lets say __ is 0x00101101

then it's ~__+__

so

    
    
       0x00101101
      +0x11010010
    

You can see how that always works.

~~~
CJefferson
Unfortunately not. When you have not written to a C variable, it doesn't have
a random value, reading it is undefined. Different reads can produce different
values.

~~~
babuskov
Not really. When you declare a variable the system assigns it an address on
stack. When you try to read it, it reads the value currently stored at that
address. Subsequent reads all read from the same address.

Unless you have something else writing to this address, the value will not
change. Since the program's stack is only available to that program, it will
stay the same.

~~~
twoodfin
Yes, really. I don't think you understand the leeway that C/C++ give
implementations when they encounter undefined behavior. Your mental model of
how the code "should" be compiled can go out the window when an optimizer
designed to work properly only on well-defined code goes to work:

[http://blog.llvm.org/2011/05/what-every-c-programmer-
should-...](http://blog.llvm.org/2011/05/what-every-c-programmer-should-
know.html)

~~~
babuskov
I'm sorry, but nothing in that article contradicts what I wrote. I wrote that
once variable is declared, a memory location is assigned to it and this
location nor it's content change over time, unless you write to (initialize)
it yourself.

Care to quote the sentence(s) that say differently?

------
cgcardona
I'm sure many would complain about the obfuscated nature of the code. I
however find it strangly beautiful. It has the appearance of a strand of DNA
and then one marvels at all of the complexity hidden in these cryptic lines.

To see it:

`git clone
[https://github.com/duckythescientist/obfuscatedLife.git](https://github.com/duckythescientist/obfuscatedLife.git)

cd obfuscatedLife

gcc life.c

./a.out`

Here is a screenshot[1] for anyone who didn't run the code. However a
screenshot hardly does the dynamic nature of it justice.

[1] [http://imgur.com/hWzEWAe](http://imgur.com/hWzEWAe)

~~~
hkmurakami
Well, this is a submission to the IOCCC, so I don't think anyone will complain
about the code's obfuscated nature :)

------
lelandbatey
Youtube link for those who are unwilling or unable to download and compile
yourself:

[http://www.youtube.com/watch?v=LFx46bAq-
_E](http://www.youtube.com/watch?v=LFx46bAq-_E)

As well, for those that don't want to use youtube, you can view/stream the raw
video here:

[http://lelandbatey.com/files/Obfuscated%20C%20-%20Game%20Of%...](http://lelandbatey.com/files/Obfuscated%20C%20-%20Game%20Of%20Life-
LFx46bAq-_E.mp4)

------
iamstef
Fun obfuscated version in ruby.

[https://github.com/stefanpenner/obfcuscatedLife.rb/blob/mast...](https://github.com/stefanpenner/obfcuscatedLife.rb/blob/master/game_of_life.rb)

and for those crazy people who ever far to trusting:

    
    
      curl https://raw.github.com/stefanpenner/obfcuscatedLife.rb/master/game_of_life.rb | ruby

------
carlosdp
On a cool side note, I don't know if this is old, but if you Google "Conway's
Game of Life", an actual game of life is run on the page:
[https://www.google.com/#q=conway's+game+of+life](https://www.google.com/#q=conway's+game+of+life)

~~~
Someone
That's old. Nowadays, it would only be impressive if it were written in pure
CSS.

Yes, that's a snark, but also a challenge to those with time on their hands.
Bonus points if your solution grows the universe to contain, e.g., an escaping
glider.

~~~
lloeki
> if it were written in pure CSS

> also a challenge

Is CSS turing-complete? If not, game over.

~~~
Someone
Apparently, it is: [https://github.com/elitheeli/stupid-
machines](https://github.com/elitheeli/stupid-machines) (I haven't taken the
time to check that, but take it that 'the internet' would have corrected this
by now if it weren't true)

------
deletes
So much effort is ruined with the usage of usleep. Obfuscated C should be
portable!

~~~
ekr
What's not portable about the usleep function? It's in the POSIX standard for
portable operating systems
([http://pubs.opengroup.org/onlinepubs/7990989799/xsh/usleep.h...](http://pubs.opengroup.org/onlinepubs/7990989799/xsh/usleep.html)).
It doesn't get much more portable than that.

Edit: Apparently, it has indeed been removed from SUSv4.

------
julienchastang
In APL, it is one line of code
[http://catpad.net/michael/apl/](http://catpad.net/michael/apl/). Whether it
is obfuscated or not is a matter of interpretation.

