Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Last active November 13, 2016 18:30
Show Gist options
  • Save railsstudent/f28fa9ec9c4348a4bd81697ef368882f to your computer and use it in GitHub Desktop.
Save railsstudent/f28fa9ec9c4348a4bd81697ef368882f to your computer and use it in GitHub Desktop.
snail = (array) ->
# enjoy
traversal = []
if array.length is 1 and array[0].length is 0 then return []
row = 0
col = 0
turn = 0
direction = 'E'
while traversal.length < (array.length * array.length)
traversal.push (array[row][col])
# consider next square to visit
if direction is 'E'
col += 1
# need to change direction?
if col is (array.length - 1) - turn then direction = 'S'
else if direction is 'S'
row += 1
if row is (array.length - 1) - turn then direction = 'W'
else if direction is 'W'
col -= 1
if col is turn then direction = 'N'
else if direction is 'N'
row -= 1
if row is (turn + 1)
direction = 'E'
turn += 1
traversal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment