15_cookies.py 871 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. def divide(amount, nbuckets):
  5. if nbuckets == 1:
  6. yield (amount,)
  7. else:
  8. for bucket in range(0, amount + 1):
  9. for rest in divide(amount - bucket, nbuckets - 1):
  10. yield (bucket,) + rest
  11. def scores(ingredients, spoons):
  12. for buckets in divide(spoons, len(ingredients)):
  13. props = (max(sum(b * s for b, s in zip(buckets, stats)), 0)
  14. for stats in zip(*ingredients))
  15. cap, dur, fla, tex, cal = props
  16. yield cap * dur * fla * tex, cal
  17. ingredients = [tuple(map(int, re.findall(r'(-?\d+)', line)))
  18. for line in sys.stdin]
  19. maxall = max500 = 0
  20. for score, calories in scores(ingredients, 100):
  21. if score > maxall:
  22. maxall = score
  23. if calories == 500 and score > max500:
  24. max500 = score
  25. print(maxall)
  26. print(max500)