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.

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/python
  2. from collections import defaultdict
  3. from itertools import permutations
  4. import re
  5. pattern = re.compile('^(.*) would (lose|gain) ([0-9]+) happiness .* next to (.*).')
  6. happiness = defaultdict(int)
  7. names = set()
  8. with open('13.txt', 'r') as file:
  9. for line in file.readlines():
  10. first, type, value, second = pattern.match(line).groups()
  11. happiness[frozenset((first, second))] += (-1 if type == 'lose' else 1) * int(value)
  12. names.add(first)
  13. def get_score(order):
  14. return sum(happiness[frozenset(pair)] for pair in zip(order, order[1:] + [order[0]]))
  15. def find_best(people):
  16. remaining = people.copy()
  17. first = remaining.pop()
  18. return max(get_score([first] + list(order)) for order in permutations(remaining))
  19. print(find_best(names))
  20. print(find_best(names.union("me")))