test = ( 939, "7,13,x,x,59,x,31,19", ) data = ( 1002462, "37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,601,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,x,17,x,x,x,x,x,23,x,x,x,x,x,29,x,443,x,x,x,x,x,x,x,x,x,x,x,x,13", ) def part_1(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) assert id*wait == 295 id, wait = part_1(*data) print(f"1: Answer is {id*wait}")