···11+import re
22+import fileinput
33+import itertools
44+from utils import parse_line
55+66+77+def button_timing(discs):
88+ """Returns the first time at which the button should be pressed."""
99+ for i in itertools.count():
1010+ for time, (positions, initial) in enumerate(discs, start=i+1):
1111+ if (initial + time) % positions != 0:
1212+ break
1313+ else:
1414+ return i
1515+1616+1717+DISCS = []
1818+DISC_RE = re.compile(r'Disc #\d+ has (\d+) positions; at time=0, it is at position (\d+).')
1919+2020+for line in fileinput.input():
2121+ disc = parse_line(DISC_RE, line.strip())
2222+ DISCS.append(disc)
2323+2424+print "Timing to press button:", button_timing(DISCS)
2525+print "Timing with added disc:", button_timing(DISCS + [(11, 0)])
+6
2016/input15.txt
···11+Disc #1 has 13 positions; at time=0, it is at position 10.
22+Disc #2 has 17 positions; at time=0, it is at position 15.
33+Disc #3 has 19 positions; at time=0, it is at position 17.
44+Disc #4 has 7 positions; at time=0, it is at position 1.
55+Disc #5 has 5 positions; at time=0, it is at position 0.
66+Disc #6 has 3 positions; at time=0, it is at position 1.