logo CodeStepByStep logo

shuttleSearch2

Write a method named shuttleSearch2 (similar to the previous exercise, shuttleSearch) that processes similar input to the previous exercise, but this time, finds the earliest timestamp such that the first bus ID departs at exactly that time and each subsequent listed bus ID departs at the subsequent minute. (The first line in your input is no longer relevant.)

As in the previous exercise, your method accepts as its parameter a string representing the name of a file of bus data. For example, suppose the file named schedule.txt contains the following text:

939
7,13,x,x,59,x,31,19

You are now looking for the earliest timestamp T such that:

  • Bus ID 7 departs at exactly T.
  • Bus ID 13 departs at T + 1.
  • Bus ID 59 departs at T + 4. (the two "x" marks between indicate that there is no constraint on time T + 2 nor T + 3.)
  • Bus ID 31 departs at T + 6.
  • Bus ID 19 departs at T + 7.

In our example, the earliest timestamp T at which this occurs is 1068781. So the call of shuttleSearch("schedule.txt") would return 1068781.

To help simplify your implementation, you may assume that all of the bus IDs are unique and that they are prime numbers.

You may assume that the file exists and is readable and follows the format described above.

(This exercise is based on the Advent of Code 2020, day 13.)

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.