Skip to content

Instantly share code, notes, and snippets.

@xem xem/LICENSE.txt forked from 140bytes/LICENSE.txt
Last active Dec 25, 2015

Embed
What would you like to do?
Snail matrix generator

I made a 133 bytes Snail function such as :

snail(4) =

[[1,  2,  3,  4],
  12, 13, 14, 5],
  11, 16, 15, 6],
  10, 9,  8,  7]]

(and the same logic for snail(N), with N >= 0)

s = function(n,a,d,x,y,c,i){
// a is the result matrix
a = [[]];
// d is the direction: right, bottom = 1; left, top = -1
d = 1;
// c is the counter value.
c = 0;
// x and y are the coordinates of the number in the matrix
x = 0;
y = -1;
// for n in [n ... 0]
for(; n; n--){
// NB: the following two for loops are merged in the minified code
// fill n columns
for(i = 0; i < n; i++){
y += d;
a[x][y] = ++c;
}
// fill n-1 lines
for(i = 0; i < n - 1; i++){
x += d;
if(!a[x]){
a[x] = [];
}
a[x][y] = ++c;
}
// change direction
d = -d;
}
return a;
}
s=function(e,c,d,b,f,h,g){c=[[]];d=1;b=h=0;for(f=-1;e;e--){for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]||(c[b]=[]),c[b][f]=++h;d=-d}return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "snail",
"description": "generates a snail matrix",
"keywords": [
"snail",
"matrix"
]
}
<script>
s=function(e,c,d,b,f,h,g){c=[[]];d=1;b=h=0;for(f=-1;e;e--){for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]||(c[b]=[]),c[b][f]=++h;d=-d}return c}
console.log(s(4));
</script>
@atk

This comment has been minimized.

Copy link

commented Oct 15, 2013

Save one byte by removing the curly brackets:

function(e,c,d,b,f,h,g){for(c=[[]],d=1,b=h=0,f=-1;e;e--)for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]||(c[b]=[]),c[b][f]=++h;d=-d;return c}
@hillerstorm

This comment has been minimized.

Copy link

commented Oct 23, 2013

That unfortunately breaks it :/

This one saves 2 bytes by removing brackets (moving d=-d to the last part of the for loop) and moving b=h=0 inside the array definition:

function(e,c,d,b,f,h,g){c=[[b=h=0]];d=1;for(f=-1;e;e--,d=-d)for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]||(c[b]=[]),c[b][f]=++h;return c}
@hillerstorm

This comment has been minimized.

Copy link

commented Oct 23, 2013

Saving another 2 bytes by changing the check/assignment of c[b]:

function(e,c,d,b,f,h,g){c=[[b=h=0]];d=1;for(f=-1;e;e--,d=-d)for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]=c[b]||[],c[b][f]=++h;return c}
@hillerstorm

This comment has been minimized.

Copy link

commented Oct 23, 2013

And one more byte (!) by moving to a var-definition instead of placeholder arguments:

function(e){var c=[[]],d=1,b=0,h=0,f,g;for(f=-1;e;e--,d=-d)for(g=1;g<2*e;g++)g>e?b+=d:f+=d,c[b]=c[b]||[],c[b][f]=++h;return c}
@subzey

This comment has been minimized.

Copy link

commented Nov 14, 2013

-6 bytes:

function(e){for(var c=[[]],d=1,b=0,h=0,f=-1,g;e;e--,d=-d)for(g=1;g<2*e;c[b][f]=++h)g++>e?c[b+=d]=c[b]||[]:f+=d;return c}
@subzey

This comment has been minimized.

Copy link

commented Nov 14, 2013

I want to ask also, is it necessary c to be [[]], not just [].
The only difference is snail(0) would return [] instead of [[]], but as for me, it is even better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.