Advertisement
Guest User

spaghetti

a guest
Nov 29th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. from math import sqrt
  2. from itertools import count, islice
  3.  
  4. def is_prime(n):
  5. return n > 1 and all(n % i for i in islice(count(2), int(sqrt(n)-1)))
  6.  
  7. N = 100
  8.  
  9. adj = [[] for _ in range(N)]
  10.  
  11. for i in range(1, N):
  12. for j in range(1, i):
  13. if is_prime(i + j):
  14. adj[i].append(j)
  15.  
  16.  
  17. grid = [[-1] * N for _ in range(N)]
  18.  
  19. def prettyprint():
  20. cp = [i[:] for i in grid]
  21. for i in range(len(cp)):
  22. for j in range(len(cp)):
  23. if cp[i][j] == -1:
  24. cp[i][j] = ''
  25. else:
  26. cp[i][j] = str(cp[i][j])
  27. cp[i][j] = cp[i][j].rjust(3)
  28. strings = [''.join(i) for i in cp]
  29. minleading = min(len(a) - len(a.lstrip()) for a in strings)
  30. maxending = min(len(a) - len(a.rstrip()) for a in strings)
  31. print('\n'.join(i[minleading:][:-maxending] for i in strings if not len(set(i)) == 1))
  32.  
  33.  
  34. def nei(x, y):
  35. for nx, ny in (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1):
  36. if 0 <= nx < N and 0 <= ny < N:
  37. yield nx, ny
  38.  
  39. best = [0]
  40.  
  41. def dfs(current, x, y):
  42. if current > best[0]:
  43.  
  44. best[0] = max(best[0], current)
  45. print('-' * 100)
  46. prettyprint()
  47.  
  48. for nx, ny in nei(x, y):
  49. if grid[nx][ny] == -1:
  50. for x1, y1 in nei(nx, ny):
  51. if grid[x1][y1] in adj[current + 1]:
  52. break
  53. else:
  54. continue
  55. grid[nx][ny] = current + 1
  56. dfs(current + 1, nx, ny)
  57. grid[nx][ny] = -1
  58.  
  59. grid[N//2][N//2] = 1
  60. dfs(1, N//2, N//2)
  61. print(best)
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement