Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

55 linhas
1.3KB

  1. def bfs_twinkle(start, graph, cycles):
  2. seen = [set()]
  3. state = {start}
  4. while len(seen) <= cycles:
  5. twinkle = seen[len(seen) - 2]
  6. state = {
  7. p + s
  8. for p in state
  9. for s in [1, -1, 1j, -1j]
  10. if wrap(p + s) in graph
  11. if p + s not in twinkle
  12. }
  13. seen.append(twinkle | state)
  14. return seen
  15. def extrapolate_quadratic(y0, y1, y2):
  16. a, b, c = (y2 - 2 * y1 + y0) / 2, (-y2 + 4 * y1 - 3 * y0) / 2, y0
  17. return lambda x: a * x ** 2 + b * x + c
  18. text = open(0).read()
  19. graph = {
  20. complex(x, y): val
  21. for y, row in enumerate(text.splitlines())
  22. for x, val in enumerate(row)
  23. if val in {'S', '.'}
  24. }
  25. width = max(int(p.real) for p in graph) + 1
  26. height = max(int(p.imag) for p in graph) + 1
  27. wrap = lambda p: complex(int(p.real % width), int(p.imag % height))
  28. start, = (k for k, v in graph.items() if v == 'S')
  29. seen = bfs_twinkle(start, graph, 327)
  30. if width < 100:
  31. check = len({wrap(p) for p in seen[6]})
  32. print(check)
  33. exit()
  34. ans1 = len({wrap(p) for p in seen[64]})
  35. print(ans1)
  36. goal = 26501365
  37. size, = {height, width}
  38. offset = goal % size
  39. xs = [offset + size * i for i in range(3)]
  40. ys = [len(seen[x]) for x in xs]
  41. yf = extrapolate_quadratic(ys[0], ys[1], ys[2])
  42. x = (goal - offset) / size
  43. ans2 = int(yf(x))
  44. print(ans2)