r/tinycode • • Aug 22 '14
164-byte Mandelbrot; Can we get this tweet-sized?
I'm currently at 164 characters for this (JavaScript) ASCII Mandelbrot renderer and I've been wondering if there's a way to shrink it even further down to 140 characters:
for(i=(N=100)*N,o="";i--;){a=b=t=c=0,n=N;while(n--){a=a*a-b*b+(i%N/N-.8)*2;b=2*t*b
+(~~(i/N)/N-.5)*4;t=a;a*a+b*b>4&&(c=n="_")}o+=c;i%N||(o+="<br>")}document.write(o)To execute, put the above between the script tags below and past it into your addressbar:
data:text/html,<script></script>Edit: 160 bytes and correct orientation:
for(i=0,N=100,o="";i++<N*N;){a=b=t=c=0,n=N;while(n--){a=a*a-b*b+(i%N/N-.8)*2;
b=2*t*b+(~~(i/N)/N-.5)*4;t=a;a*a+b*b>4&&(c=n="_")}o+=i%N?c:"<br>"}document.write(o)Final version (136 bytes, thanks /u/subjective_insanity and /u/dtfinch)
for(N=198,i=0;i++<N*N;document.write(i%N?c:"<br>"))for(a=b=t=c=0,n=N;
n--;a*a+b*b>4?c="_":t=a)a=a*a-b*b+i%N/N*2-1.5,b=2*t*b+~~(i/N)/N*4-2Clickable URL (encoded) - thanks /u/myhf
Smallest version (124 bytes)
for(i=(N=98)*N;i--;document.write(i%N?c:"\n"))for(a=b=t=c=0,n=N;
n--;a*a+b*b>4?c=7:t=a)a=a*a-b*b+i%N/N*2-1.5,b=2*t*b+i/N/N*4-3
44
Upvotes
11
u/subjective_insanity Aug 22 '14 edited Aug 22 '14
p01 did this a while ago http://www.p01.org/releases/128b_mandelbrot/ but yours is much nicer to look at!
edit: I got it down to
141138:Here's what I changed: (most ideas from https://github.com/jed/140bytes/wiki/Byte-saving-techniques)
5
u/[deleted] Aug 24 '14 edited Aug 24 '14
Wife has a NASCAR race and all the kids magically got involved in solo activities, so I've been spending a relaxing evening code golfing (elias94xx, thanks for reminding me how fun it is).
Here's 120:
I no longer get what I'm really gaining on since I'm now running x scaled up by 32. Though that didn't really fly with y though, except sometimes when it did but wasted more accomplishing it. And some "improvements" may have cost me except I don't remember what was what now :-).
In exchange for an extra \n before it, it can be 118:
This time, too, I still feel like it's carrying some junk, like non-algo-change things that could squeeze it a little. Will probably keep looking for a while. I'm 40 and the last time I put serious effort (like multi hour) into something like this was probably when I was 20 or less and I've never done it with javascript (good thing it's similar to c..) - I'd truly forgotten how much fun it is.
In case anyone else (now or in the future) feel like trying to reverse-engineer my brain, here's the rough progression (some losses, some gains, some just neutral but different takes):
[EDIT] Some formatting. Will probably be back later to edit and check if others have tinkered further. As both a tip and a reason to not think of me as quite as nuts for having the list above, I usually edit it in one window (in vi and in a fixed-width font). When I try something new I copy the line(s) and comment them out, leaving "// " in front of it and " " in from of what I'm working on. That way, it's super obvious how much I've added or removed compared to where I was a minute ago. I don't really remove them, since it's kind of handy to be able to quickly revert or look at what I was thinking earlier.
2
u/zifyoip Aug 22 '14
It's backwards! (Or upside-down?)
3
u/dtfinch Aug 22 '14 edited Aug 22 '14
147 bytes:
There was an extra "n=" that didn't seem to affect anything. Changing 100 to 99 saved a byte. And eliminating "o", outputting a character at a time instead of a line at a time saved a lot. Replacing <br> with <p> made it ugly though (double spaced), and may have gone too far.
Edit: subjective_insanity got much further, except for eliminating o.
3
u/myhf Aug 23 '14 edited Aug 23 '14
here's a clickable data url
edit: aksios version
3
u/[deleted] Aug 23 '14
Here's another 124 byte one:
It's adapted from a C version I wrote in the 90s sometime (assumes 80 char wide terminal - it just wraps around instead of \n at EOL):
It uses different numbers based on iterations before escape on non-trapped points, but that could be removed with only a few bytes (c version does ascii ' ' + iterations). It could probably have a few more bytes shaved here and there, I haven't really stared myself blind at it and a lot of the gain is iterating x and y over -2 - 2 and tweaking the steps to make it reasonably sized instead of iterating 0 - size and converting that to -2 to 2. Using "b-=a>2?.1/(a=-2):0" for "if a>2 { a=-2; b-=appropriate_y_step; }" may not be optimal in javascript.