|
7 | 7 | arrival = int(lines[0])
|
8 | 8 | buses = [(i, int(e)) for i, e in enumerate(lines[1].split(",")) if e.isdigit()]
|
9 | 9 |
|
10 | | -offsets, times = zip(*buses) |
| 10 | +times = [tfor_, tinbuses] |
11 | 11 | b = [e - (arrival % e) for e in times]
|
12 | 12 | print(np.min(b) * times[np.argmin(b)])
|
13 | 13 |
|
14 | | -def chinese_remainder(ns, rems): |
15 | | - p = prod(ns) |
16 | | - x = sum(r * (p // n) * pow(p // n, -1, n) for r, n in zip(rems, ns)) |
17 | | - return x % p |
| 14 | +def crt(ns, bs): |
| 15 | + # Chinese Remainder Theorem |
| 16 | + # https://brilliant.org/wiki/chinese-remainder-theorem/ |
| 17 | + N = prod(ns) |
| 18 | + x = sum(b * (N // n) * pow(N // n, -1, n) for b, n in zip(bs, ns)) |
| 19 | + return x % N |
18 | 20 |
|
19 | | -rems = [time-offset for offset, time in buses] |
20 | | -print(chinese_remainder(times, rems)) |
| 21 | +offsets = [time-idx for idx, time in buses] |
| 22 | +print(crt(times, offsets)) |
0 commit comments