02_colorcubes.py 541 B

12345678910111213141516
  1. #!/usr/bin/env python3
  2. import sys
  3. def parse(line):
  4. maximums = [0, 0, 0]
  5. for hand in line.split(': ', 1)[1].split('; '):
  6. for ty in hand.split(', '):
  7. amount, color = ty.split()
  8. i = ('red', 'green', 'blue').index(color)
  9. maximums[i] = max(maximums[i], int(amount))
  10. return maximums
  11. games = list(map(parse, sys.stdin))
  12. print(sum(i + 1 for i, game in enumerate(games)
  13. if all(real <= expect for real, expect in zip(game, (12, 13, 14)))))
  14. print(sum(r * g * b for r, g, b in games))