Skip to content

Instantly share code, notes, and snippets.

@Luisgustavom1
Created May 25, 2024 21:33
Show Gist options
  • Save Luisgustavom1/84b07f07e6535810395ad87bb2f10a18 to your computer and use it in GitHub Desktop.
Save Luisgustavom1/84b07f07e6535810395ad87bb2f10a18 to your computer and use it in GitHub Desktop.
snail = function(array) {
if (array.length === 0) return []
if (array.length === 1) return array[0]
const a = []
let bxi = 0
let bxe = array[0].length - 1
let byi = 0
let bye = array.length - 1
let r = 0
let c = 0
let d = 1
const n = array.length
while(a.length !== n * n) {
a.push(array[r][c])
if (d === 1 && c < bxe) {
c++
} else if (d === -1 && c > bxi) {
c--
} else if (c === bxe) {
r++
} else if (c === bxi) {
r--
}
if (d === 1 && r === bye && c === bxe) {
d = -1
bxe--
byi++
}
if (d === -1 && r === byi && c === bxi) {
d = 1
bxi++
bye--
}
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment