Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Created April 17, 2018 16:16
Show Gist options
  • Save Ni55aN/877be987884710ccf6bd4d7b8ef4529d to your computer and use it in GitHub Desktop.
Save Ni55aN/877be987884710ccf6bd4d7b8ef4529d to your computer and use it in GitHub Desktop.
snail = function(array) {
var side = 'top';
var result = [];
while(array.length > 0){
switch(side){
case 'top':
result.push(...cutRow(0, array));
side = 'right';
break;
case 'right':
result.push(...cutColumn(array[0].length - 1, array));
side = 'bottom';
break;
case 'bottom':
result.push(...cutRow(array.length - 1, array, true));
side = 'left';
break;
case 'left':
result.push(...cutColumn(0, array, true));
side = 'top';
break;
}
}
return result;
}
function cutRow(i, array, reverse){
const res = array.splice(i, 1)[0];
return reverse?res.reverse():res;
}
function cutColumn(i, array, reverse){
const res = array.map(arr => arr.splice(i, 1)[0]);
return reverse?res.reverse():res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment