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.

56 satır
1.5KB

  1. import sys
  2. from functools import lru_cache
  3. from toolkit import read_image
  4. def simple_adjace(level, pos):
  5. inbound = {pos + ori for ori in [1, -1, 1j, -1j]} & domain
  6. return [(level, p) for p in inbound]
  7. @lru_cache(maxsize=None)
  8. def recursive_adjace(level, pos, center=complex(2, 2)):
  9. neis = []
  10. for ori in [1, -1, 1j, -1j]:
  11. adj = pos + ori
  12. if adj == center:
  13. neis += [(level + 1, p) for p in domain if p - ori not in domain]
  14. elif adj not in domain:
  15. neis += [(level - 1, center + ori)]
  16. else:
  17. neis += [(level, adj)]
  18. return neis
  19. def evolve(bugs, adjacency, lim=None):
  20. seen_states = set()
  21. while lim is None or len(seen_states) < lim:
  22. affected = {nei for tile in bugs for nei in adjacency(*tile)} | bugs
  23. infested = set()
  24. dead = set()
  25. for tile in affected:
  26. count = sum(nei in bugs for nei in adjacency(*tile))
  27. if tile in bugs and count != 1:
  28. dead.add(tile)
  29. elif tile not in bugs and count in [1, 2]:
  30. infested.add(tile)
  31. bugs = bugs - dead | infested
  32. if bugs in seen_states:
  33. break
  34. seen_states.add(bugs)
  35. return bugs
  36. text = sys.stdin.read()
  37. domain = set(read_image(text))
  38. bugs = frozenset((0, pos) for pos, c in read_image(text).items() if c == '#')
  39. final = evolve(bugs, simple_adjace)
  40. print(int(sum(2 ** (p.real + p.imag * 5) for _, p in final)))
  41. final = evolve(bugs, recursive_adjace, 200)
  42. print(len(final))