From 4c8beac3155278667fa7f99c27238a92157c05e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Mon, 14 Dec 2020 20:40:31 +0100 Subject: [PATCH] Day 13, part 2 --- 2020/13/main.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/2020/13/main.py b/2020/13/main.py index f9cf7d3..b321d56 100644 --- a/2020/13/main.py +++ b/2020/13/main.py @@ -1,3 +1,6 @@ +from functools import reduce + + test = ( 939, "7,13,x,x,59,x,31,19", @@ -9,15 +12,48 @@ data = ( ) -def part_1(departure, bus_ids): +def part1(departure, bus_ids): buses = [int(x) for x in bus_ids.split(",") if x != "x"] distances = [(departure // x + 1) * x - departure for x in buses] idx = distances.index(min(distances)) return buses[idx], distances[idx] -id, wait = part_1(*test) +def part2(bus_ids): + # t = bus_id % bus_id - idx + buses = [int(0 if x == "x" else x) for x in bus_ids.split(",")] + p = [x - buses.index(x) for x in buses if x != 0] + n = [x for x in buses if x != 0] + cr = chinese_remainder(n, p) + return cr + + +def chinese_remainder(n, p): + prod = reduce(lambda a,b: a*b, n) + + sum = 0 + for div, rem in zip(n, p): + pp = prod // div + i = pow(pp, -1, div) + sum += rem * pp * i + + return sum % prod + + +id, wait = part1(*test) assert id*wait == 295 -id, wait = part_1(*data) +id, wait = part1(*data) print(f"1: Answer is {id*wait}") + +assert chinese_remainder([3, 5, 7], [2, 3, 2]) == 23 + +assert part2(test[1]) == 1068781 +assert part2("17,x,13,19") == 3417 +assert part2("67,7,59,61") == 754018 +assert part2("67,x,7,59,61") == 779210 +assert part2("67,7,x,59,61") == 1261476 +assert part2("1789,37,47,1889") == 1202161486 + +ts = part2(data[1]) +print(f"2: Answer is {ts}")