Wrong solution for day 13, part 2

This computes *a* solution, but not the smallest one.
This commit is contained in:
Peter J. Holzer 2020-12-13 23:09:39 +01:00 committed by Peter J. Holzer
parent 216f590993
commit 272523b8b2
1 changed files with 50 additions and 0 deletions

50
13/part2 Executable file
View File

@ -0,0 +1,50 @@
#!/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)