Day 13
This commit is contained in:
30
day13/part2.py
Normal file
30
day13/part2.py
Normal 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])
|
||||
Reference in New Issue
Block a user