08_2fa.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. import sys
  3. class Screen:
  4. def __init__(self, width, height):
  5. self.rows = [[False] * width for y in range(height)]
  6. def fill_rect(self, width, height):
  7. for y in range(height):
  8. for x in range(width):
  9. self.rows[y][x] = True
  10. def rotate_row(self, row, amount):
  11. self.rows[row][:] = self.rows[row][-amount:] + self.rows[row][:-amount]
  12. def rotate_col(self, col, amount):
  13. old = [row[col] for row in self.rows]
  14. for y, row in enumerate(self.rows):
  15. row[col] = old[y - amount]
  16. def count_on(self):
  17. return sum(sum(map(int, row)) for row in self.rows)
  18. def process(self, f):
  19. for line in f:
  20. words = line.split()
  21. if words[0] == 'rect':
  22. a, b = words[1].split('x')
  23. self.fill_rect(int(a), int(b))
  24. else:
  25. assert words[0] == 'rotate'
  26. fn = self.rotate_row if words[1] == 'row' else self.rotate_col
  27. fn(int(words[2][2:]), int(words[4]))
  28. def print(self):
  29. for row in self.rows:
  30. print(''.join('#' if px else '.' for px in row))
  31. screen = Screen(50, 6)
  32. screen.process(sys.stdin)
  33. print(screen.count_on(), 'pixels on:')
  34. screen.print()