13_mirrors.py 960 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env python3
  2. import sys
  3. def reflect(pat, multiplier=100):
  4. for mirror in range(len(pat) - 1):
  5. if all(pat[mirror - dist] == pat[mirror + dist + 1]
  6. for dist in range(min(mirror + 1, len(pat) - mirror - 1))):
  7. yield multiplier * (mirror + 1)
  8. if multiplier == 100:
  9. yield from reflect(list(map(''.join, zip(*pat))), 1)
  10. def fix_and_reflect(pat, old_reflection):
  11. for y, row in enumerate(pat):
  12. for x, smudge in enumerate(row):
  13. newrow = row[:x] + '#.'[smudge == '#'] + row[x + 1:]
  14. newpat = pat[:y] + [newrow] + pat[y + 1:]
  15. for reflection in reflect(newpat):
  16. if reflection != old_reflection:
  17. return reflection
  18. patterns = [pat.split() for pat in sys.stdin.read().split('\n\n')]
  19. reflections = [next(reflect(pat)) for pat in patterns]
  20. print(sum(reflections))
  21. print(sum(fix_and_reflect(*p) for p in zip(patterns, reflections)))