News for hackers. Stuff that matters.
Re:2048 implemented in 487 bytes of C by nullc on Saturday April 05, 2014 @12:46AM (#7535989)
Like the small JS version this also has the bug where you can summon more tiles by pushing again in a direction where movement isn't allowed.
Re:2048 implemented in 487 bytes of C by Almaviva on Saturday April 05, 2014 @12:55PM (#7537583)
He shouldn't call his game "2048" if it has that bug. It's a much easier game than the original one, as you are never forced to move a big number out of a corner and you never fear the dreaded position where you have exactly one open row and you are forced to move all your big numbers away from the edge.
Re:2048 implemented in 487 bytes of C by deletes on Saturday April 05, 2014 @01:18AM (#7536056)
Doesn't run correctly under windows. The code relies on the system call "stty cbreak", which is linux specific.

'stty' is not recognized as an internal or external command, operable program or batch file.

I would love to read comments instead of downvotes. Where am I wrong?

Re:2048 implemented in 487 bytes of C by ghewgill on Saturday April 05, 2014 @01:56AM (#7536132)
`stty` is not Linux specific, it's common to all Unix-derived systems (including Mac OS X, for example). Windows is not Unix-derived.
Re:2048 implemented in 487 bytes of C by deletes on Saturday April 05, 2014 @02:01AM (#7536143)
HN got really trigger happy in the last few weeks.

That part of the comment only is partially correct, and not was even the main point of my comment; what I really meant was that it doesn't belong in windows, and the main point was that the code is not portable.

Re:2048 implemented in 487 bytes of C by pseut on Saturday April 05, 2014 @02:12AM (#7536159)
A reaction to the new moderation proposals, maybe?
Re:2048 implemented in 487 bytes of C by bananas on Saturday April 05, 2014 @03:57AM (#7536325)
Were you expecting WINAPI wMainMain(...)?
Re:2048 implemented in 487 bytes of C by deletes on Saturday April 05, 2014 @04:21AM (#7536378)
As an obfuscated c example I was expecting portable code. Anyone hoping that as many people as possible try their code should aim for that.
Re:2048 implemented in 487 bytes of C by bananas on Saturday April 05, 2014 @05:31AM (#7536513)
Obfuscated C is always with caveats. The fact that it uses terminal escapes and stty suggests its not going to work on anything other than a colour xterm, if you're lucky, there are no sunspots and the wind is blowing in the correct direction...
Re:2048 implemented in 487 bytes of C by peterkelly on Saturday April 05, 2014 @07:24AM (#7536701)
I'm not sure that's a common goal for obfuscated c code
Re:2048 implemented in 487 bytes of C by cubancigar11 on Saturday April 05, 2014 @04:51AM (#7536434)
Same here. stty is available through cygwin, although I didn't result in a playable game.
Re:2048 implemented in 487 bytes of C by mden on Saturday April 05, 2014 @12:56AM (#7536012)
Huh, was not aware of the "Implicit Int Rule" - http://stackoverflow.com/questions/11064292/declaring-variab....
Re:2048 implemented in 487 bytes of C by ika on Saturday April 05, 2014 @01:08AM (#7536040)
Re:2048 implemented in 487 bytes of C by geocar on Saturday April 05, 2014 @04:03AM (#7536339)
How is that more beautiful?
Re:2048 implemented in 487 bytes of C by dsego on Saturday April 05, 2014 @04:25AM (#7536383)
uglified = minifed
Re:2048 implemented in 487 bytes of C by brandonbloom on Saturday April 05, 2014 @01:11AM (#7536043)
While 487 bytes is impressive, it feels like you should round up to the nearest power of two...
Re:2048 implemented in 487 bytes of C by QuantumChaos on Saturday April 05, 2014 @12:45PM (#7537555)
That's nothing. Let me know when someone manage to implement 2048 in 2048 lines of Java
Re:2048 implemented in 487 bytes of C by userbinator on Saturday April 05, 2014 @07:40AM (#7536730)
I haven't tried it, but a game of this complexity looks like it could be implemented in roughly the same number of bytes of binary, meaning it fits in a bootsector...

Coincidentally, a sector on optical media is typically 2048 bytes.

Re:2048 implemented in 487 bytes of C by akira2501 on Saturday April 05, 2014 @04:38PM (#7538356)
ISO 9660 is 2352 bytes, actually. The logical size is 2048 bytes, but the other bytes are needed for error correction and other headers.
Re:2048 implemented in 487 bytes of C by jevinskie on Saturday April 05, 2014 @12:56AM (#7536013)

    puts(W&1?"WIN":"LOSE");
Shouldn't exit(W&1); be "good enough" while saving some bytes?
Re:2048 implemented in 487 bytes of C by ghewgill on Saturday April 05, 2014 @02:01AM (#7536144)
Unfortunately it's the same every time it is played. It needs some srand() love in there somewhere.
Re:2048 implemented in 487 bytes of C by corruptio on Wednesday April 09, 2014 @06:05AM (#7558495)
Author here. Per many requests, here is an annotated version https://gist.github.com/justecorruptio/10248923
Re:2048 implemented in 487 bytes of C by 3327 on Saturday April 05, 2014 @01:57AM (#7536133)
well, bugs or not, 487 bytes is a new high bar... I guess the lisp lads might need to curl up their sleeves.
Re:2048 implemented in 487 bytes of C by bjterry on Saturday April 05, 2014 @03:10AM (#7536240)
On any modern filesystem, the source will take up substantially more than 487 bytes anyway. Perhaps you should round up to the nearest block size.
Re:2048 implemented in 487 bytes of C by pkrumins on Saturday April 05, 2014 @12:31PM (#7537513)
I'd understand 2048 implemented in 2048 bytes of C, but this I can't understand.
Re:2048 implemented in 487 bytes of C by mariuscoto on Saturday April 05, 2014 @03:15AM (#7536248)
Can I build this using gcc ?
Re:2048 implemented in 487 bytes of C by ghewgill on Saturday April 05, 2014 @03:51AM (#7536305)
Sure:

    gcc -o 2048 2048.c
Ignore all the warnings.
Re:2048 implemented in 487 bytes of C by mariuscoto on Saturday April 05, 2014 @04:06AM (#7536352)
That was the first command I have tried, but I get many of those: error: use of undeclared identifier 'X'. I am running OSX.
Re:2048 implemented in 487 bytes of C by sampo on Saturday April 05, 2014 @10:56AM (#7537230)
If you haven't installed gcc on your OSX, the command gcc actually calls clang.

http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-li...

Re:2048 implemented in 487 bytes of C by cyliu on Saturday April 05, 2014 @04:11AM (#7536359)
It sounds like the result of g++
Re:2048 implemented in 487 bytes of C by randv on Saturday April 05, 2014 @02:50AM (#7536216)
nothing is returned from function s?
Re:2048 implemented in 487 bytes of C by mschuster91 on Saturday April 05, 2014 @03:38AM (#7536285)
the only usage of the "s" function is down in T(i), and the "i" parameter passed to T() is ignored anyway ("for (i = X + rand() % X;"). It's just needed to save a int i; in the function itself.
Re:2048 implemented in 487 bytes of C by ohmygeek on Saturday April 05, 2014 @12:23PM (#7537489)
This is called sheer brilliance!
Re:2048 implemented in 487 bytes of C by sawmebabe on Saturday April 05, 2014 @05:06AM (#7536468)
wow. im more than impressed 0.0 wonderful
Re:2048 implemented in 487 bytes of C by BorisMelnik on Saturday April 05, 2014 @01:19AM (#7536062)
Re:2048 implemented in 487 bytes of C by myliverhatesme on Saturday April 05, 2014 @03:29AM (#7536266)
No
Re:2048 implemented in 487 bytes of C by plg on Saturday April 05, 2014 @10:31AM (#7537169)
I never understood why this sort of thing is interesting. Let's say you can program 2048 in 487 bytes of C ... and then someone else comes along and they can program it in 387 bytes of C. So what? Is there a use case in the year 2014 where 100 bytes matter?

How many bytes would it take to code it in C in a human readable fashion? Such that some other programmer could look at it, understand it, modify it, fix bugs, etc?

Is the savings in bytes going from the latter case to the former case really an issue in 2014, and does it outweigh the downside (which I think is enormous) of generating obfuscated code?

Having said all that, as an exercise purely in "how small can by code be, regardless of how obfuscated I make it", I suppose it is ... interesting?

but even still I have to admit I don't get the appeal

Re:2048 implemented in 487 bytes of C by umanwizard on Saturday April 05, 2014 @10:49AM (#7537211)
Dude, it's just for fun. It's done in the same spirit as making speed runs for video games. Does everything have to be completely utilitarian?
Re:2048 implemented in 487 bytes of C by userbinator on Saturday April 05, 2014 @12:06PM (#7537423)
It's also very good mental exercise, since it forces you to think even more about the corner cases of the language and its syntax. Even if you won't be using all these corner cases in production code (hopefully), it will make you a better programmer.

I find that reading minified/obfuscated code really helps with spotting syntax errors and related subtle bugs in regularly formatted code. Instead of relying on cues like indentation, you begin to parse more like a compiler, and things like missing semicolons stand out.

Re:2048 implemented in 487 bytes of C by plg on Saturday April 05, 2014 @11:05AM (#7537258)
Point taken. I think I need to take a vacation
Re:2048 implemented in 487 bytes of C by yosyp on Saturday April 05, 2014 @10:51AM (#7537218)
Although the obfuscated case is really pushing it in terms of code usability and readability, being able to write miniaturized code is really important in some industries (ie defense projects).

Take for example the Kinetis KL02 ( http://www.engadget.com/2014/02/25/freescale-kinetis-kl03/ ). With only 32KB of storage and 4KB of RAM, writing efficient code with a small footprint is important. These microprocessors can be used to make swarms of autonomous fly sized drones, and programmers that can cram more procedures into those 32KB are extremely rare and useful.

Re:2048 implemented in 487 bytes of C by adsr on Saturday April 05, 2014 @11:12AM (#7537277)
That could be true for other embedded projects as well, but then it's the size of the executable binary that is of concern, not necessarily the size of the source code on your development box.
Re:2048 implemented in 487 bytes of C by userbinator on Saturday April 05, 2014 @12:10PM (#7537438)
Exceptions like C++ templates exist (in which a small amount of source can expand to a huge binary), but usually there tends to be a general correlation between the size of the source and the binary.

Trying to write as little source code as possible also leads to trying to find the simplest, most concise algorithm to do a particular task, and that also has effects on the binary size.

Re:2048 implemented in 487 bytes of C by adsr on Saturday April 05, 2014 @12:28PM (#7537503)
This isn't C++. Just adding whitespace, descriptive variable and function names and comments wouldn't add any size to the resulting binary since the code is semantically the same. Using library functions can keep your amount of lines low, but you get that back when at link time if you do static linking at least. Wouldn't you prefer maintainable code that people can easily understand. I just want this said since obfuscated minimal C code isn't the same as minified JS.

Algorithms is an interesting case since you have to ask if you are optimizing for size or speed, as an example you can implement a linear search in fewer lines of code than binary search. Does that mean that linear search is a better more optimized choice. It does if you are optimizing for small executable binary size.

Re:2048 implemented in 487 bytes of C by wglb on Saturday April 05, 2014 @12:29PM (#7537507)
Think of it as a puzzle.

Checkout the demoscene stuff and superpacking JS.