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.

9.py 825B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/python
  2. import re
  3. with open('9.txt', 'r') as file:
  4. input = file.read()
  5. FUNC = max # min for part 1
  6. distances = {}
  7. places = {}
  8. for line in input.splitlines():
  9. (source, dest, distance) = re.match('^(.*?) to (.*?) = ([0-9]+)$', line).group(1, 2, 3)
  10. distances[(source, dest)] = int(distance)
  11. distances[(dest, source)] = int(distance)
  12. places[dest] = True
  13. places[source] = True
  14. places = places.keys()
  15. def without(list, item):
  16. return [x for x in list if x != item]
  17. def find_shortest(current, to_visit):
  18. if len(to_visit) == 0:
  19. return 0
  20. return FUNC([distances[(current, target)] +
  21. find_shortest(target, without(to_visit, target))
  22. for target in to_visit])
  23. print(FUNC([find_shortest(place, without(places, place)) for place in places]))