Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

29 lines
738B

  1. import collections
  2. import re
  3. import sys
  4. def calc(pos):
  5. is_black = pos in active
  6. count = sum(pos + step in active for step in maps.values())
  7. if is_black and count not in {1, 2}:
  8. return False
  9. elif not is_black and count == 2:
  10. return True
  11. return is_black
  12. text = sys.stdin.read()
  13. maps = {'w': 2, 'e': -2, 'nw': 1+1j, 'ne': -1+1j, 'sw': 1-1j, 'se': -1-1j}
  14. grid = collections.defaultdict(bool)
  15. for line in text.splitlines():
  16. end = sum(map(maps.get, re.findall(r'(se|sw|nw|ne|e|w)', line)))
  17. grid[end] = not grid[end]
  18. active = {pos for pos in grid if grid[pos]}
  19. print(len(active))
  20. for _ in range(100):
  21. active = {p + s for p in active for s in maps.values() if calc(p + s)}
  22. print(len(active))