You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
1234567891011121314151617181920212223242526272829303132333435363738
  1. import itertools
  2. # transform text into two helpful data structures
  3. valid = set()
  4. special_locations = {}
  5. for y, line in enumerate(data_file.read_text().splitlines()):
  6. for x, char in enumerate(line):
  7. if char != '#':
  8. valid.add((x, y))
  9. if char != '.':
  10. special_locations[(x, y)] = char
  11. # generate travel graph from every starting point to every other point via BFS
  12. graph = {}
  13. steps = [(0, 1), (0, -1), (1, 0), (-1, 0)]
  14. for A in special_locations:
  15. seen = {A}
  16. boundary = {A}
  17. locations_pending = set(special_locations) - seen
  18. for n_steps_taken in itertools.count(1):
  19. boundary = {(x + dx, y + dy) for x, y in boundary for dx, dy in steps}
  20. boundary &= valid - seen
  21. seen.update(boundary)
  22. for B in boundary & locations_pending:
  23. locations_pending -= {B}
  24. graph[special_locations[A], special_locations[B]] = n_steps_taken
  25. if not locations_pending:
  26. break
  27. # use map to determine routes
  28. z = '0'
  29. not_z = set(special_locations.values()) - {z}
  30. calc = lambda combo: sum(graph[a, b] for a, b in zip(combo, combo[1:]))
  31. ans1 = min(calc((z, *combo)) for combo in itertools.permutations(not_z))
  32. ans2 = min(calc((z, *combo, z)) for combo in itertools.permutations(not_z))