5 ms·
Obfuscated Game of Life in 9 lines of C
- carlosdp 13y agoOn 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 13y agoThat'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 13y ago> if it were written in pure CSS > also a challenge Is CSS turing-complete? If not, game over.
- Someone 13y agoApparently, 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)
- babuskov 13y agoNice explanation of each line is here: https://github.com/duckythescientist/obfuscatedLife/blob/master/remarks.md 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.
- deleted 13y ago[deleted]
- twoodfin 13y agoIsn't reading an uninitialized variable that could have been given a "register" qualifier undefined behavior? That is, the program never takes the address of __.
- babuskov 13y agoIn 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.
- GhotiFish 13y agolets say __ is 0x00101101 then it's ~__+__ so 0x00101101 +0x11010010 You can see how that always works.
- CJefferson 13y agoUnfortunately 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.
- foxhill 13y agoas 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.
- babuskov 13y agoNot 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.
- cgcardona 13y agoI'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 13y agoWell, this is a submission to the IOCCC, so I don't think anyone will complain about the code's obfuscated nature :)
- deletes 13y agoSo much effort is ruined with the usage of usleep. Obfuscated C should be portable!
- deleted 13y ago[deleted]
- deletes 13y agoOne should strive to make c code as portable as possible. Obfuscated does not mean it should be hard to compile.
- ekr 13y agoWhat'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.html 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.
- lelandbatey 13y agoYoutube 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%20Life-LFx46bAq-_E.mp4 http://lelandbatey.com/files/Obfuscated%20C%20-%20Game%20Of%...
- iamstef 13y agoFun obfuscated version in ruby. https://github.com/stefanpenner/obfcuscatedLife.rb/blob/master/game_of_life.rb 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
- Aardwolf 13y agoAnd 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 .
- julienchastang 13y agoIn 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.