Skip to content

Commit 6a24b5e

Browse files
committed
day 13
1 parent 93b5609 commit 6a24b5e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

day13/main.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
arrival = int(lines[0])
88
buses = [(i, int(e)) for i, e in enumerate(lines[1].split(",")) if e.isdigit()]
99

10-
offsets, times = zip(*buses)
10+
times = [t for _, t in buses]
1111
b = [e - (arrival % e) for e in times]
1212
print(np.min(b) * times[np.argmin(b)])
1313

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
1820

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

Comments
 (0)