AoC 2020 D13: Shuttle Search
| Problem statement | Source code | Tags: Modular arithmetic
← Previous Back to AoC Index Next →
Part 1
Once we have the list of bus IDs, we can compute the next time each bus arrives, which is the smallest multiple of the bus ID that is greater than or equal to the earliest timestamp: np.ceil(time / buses) * buses. We then find the bus with the smallest arrival time with np.argmin.
Part 2
If buses[i] departs at timestamp t + i, that means t + i is a multiple of buses[i], or . If we denote each pair as , then we just need to solve the system of congruences for the lowest positive . A nice observation is that all bus IDs are prime numbers, which means that by the Chinese Remainder Theorem, there exists a unique solution for modulo .
To find the solution, we iteratively satisfy each congruence while holding all previous ones true. Start with , , which denotes . For each congruence