Advent of Code 2016 solutions https://adventofcode.com/2016/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/python3
  2. """Solution for day 20 of Advent of Code 2016.
  3. This solution first compresses the blacklist by merging overlapping sections, using functools.reduce and the merge
  4. function below. For example, [(0, 100), (50, 150)] becomes (0, 150). The blacklist is then inverted to become a
  5. whitelist, which gives both answers pretty easily.
  6. """
  7. import functools
  8. def merge(x, y):
  9. if y[0] <= x[-1][1] + 1:
  10. # This entry overlaps with the last one, combine them
  11. x[-1] = (x[-1][0], max(y[1], x[-1][1]))
  12. else:
  13. # New, non-overlapping entry, just append it to our list
  14. x.append(y)
  15. return x
  16. with open('data/20.txt', 'r') as file:
  17. ranges = sorted(list(map(lambda x: tuple(map(int, x.strip().split('-'))), file.readlines())))
  18. blacklist = functools.reduce(merge, ranges[1:], [ranges[0]])
  19. whitelist = []
  20. last = 0
  21. for pair in blacklist:
  22. if pair[0] > last:
  23. whitelist.append((last, pair[0] - 1))
  24. last = pair[1] + 1
  25. if last < 4294967295:
  26. whitelist.append((last, 4294967295))
  27. print("Part 1: %s" % whitelist[0][0])
  28. print("Part 2: %s" % sum(map(lambda p: 1 + p[1] - p[0], whitelist)))