Advent of code 2015 solutions https://adventofcode.com/2015/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15.py 909B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python
  2. import operator
  3. import functools
  4. from collections import defaultdict
  5. PART_TWO = True
  6. ingredients = []
  7. with open('15.txt', 'r') as file:
  8. for line in file.readlines():
  9. _, details = line.split(': ')
  10. ingredients.append({p: int(v) for p, v in (x.split(' ') for x in details.split(', '))})
  11. def sums(n, target):
  12. if n == 1:
  13. yield [target]
  14. else:
  15. for i in xrange(target + 1):
  16. for j in sums(n - 1, target - i):
  17. yield [i] + j
  18. def score(amounts):
  19. properties = defaultdict(int)
  20. for i, amount in enumerate(amounts):
  21. for k, v in ingredients[i].iteritems():
  22. properties[k] += v * amount
  23. if properties.pop('calories') != 500 and PART_TWO:
  24. return -1
  25. return functools.reduce(operator.mul, (max(0, v) for v in properties.values()))
  26. print(max(map(score, sums(len(ingredients), 100))))