Files
python-aoc-2020/day13/part2.py
Sebastian Seedorf b22c9c9e48 Day 13 (more glamor)
2020-12-13 15:50:14 +01:00

29 lines
558 B
Python

from math import gcd
def lcm(a, b):
return abs(a*b) // gcd(a, b)
# f(n)=an+b and f(m)=xm+y
# Intersection of 2 sequences -> ck+d
def merge(a, b, x, y):
while b != y:
if b < y:
b += a * ((y-b-1) // a + 1)
else:
y += x * ((b-y-1) // x + 1)
d = b
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])