Skip to content

Instantly share code, notes, and snippets.

@trsqxyz
Created June 12, 2015 07:09
Show Gist options
  • Save trsqxyz/7809b6ca667f120b9838 to your computer and use it in GitHub Desktop.
Save trsqxyz/7809b6ca667f120b9838 to your computer and use it in GitHub Desktop.
import sys
import math
def compass(n):
cells = math.ceil(math.sqrt(n))
maps = [[[] for _ in range(cells)] for __ in range(cells)]
row = math.ceil(cells//2)
col = row if cells%2 == 0 else row + 1
pos = 'S'
direction = {
'N':((-1, 0), 'EN'),
'E':(( 0, 1), 'SE'),
'W':(( 0, -1), 'NW'),
'S':(( 1, 0), 'WS'),
}
for pin in range(n):
for path in direction.get(pos)[1]:
motion = direction.get(path)[0]
r = row + motion[0]
c = col + motion[1]
if maps[r][c] == []:
row, col = r, c
maps[r][c] = pin
pos = path
break
return maps
def main(n):
maps = compass(int(n))
for m in maps:
m = list(map(str, m))
m = [' '*len(n) if _ == '[]' else _.rjust(len(n)) for _ in m]
print(' '.join(m))
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment