Hacker News new | past | comments | ask | show | jobs | submit login
Obfuscated Game of Life in 9 lines of C (github.com/duckythescientist)
66 points by jackhammer2022 on Nov 29, 2013 | hide | past | favorite | 23 comments



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
  
  .


Nice explanation of each line is here:

https://github.com/duckythescientist/obfuscatedLife/blob/mas...

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.


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 __.


In most cases, a program would just read from "dirty" memory and you might get a random value. In this case, whatever bits are there, they "add up" to -1, so it just works.


Yes, it is undefined behavior by the standard, but most compilers will give you some random value and you will get the answer that GhotiFish states.


lets say __ is 0x00101101

then it's ~__+__

so

   0x00101101
  +0x11010010
You can see how that always works.


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.


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.


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-...


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?


as you said, reading it is just undefined. the compiler has to do something, and it's improbable that a read before write, followed by another read will result in two different values.

if you could produce a piece of code that exhibits the behaviour you describe, i'd very much like to see it.


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

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


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


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

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%...


Fun obfuscated version in ruby.

https://github.com/stefanpenner/obfcuscatedLife.rb/blob/mast...

and for those crazy people who ever far to trusting:

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


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


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.


> if it were written in pure CSS

> also a challenge

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


Apparently, it is: 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)


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


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...). It doesn't get much more portable than that.

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


[deleted]


One should strive to make c code as portable as possible. Obfuscated does not mean it should be hard to compile.


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: