with open("data13.txt") as f: buses = [(i,int(x)) for i,x in enumerate(f.read().split("\n")[1].split(",")) if x != "x"] num = buses[0][1] base = 0 mult = 1 omega_i = 1 while True: i = 0 base_found = False mult_found = False test = 0 print("CURR:", buses[1:omega_i+2], base, mult) while True: temp_base = base temp_mult = mult factor = temp_base + temp_mult * i curr = num * factor # print(i, curr, factor) flag = True for kth,bus in buses[1:omega_i+2]: if (curr - bus + kth) % bus != 0 or curr - bus + kth < 0: flag = False break if flag: if not base_found: print("BASE FOUND", factor) base_found = True base = factor test = base else: print("MULT FOUND", base, factor - test) mult_found = True mult = factor - test if mult_found: break i += 1 print(base * buses[0][1], mult, base) print("----------------------") if omega_i == len(buses)-1: break omega_i += 1 print("ANS:", base * num, "BASE:", base, "MULT:",mult)