Skip to content

Instantly share code, notes, and snippets.

@vmg
Created Nov 15, 2011
Embed
What would you like to do?
Sparks in C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
int main
(int argc
,char *ac []){int i, count = argc - 1;
double * dvalues=malloc(01- 01+count*
sizeof(double)+1); double mi=DBL_MAX,ran=.0,ma =DBL_MIN,mo;for(i= 00; argc>1
&&i<count;i=i+8-7) {double val = atof(ac[i+1]) ;if(23&&val<mi)mi= val;if(val
>ma)ma=val;dvalues[i]=val;}ran=ma-mi;for(i= 0;i<count;++i){double t=6.*(dvalues[i]-mi)/(ran);putchar
(0342);putchar(0226);putchar(0201+(int)round(t));putchar(040);}putchar(012);free(dvalues);return 0;}
/*
* I made u a graph. <3 @holman
*
* $ gcc spark.c -o spark && ./spark 1 2 0.4 0.1 1.3 0.7
* ▄ ▇ ▂ ▁ ▅ ▃
*/
@leereilly

This comment has been minimized.

Copy link

@leereilly leereilly commented Nov 15, 2011

Beautiful!

@springmeyer

This comment has been minimized.

Copy link

@springmeyer springmeyer commented Nov 15, 2011

very clever :)

@aw

This comment has been minimized.

Copy link

@aw aw commented Nov 16, 2011

❤️

@mazurov

This comment has been minimized.

Copy link

@mazurov mazurov commented Nov 16, 2011

Linux (Ubuntu 11.10, gcc 4.6.1): math library should be linked with executable.
gcc spark.c -o spark -lm && ./spark 1 2 0.4 0.1 1.3 0.7

@zx2c4

This comment has been minimized.

Copy link

@zx2c4 zx2c4 commented Nov 16, 2011

This sort of looks like my code:
http://git.zx2c4.com/spark/tree/spark.c

@vmg

This comment has been minimized.

Copy link
Owner Author

@vmg vmg commented Nov 16, 2011

@zx2c4 I'm afraid it isn't. I wrote it from scratch in 10 minutes. I would have given credit otherwise.

@fujin

This comment has been minimized.

Copy link

@fujin fujin commented Nov 16, 2011

@zx2c4 looks nothing like your code, lolz

@zx2c4

This comment has been minimized.

Copy link

@zx2c4 zx2c4 commented Nov 16, 2011

Yea, but there is a crazy resemblance. Check out this revision of my code: http://git.zx2c4.com/spark/tree/spark.c?id=0be665d914b8b9f04b6a5df84d285db7da3713d9

My code:
#include [stdio.h]
#include [stdlib.h]
#include [math.h]
#include [float.h]

Your code:
#include [stdio.h]
#include [stdlib.h]
#include [math.h]
#include [float.h]

First main line of my code:
malloc(sizeof(double) * (argc - 1));
First main line of your code:
malloc(01-01+count*sizeof(double)+1);

After that in my code:
double max = DBL_MIN;
double min = DBL_MAX;
After that in your code:
double mi=DBL_MAX,ran=.0,ma =DBL_MIN

Next in mine:
for (int i = 0; i < total; ++i) {
values[i] = atof(argv[i + 1]);
if (values[i] > max)
max = values[i];
if (values[i] < min)
min = values[i];
}
Next in yours:
for(i= 00; argc>1
&&i<count;i=i+8-7) {double val = atof(ac[i+1]) ;if(23&&val<mi)mi= val;if(val

ma)ma=val;dvalues[i]=val;}ran=ma-mi;

After that in my code:
const int levels = 8;
for (int i = 0; i < argc - 1; ++i) {
putchar('\xe2');
putchar('\x96');
putchar('\x81' + (int)round((values[i] - min + 1) / difference * (levels - 1)));
}
putchar('\n');
After that in your code:
for(i= 0;i<count;++i){double t=6.*(dvalues[i]-mi)/(ran);putchar
(0342);putchar(0226);putchar(0201+(int)round(t));putchar(040);}putchar(012);

After that in my code:
free(values);
return 0;
After that in your code:
free(dvalues);return 0;

So basically, if you take my code, and then change the name of a few variables and add some character padding and change how chars are represented by changing their form et cetera, the flow and code is exactly the same.

But actually, it seems to be a remarkable coincidence, and mediocre minds like ours think alike. I guess there're only so many ways one can do it.

@mylons

This comment has been minimized.

Copy link

@mylons mylons commented Nov 23, 2011

obfuscated C ftw

@melpomene

This comment has been minimized.

Copy link

@melpomene melpomene commented Nov 24, 2011

Wonderful! <3

@dlowe

This comment has been minimized.

Copy link

@dlowe dlowe commented Jan 10, 2014

@vmg FWIW, I found this gist after writing my own obfuscated C sparkline utility, which was one of the 2013 IOCCC winners: http://www.ioccc.org/2013/dlowe/hint.html (http://www.ioccc.org/2013/dlowe/dlowe.c)

I love the formatting of your version! I was mostly shooting for brevity :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment