Advertisement
Guest User

PathFinderCircleOfSquares.py

a guest
May 5th, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Usage: First commandline argument is max.
  4. #
  5. # This script finds all possible paths that make a circle of squares
  6. # and prints them.
  7. #
  8. # The output still contains duplicates and mirrors.
  9. #
  10.  
  11. import sys
  12.  
  13. high = int( sys.argv[1] )
  14. ring = tuple([ x+1 for x in range(high) ])
  15. squares = set([ x**2 for x in range(1, high) if x**2 < 2*high ])
  16. final = []
  17.  
  18.  
  19.  
  20. # Create dict of possible squares.
  21. possq = dict()
  22.  
  23. for base in ring:
  24. possq[base] = []
  25. for num in ring:
  26. if num + base in squares and num != base:
  27. possq[base].append(num)
  28.  
  29. for key in possq.keys():
  30. possq[key] = tuple(possq[key])
  31.  
  32. firstkey = possq[1]
  33.  
  34. # Print dictionary with possible squares.
  35. for i, j in possq.items():
  36. # print(i, j)
  37. pass
  38.  
  39. # Find circle
  40.  
  41. path = [1]
  42. a = 0
  43. x = 1
  44. error = []
  45. term = len(possq[1])
  46.  
  47. while not (len(path) == 1 and a == term):
  48. # print(path)
  49. while a < len(possq[path[-1]]):
  50. if possq[path[-1]][a] not in path:
  51. path.append(possq[path[-1]][a])
  52. a = 0
  53. if len(path) == high:
  54. print(*path, sep=' ')
  55. break
  56. a += 1
  57. else:
  58. b = path.pop()
  59. a = possq[path[-1]].index(b) + 1
  60.  
  61.  
  62. # Print result
  63.  
  64. Final = []
  65. [ Final.append(i) for i in final if i not in Final ]
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement