adventofcode-2020/13/part2

51 lines
968 B
Plaintext
Raw Normal View History

#!/usr/bin/python3
def ext_euclid(a, b):
"""
returns solutions x and y for equation ax + by = gcd(a, b)
"""
(old_r, r) = (a, b)
(old_s, s) = (1, 0)
(old_t, t) = (0, 1)
while r:
quotient = old_r // r
(old_r, r) = (r, old_r - quotient * r)
(old_s, s) = (s, old_s - quotient * s)
(old_t, t) = (t, old_t - quotient * t)
return old_s, old_t, old_r
n = []
a = []
with open("input") as fh:
now = int(fh.readline())
for i, x in enumerate(fh.readline().split(",")):
if x != "x":
x = int(x)
n.append(x)
a.append((x - i) % x)
print(n)
print(a)
n1 = n.pop()
n2 = n.pop()
a1 = a.pop()
a2 = a.pop()
m1, m2, _ = ext_euclid(n1, n2)
assert _ == 1
x = a1 * m2 * n2 + a2 * m1 * n1
print(x)
while n:
n1 = n1 * n2
a1 = x
n2 = n.pop()
a2 = a.pop()
m1, m2, _ = ext_euclid(n1, n2)
assert _ == 1
x = a1 * m2 * n2 + a2 * m1 * n1
print(x)