Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

52 Zeilen
1.0KB

  1. import sys
  2. from intcode import compute
  3. WALL, MOVE, GOAL = range(3)
  4. N, S, W, E = range(1, 5)
  5. ORIENTATIONS = {-1j: N, 1j: S, -1: W, 1: E}
  6. commands = []
  7. command_iter = iter(commands)
  8. text = sys.stdin.read()
  9. bot = compute(text, command_iter)
  10. # hug the wall until we get back home
  11. pos, ori, grid = 0, -1j, {}
  12. while 0 not in grid:
  13. commands += [ORIENTATIONS[ori]]
  14. result = next(bot)
  15. if result == WALL:
  16. grid[pos + ori] = 1
  17. ori *= -1j
  18. else:
  19. if result == GOAL:
  20. target = pos
  21. grid[pos + ori] = 0
  22. pos += ori
  23. ori *= 1j
  24. # determine viable spaces
  25. valid = {p for p, v in grid.items() if v == 0}
  26. # from start to end
  27. hops = 0
  28. seen = set()
  29. edge = {0}
  30. while target not in seen:
  31. hops += 1
  32. seen |= edge
  33. edge = {p + d for p in edge for d in ORIENTATIONS} & valid - seen
  34. print(hops)
  35. # from end to all
  36. hops = 0
  37. seen = set()
  38. edge = {target}
  39. while seen != valid:
  40. hops += 1
  41. seen |= edge
  42. edge = {p + d for p in edge for d in ORIENTATIONS} & valid - seen
  43. print(hops)