Day 13 (more glamor)
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
import math
|
||||
from math import gcd
|
||||
|
||||
|
||||
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)
|
||||
# -> cn+d
|
||||
# f(n)=an+b and f(m)=xm+y
|
||||
# Intersection of 2 sequences -> ck+d
|
||||
def merge(a, b, x, y):
|
||||
d1 = b
|
||||
d2 = y
|
||||
while d1 != d2:
|
||||
if d1 < d2:
|
||||
d1 += a * ((d2-d1-1) // a + 1)
|
||||
while b != y:
|
||||
if b < y:
|
||||
b += a * ((y-b-1) // a + 1)
|
||||
else:
|
||||
d2 += x * ((d1-d2-1) // x + 1)
|
||||
d = d1
|
||||
y += x * ((b-y-1) // x + 1)
|
||||
d = b
|
||||
c = lcm(a, x)
|
||||
return c, d
|
||||
|
||||
@@ -26,5 +25,4 @@ current = (1, 0)
|
||||
for bus in busses:
|
||||
current = merge(*current, *bus)
|
||||
|
||||
|
||||
print(current[1])
|
||||
|
||||
Reference in New Issue
Block a user