7 ms·
Yahoo Logo ASCII Animation in 462 bytes of C (2011)
- cheeze 11y agoI always love these things. "X in Y lines of $language!" Then you look at it and realize that a "line" is actually 20 different statements minified. I found jquery implemented in a single line![1] [1] https://jquery.com/download/ https://jquery.com/download/
- Retr0spectrum 11y ago"[Update 10/21/2015: Changed the title from "six lines of C" to "462 bytes of C" to avoid endless arguments about what constitutes a line of C code.]"
- cryptoz 11y agoYes, I don't see how this isn't > Yahoo Logo ASCII Animation in one line of C (2011) What's the difference between six lines of C and one line of C?
- deleted 11y ago[deleted]
- rcy 11y ago400 characters? Assuming one line is 80 characters max.
- ghayes 11y agoYeah, I think you have to count a semi-colon as a "line of C" for this to be meaningful. It's still awesome none-the-less.
- steckerbrett 11y agofor(i=0;i<4;i++) This is three lines?
- giancarlostoro 11y agoI think he meant outside of for loops in function calls and declarations.
- _lce0 11y agoActually there are two types of ";", expression-separator (the one you mentioned) and statement-separator (the one parent meant) The problem with using those for code metrics is that many systems don't require separators, like `if`, `for` and friends
- a1k0n 11y agoFWIW, most of this code is actually contained inside for loop expressions, since you can chain them together with commas and get away without using any braces. Once that's done it's hard to even count "lines".
- MichaelBurge 11y agoWhy not? It expands to 2 statements and a statement-expression: int i = 0; while (i < 4) { i++; }
- lopatin 11y agoThe lines don't seem to be over 80 chars long. It's code golf so it's still going to be hard to read but the 80 char lines are acceptable in most code bases.
- oneeyedpigeon 11y agoCome on, were you really expecting that animation to be done in, like, 6 statements? Even once you understand the current of compression ratios minification of c code can achieve, this is still impressive. I bet the 'uncompressed' version of this code would still be interesting. Actually - here's where I kinda agree with you - it would probably be much more interesting, since we'd be able to understand it more readily.
- deleted 11y ago[deleted]
- a1k0n 11y agoI transformed the code into something sort of readable, although still somewhat difficult to understand: https://gist.github.com/a1k0n/15f6cc2fc9428d6fd197 https://gist.github.com/a1k0n/15f6cc2fc9428d6fd197
- cgriswald 11y ago"Lines of C" is an arbitrary and relatively meaningless metric. Even number of characters would have more meaning and perhaps be more interesting than number of 'lines'. Unless you define a "line" as 80 characters (in which case you really have a rough estimation of character count). Nonetheless, these things are fun and often novel, both in what they accomplish and how they accomplish it. I like looking at them (not always reading them) for many of the same reasons I like looking at brainfuck or perl code. Plus, you know, compression. ;)
- userbinator 11y agoEven if you run a formatter over the source and rename the variables to some longer names, these tiny programs are still algorithmically impressive. Otherwise they'd be nearly trivial to generate from bigger ones by running minifiers on them.
- agumonkey 11y agoMany where like minimally functioning PoC. When I thought about raytracing I always assumed large code base. Watching the bare principle in a snippet is pretty awesome.
- agumonkey 11y agoSometimes it's more than lexical minification though, you also learn a bit about the grammar and how to express something in fewer bits. Even tiny design tricks, like encoding binary as chars to express data.
- david-given 11y agoThis kind of game was common in BBC Basic, way back in the 80s and 90s. It was limited to 255-ish tokens per line. (Excitingly, the keyboard buffer was 255 characters, but each keyword was tokenised to a single byte. Which meant that with trickery it was possible to get lines of Basic longer than it was actually possible to edit.) At least one of the computer magazines of the day had a regular column of one-line BBC Basic programs sent in by readers. There were some quite good games. Alas, I suspect that most of these have been lost to history, but here's a one-line Tetris: 0d=d:IFdVDUd:a=POINT(32*POS,31-VPOS<<5):RETURNELSEMODE9:GCOL-9:CLG:O FF:d=9:REPEATVDU30:REPEATGOSUBFALSE:IFPOS=28VDUPOS,15,VPOS,24;11,26:IF0E LSEIFa=0PRINT:UNTIL0ELSEUNTILVPOS=25:v=ABSRNDMOD7:i=0:VDU4895;3:REPEATm= 9-INKEY6MOD3:FORr=TRUETO1:t=rANDSGNt:IFt=rCOLOURv-15:VDUrEORm:i+=m=7AND9 -6*r:IF0ELSEFORn=0TO11:d=n/3OR2EORd:GOSUBFALSE:IF1<<(n+i)MOD12AND975AND& C2590EC/8^vVDU2080*ABSr;:t+=a:IF0ELSENEXT,:VDU20:UNTILt*LOGm:UNTILVPOS=3 http://survex.com/~olly/rheolism/dsm_rheolism/ http://survex.com/~olly/rheolism/dsm_rheolism/
- deleted 11y ago[deleted]
- x0ra 11y agoAhem... No wonder they tried to hide the warnings... % gcc -o yanim yanim.c -lm yanim.c:1:1: warning: data definition has no type or storage class yanim.c:1:1: warning: type defaults to 'int' in declaration of 'c' [-Wimplicit-int] yanim.c:1:3: warning: type defaults to 'int' in declaration of 'p' [-Wimplicit-int] yanim.c:1:5: warning: type defaults to 'int' in declaration of 'i' [-Wimplicit-int] yanim.c:1:7: warning: type defaults to 'int' in declaration of 'j' [-Wimplicit-int] yanim.c:1:9: warning: type defaults to 'int' in declaration of 'n' [-Wimplicit-int] yanim.c:1:11: warning: type defaults to 'int' in declaration of 'F' [-Wimplicit-int] yanim.c:1:16: warning: type defaults to 'int' in declaration of 'k' [-Wimplicit-int] yanim.c:1:18: warning: type defaults to 'int' in declaration of 'm' [-Wimplicit-int] yanim.c:1:40: warning: return type defaults to 'int' [-Wimplicit-int] yanim.c: In function 'main': yanim.c:1:56: warning: implicit declaration of function 'usleep' [-Wimplicit-function-declaration] yanim.c:1:72: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration] yanim.c:2:52: warning: implicit declaration of function 'putchar' [-Wimplicit-function-declaration] yanim.c:5:24: warning: implicit declaration of function 'cos' [-Wimplicit-function-declaration] yanim.c:5:24: warning: incompatible implicit declaration of built-in function 'cos' yanim.c:5:24: note: include '<math.h>' or provide a declaration of 'cos' yanim.c:5:33: warning: implicit declaration of function 'sin' [-Wimplicit-function-declaration] yanim.c:5:33: warning: incompatible implicit declaration of built-in function 'sin' yanim.c:5:33: note: include '<math.h>' or provide a declaration of 'sin'
- swiley 11y agoMaybe it would be better to just specify the length of the source file in bytes, so we don't have these weird arguments about line length.
- inoop 11y agoMy personal favorite is Viznut's classic signature: #include <math.h> /* outputs 8 kHz 8-bit unsigned pcm to stdout */ main(v,i,z,n,u,t){for(v=-1;;)for(n=pow(/* gcc -lm sig.c;./a.out>/dev/dsp */ 1.06,"`cW`g[`cgcg[eYcb^bV^eW^be^bVecb^"[++v&31]+(v&64)/21),i=999;i;putchar( 128+((8191&u)>i?0:i/8)-((8191&(z+=n))*i-->>16)))u+=v&1?t/2: (t=v&6?t:n/4);} edit: fixed copy/paste error
- e19293001 11y agoGot an error when I compile: $ gcc -lm sig.c sig.c: In function ‘main’: sig.c:3: error: expected ‘;’ before numeric constant Fixed this statement: < i=9 99; > i=999; Garbage is displayed when I execute the code.
- Rotten194 11y agoYou have to pipe it into an audio player (traditionally that was /dev/dsp IIRC but I don't think that's a thing anymore). aplay should work with the right flags.
- Narishma 11y agoIt's not a thing anymore on modern (pulseaudio-enabled) Linux but you can still use applications that write to /dev/dsp by launching them with padsp.
- a1k0n 11y agoI am a big fan of viznut also, and took the liberty of translating this into Javascript so we can all hear it without trying to find a /dev/dsp analog (I used ./viznut | sox -t raw -e unsigned -b 8 -r 8000 - -d): https://jsbin.com/tazeyoqoci/edit?html,output https://jsbin.com/tazeyoqoci/edit?html,output Amazing work.
- inoop 11y agoHey wow, this is great, thanks!
- cantrevealname 11y agoYou can do super dense mathematical code in the ancient APL programming language[1]. I wonder how even more tiny this program could get at the hands of an APL expert if any still exist. Here are some complete APL programs[2] to give a flavor of just how dense it can get: Sum integers from 1 to 100: +/ι100 Find all primes from 2 to a given number R: (∼R∈R◦.×R)/R←1↓ιR Play Conway's Game of Life: life←{↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵} [1] https://en.wikipedia.org/wiki/APL_%28programming_language%29 https://en.wikipedia.org/wiki/APL_%28programming_language%29 [2] http://www.vaxman.de/publications/apl_slides.pdf http://www.vaxman.de/publications/apl_slides.pdf
- robotmlg 11y agoI've always enjoyed this video of a guy showing how to write Conway's Game of Life in APL, step by step: https://www.youtube.com/watch?v=a9xAKttWgP4 https://www.youtube.com/watch?v=a9xAKttWgP4
- a1k0n 11y agoSpeaking of crazy ascii company logo animations, I never got a chance to golf these down, but I made some with color and signed distance field ray marching: https://github.com/a1k0n/asciitracing https://github.com/a1k0n/asciitracing
- userbinator 11y agoI think raymarching is an extremely elegant way to make complex scenes fit into a tiny amount of space. These are my favourite articles on raymarching because it shows just how compact they can be - a single mathematical equation: http://iquilezles.org/www/articles/distfunctions/distfunctions.htm http://iquilezles.org/www/articles/distfunctions/distfunctio... http://iquilezles.org/www/articles/raymarchingdf/raymarchingdf.htm http://iquilezles.org/www/articles/raymarchingdf/raymarching... Edit: The description for the "bridge" scene is interesting: "This is a sketch really, but it never got completed, due to the lack of artistic appealing of the image." I don't know if it's his extreme humility, but I think it wouldn't be overexaggerating if I said it's one of the best scenes on that page.
- anon4 11y agoOne more crazy thing you can do with raymarching - smoothly twine any shape into any other shape. Take any two signed distance fields, say they're defined by the functions dist0(p) and dist1(p). Then you can do dist(p, t) = (1-t)*dist0(p) + t*dist1(p) T doesn't have to be time-based, it can be space-based, so you have a shape that's a cylinder on the bottom and a large sphere on top and here's a mushroom https://www.shadertoy.com/view/lsSXWc https://www.shadertoy.com/view/lsSXWc Edit: I have a very strong feeling that in most cases the resulting function isn't a "proper" distance field, but you can still raymarch through it normally.
- bhickey 11y agoAndy, you're very much to ascii rendering what Inigo is to the demo scene. I hope Milwaukee has been treating you well.
- agumonkey 11y agoAlways enjoyed your previous projects. This one doesn't disappoint. Do you live in a tty ?
- dheera 11y agoWonder if someone can create a startup to do Obfuscated-C-Code-as-a-service.
- cfarre 11y agome too
- hliyan 11y agoPerhaps we should consider replacing line count with something like "parameter weighted statement count" as a measure of software size?
- seyfulislam 11y agoEverything is six lines when you have semicolon.
- onion2k 11y agoThat's very cool. You can do something similar using Chrome's console output. There used to be an intro that worked by rendering a canvas as ascii but it's broken in newer Chrome - http://sandbox.photonstorm.com/console.log/ http://sandbox.photonstorm.com/console.log/ (it may work for other people). I figured out how it works and made a little animation to explore the principle - http://ooer.com/console/i.html http://ooer.com/console/i.html (You'll need to change the height of the console to fit 16 lines of output)
- z3r0d4v3 11y agoif you have some time to kill... http://mnml.is/hn/jslinux.jpg http://mnml.is/hn/jslinux.jpg