def part2(lines): t = 754018 # print(t % 67 == 0) # print(t % 7 == 6) # print(t % 59 == 57) # print(t % 61 == 58) # print('Part 2:') n = [] a = [] for i, bus in enumerate(lines[1].split(',')): try: n1 = int(bus) rem = (n1 - i) % n1 except ValueError: pass else: if i == 0 or True: #i != n[0]: n.append(n1) a.append(rem) else: print('skipping', n[0], n1, rem, i) for n1, n2 in combinations(n, 2): if gcd(n1, n2) != 1: print(' ', n1, n2) print(n) print(a) result = int(chinese_remainder(n, a)) for x, y in zip(n, a): print(x, result % x, y) print(result)
def useCRT(self): "Solve the puzzle using the Chineese Remainder Theorem" # 1. Adjust the positions adjusted = [] for offset, position in enumerate(self.positions): adjusted.append(-position - offset) # 2. Use CRT result = crt.chinese_remainder(self.sizes, adjusted) # 3. Drop time is one less if result == 0: return None return result - 1
def solve(self): chosen_holders = random.sample(self.__holders, self.__min_holder) if self.__verbose: print("Chosen holders :") for holder in chosen_holders: print(holder) ## Solver CRT modulo_list = [holder.modulo for holder in chosen_holders] remainder_list = [holder.secret for holder in chosen_holders] solution = chinese_remainder(modulo_list, remainder_list) if self.__verbose: print("CRT Solution : " + str(solution)) return (solution) % self.__sequence[0]
def main(): """ solutions """ timestamp = 1005162 puzzle_input = "19,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,823,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,"\ "17,x,x,x,x,x,x,x,x,x,x,x,29,x,443,x,x,x,x,x,37,x,x,x,x,x,x,13" # convert input to tuple list of positions in input and bus_ids bus_ids = [(x[0], int(x[1])) for x in enumerate(puzzle_input.replace('x', '1').split(','))] bus_ids = [*filter(lambda x: x[1] != 1, bus_ids)] # calculate departure time relative to timestamp times = [(x[0], (math.ceil(timestamp / x[1]) * x[1]) - timestamp) for x in bus_ids] # calculate the earliest departure time min_time = sorted(times, key=lambda x: x[1])[0] print("part 1:", min_time[1] * bus_ids[times.index(min_time)][1]) # find the earliest timestamp such that the first bus ID departs at that time and each # subsequent listed bus ID departs at that subsequent minute. # this boils down to a chinese remainder theorem problem find the congruent modulo seq_n = [x[1] for x in bus_ids] coprime_a = [x[1] - x[0] for x in bus_ids] print("part 2:", chinese_remainder(seq_n, coprime_a))
def test_rosetta_code_examples(self): "Test examples from rosettacode" self.assertEqual(crt.chinese_remainder([3, 5, 7], [2, 3, 2]), 23) self.assertEqual(crt.chinese_remainder([5, 13], [2, 3]), 42) self.assertEqual(crt.chinese_remainder([100, 23], [19, 0]), 1219) self.assertEqual(crt.chinese_remainder([11, 12, 13], [10, 4, 12]), 1000) self.assertEqual(crt.chinese_remainder([5, 7, 9, 11], [1, 2, 3, 4]), 1731) self.assertEqual( crt.chinese_remainder([ 17353461355013928499, 3882485124428619605195281, 13563122655762143587 ], [ 7631415079307304117, 1248561880341424820456626, 2756437267211517231 ]), 937307771161836294247413550632295202816)
import crt input = open('input.txt').read().split() ids = [int(e) for e in input[1].replace('x', '0').split(',')] # print(ids) frequency = [e for e in ids if e] # print(frequency) # print([ ids.index(e)for e in frequency ]) rem = [e - ids.index(e) for e in frequency] # print(rem) print(crt.chinese_remainder(frequency, rem))
def test_part_one_example(self): "Test example from part one description [disc sizes], [initial values]" self.assertEqual(crt.chinese_remainder([5, 2], [-4, -1 - 1]), 5 + 1)