Day 13 (more glamor)

This commit is contained in:
Sebastian Seedorf
2020-12-13 15:50:14 +01:00
parent 6c129f1750
commit b22c9c9e48

View File

@@ -1,20 +1,19 @@
import math from math import gcd
def lcm(a, b): def lcm(a, b):
return abs(a*b) // math.gcd(a, b) return abs(a*b) // gcd(a, b)
# an+b=xn+y -> n=(y-b)/(a-x) # f(n)=an+b and f(m)=xm+y
# -> cn+d # Intersection of 2 sequences -> ck+d
def merge(a, b, x, y): def merge(a, b, x, y):
d1 = b while b != y:
d2 = y if b < y:
while d1 != d2: b += a * ((y-b-1) // a + 1)
if d1 < d2:
d1 += a * ((d2-d1-1) // a + 1)
else: else:
d2 += x * ((d1-d2-1) // x + 1) y += x * ((b-y-1) // x + 1)
d = d1 d = b
c = lcm(a, x) c = lcm(a, x)
return c, d return c, d
@@ -26,5 +25,4 @@ current = (1, 0)
for bus in busses: for bus in busses:
current = merge(*current, *bus) current = merge(*current, *bus)
print(current[1]) print(current[1])