TehVulpez

snakepath.py

Jul 28th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def snakepath(path, size):
  2.     x = path[-1]%size
  3.     y = path[-1]//size
  4.     possible = []
  5.    
  6.     if 0 <= y-1 and path[-1]-size not in path: #up
  7.         possible.append(path[-1]-size)
  8.     if 0 <= x-1 and path[-1]-1 not in path: #left
  9.         possible.append(path[-1]-1)
  10.     if x+1 < size and path[-1]+1 not in path: #right
  11.         possible.append(path[-1]+1)
  12.     if y+1 < size and path[-1]+size not in path: #down
  13.         possible.append(path[-1]+size)
  14.    
  15.     for direction in possible:
  16.         new = path+(direction,)
  17.         yield new
  18.         for p in snakepath(new, size):
  19.             yield p
  20.  
  21. size = 6
  22. start = 0, 1
  23.  
  24. for path in snakepath(start, size):
  25.     if len(path) == size**2 and path[-1] == size:
  26.         print(*path, sep='-')
Advertisement
Add Comment
Please, Sign In to add comment