logo CodeStepByStep logo

shuttleSearch

Write a method named shuttleSearch that processes a file of bus schedules looking for the soonest bus to take. Your method accepts as its parameter a string representing the name of a file of bus schedule data.

The file contains exactly two lines of data. The first line contains the start time at which we begin looking for a bus to take. The second line contains a set of bus numbers (some of which are marked as "x" to be ignored). Each bus number indicates the times at which that bus arrives. For example, a bus number 12 arrives at time 12, 24, 36, and so on. Your goal is to find the bus that appears soonest after the given start time. For example, suppose the file named schedule.txt contains the following text:

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

In the above case you are looking for the bus that will arrive soonest after time 939. This happens to be bus number 59, which arrives at time 944.

Your method should find the best bus and return its number multiplied by the number of minutes the passenger needed to wait. In our example, the passenger boards bus 59 after waiting 5 minutes (from time 939 to 944), so the call of shuttleSearch("schedule.txt") would return 59 * 5 or 295.

You may assume that the file exists and is readable, that it follows the format described above, that there will be at least one line of input in the file, and that the data will contain exactly one "best" bus to take.

(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.