Last active
November 13, 2016 18:30
-
-
Save railsstudent/f28fa9ec9c4348a4bd81697ef368882f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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