13_fold.py 929 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. import sys
  3. def read_dots(f):
  4. for line in f:
  5. if line == '\n':
  6. break
  7. x, y = line.split(',')
  8. yield int(x), int(y)
  9. def read_folds(f):
  10. for line in f:
  11. axis, at = line.split()[-1].split('=')
  12. yield axis == 'y', int(at)
  13. def flip(i, at):
  14. return i if i < at else i - 2 * (i - at)
  15. def fold(dots, up, at):
  16. return {(x, flip(y, at)) if up else (flip(x, at), y) for x, y in dots}
  17. def plot(dots):
  18. xmin = min(x for x, y in dots)
  19. xmax = max(x for x, y in dots)
  20. ymin = min(y for x, y in dots)
  21. ymax = max(y for x, y in dots)
  22. for y in range(ymin, ymax + 1):
  23. print(''.join(' #'[(x, y) in dots] for x in range(xmin, xmax + 1)))
  24. dots = list(read_dots(sys.stdin))
  25. first_fold, *other_folds = read_folds(sys.stdin)
  26. dots = fold(dots, *first_fold)
  27. print(len(dots))
  28. for up, at in other_folds:
  29. dots = fold(dots, up, at)
  30. plot(dots)