From 259f974f325ac88d5f8cb31991b64ec8157cdf4a Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Mon, 14 Dec 2020 22:02:32 +0100 Subject: [PATCH] Use sieve method instead of algebraic method --- 13/part2 | 70 +++++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/13/part2 b/13/part2 index 3d25a0b..cd5d013 100755 --- a/13/part2 +++ b/13/part2 @@ -1,50 +1,32 @@ #!/usr/bin/python3 +import time -def ext_euclid(a, b): - """ - returns solutions x and y for equation ax + by = gcd(a, b) - """ +def shuttle_generator(): + with open("input") as fh: + now = int(fh.readline()) + for i, x in enumerate(fh.readline().split(",")): + if x != "x": + x = int(x) + yield i, x - (old_r, r) = (a, b) - (old_s, s) = (1, 0) - (old_t, t) = (0, 1) +shuttle = shuttle_generator() - 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 +def check(t, tests): + for i, n in tests: + if (t + i) % n != 0: + return False + return True -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) +t = 0 +step = 1 +n = 1 +tests = [] +while True: + if check(t, tests): + print(t, "matches", tests) + time.sleep(2) + step *= n + i, n = next(shuttle) + tests.append((i, n)) + t += step -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)