Advent of Code 2016 solutions https://adventofcode.com/2016/
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 1.4KB

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/python3
  2. import itertools
  3. import re
  4. input_matcher = re.compile(r'^Disc #(.*?) has (.*?) positions; at time=0, it is at position (.*?)\.$')
  5. # Returns a generator for positions that this disc will be at when the capsule arrives. e.g. if disc 3 starts at
  6. # position 0 at time 0, the first returned position will be 3 (because the disc will have ticked around three times
  7. # before a capsule released at t=0 arrives).
  8. positions = lambda disc: ((disc[0] + disc[2] + i) % disc[1] for i in itertools.count())
  9. def run(lines):
  10. # Pull the information out from the text line, making sure we treat numbers as ints.
  11. discs = map(lambda line: tuple(map(int, input_matcher.match(line).groups())), map(str.strip, lines))
  12. # Zip together the positions of each disc. That means the first element in combos will show the positions each
  13. # disc will be at when the capsule arrives if it's released at t=0; the second element will be the positions for
  14. # a capsule released at t=1, etc.
  15. combos = zip(*map(positions, discs))
  16. # Find a time when all discs will be at position 0 for the arrival of the capsule.
  17. times = (i for i, c in enumerate(combos) if all(p == 0 for p in c))
  18. return next(times)
  19. with open('data/15.txt', 'r') as file:
  20. lines = file.readlines()
  21. print("Step 1: %s" % run(lines))
  22. print("Step 2: %s" % run(lines + ['Disc #7 has 11 positions; at time=0, it is at position 0.']))