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.

43 lines
1.0KB

  1. import itertools
  2. import sys
  3. import toolkit
  4. def is_taken(pos, state):
  5. taken = sum(pos + step in state for step in moves)
  6. if pos not in state and not taken:
  7. return True
  8. elif pos in state and taken >= 4:
  9. return False
  10. return pos in state
  11. def is_taken2(pos, state):
  12. taken = 0
  13. for step in moves:
  14. for x in itertools.count(1):
  15. adj = (pos + step * x)
  16. if adj in seats or adj not in grid:
  17. taken += adj in state
  18. break
  19. if pos not in state and not taken:
  20. return True
  21. elif pos in state and taken >= 5:
  22. return False
  23. return pos in state
  24. text = sys.stdin.read()
  25. grid, _, _ = toolkit.read_image(text)
  26. moves = {dx + dy for dx in [-1, 0, 1] for dy in [-1j, 0, 1j]} - {0}
  27. seats = {pos for pos, value in grid.items() if value != '.'}
  28. for fn in [is_taken, is_taken2]:
  29. state = frozenset()
  30. seen = set()
  31. while state not in seen:
  32. seen.add(state)
  33. state = frozenset({pos for pos in seats if fn(pos, state)})
  34. print(len(state))