This commit is contained in:
Sebastian Seedorf
2020-12-13 12:37:16 +01:00
parent 671787d627
commit 6c129f1750
3 changed files with 39 additions and 0 deletions

30
day13/part2.py Normal file
View File

@@ -0,0 +1,30 @@
import math
def lcm(a, b):
return abs(a*b) // math.gcd(a, b)
# an+b=xn+y -> n=(y-b)/(a-x)
# -> cn+d
def merge(a, b, x, y):
d1 = b
d2 = y
while d1 != d2:
if d1 < d2:
d1 += a * ((d2-d1-1) // a + 1)
else:
d2 += x * ((d1-d2-1) // x + 1)
d = d1
c = lcm(a, x)
return c, d
lines = [x.strip() for x in open("input.txt")]
busses = ((int(x), -idx) for idx, x in enumerate(lines[1].split(',')) if x != 'x')
current = (1, 0)
for bus in busses:
current = merge(*current, *bus)
print(current[1])