40 lines
814 B
Python
40 lines
814 B
Python
from lib import get_data, str_to_ints, mod_inverse
|
|
from functools import reduce
|
|
|
|
data = get_data(__file__)
|
|
lines = data.splitlines()
|
|
|
|
earliest = int(lines[0])
|
|
times = str_to_ints(lines[1])
|
|
|
|
mintime = 10**21
|
|
id = None
|
|
|
|
for time in times:
|
|
mintimecur = (earliest // time) * time + time
|
|
if mintimecur < mintime:
|
|
mintime = mintimecur
|
|
id = time
|
|
|
|
|
|
assert id is not None
|
|
print((mintime - earliest) * id)
|
|
|
|
fields = lines[1].split(",")
|
|
|
|
buses = []
|
|
for offset, busid in enumerate(fields):
|
|
if busid == "x":
|
|
continue
|
|
period = int(busid)
|
|
buses.append((period, -offset % period))
|
|
|
|
|
|
total = 0
|
|
product = reduce(lambda a, b: a * b, map(lambda x: x[0], buses))
|
|
for period, offset in buses:
|
|
p = product // period
|
|
total += offset * mod_inverse(p, period) * p
|
|
|
|
print(total % product)
|