10.py 740 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. lines = [line.strip().split() for line in open("10.input")]
  2. X = 1
  3. cycle = 0
  4. snap_times = [20, 60, 100, 140, 180, 220]
  5. snaps = []
  6. pixels = []
  7. def run_cycle():
  8. global cycle, snap_times, snaps, pixels, X
  9. cycle += 1
  10. if cycle in snap_times:
  11. snaps.append(X)
  12. col = (cycle - 1) % 40
  13. if abs(col - X) <= 1:
  14. pixels.append("█")
  15. else:
  16. pixels.append(" ")
  17. for line in lines:
  18. if len(line) == 1:
  19. run_cycle()
  20. else:
  21. run_cycle()
  22. run_cycle()
  23. X += int(line[1])
  24. # Part 1
  25. prod = [a * b for a, b in zip(snaps, snap_times)]
  26. print(sum(prod))
  27. # Part 2
  28. for row in range(0, 6):
  29. for pos in range(row*40, (row+1)*40):
  30. print(pixels[pos], end='')
  31. print("")