import java.util.*;
import java.io.*;

public class Timetable
{
    public static String SEPARATOR = ",";
    public static String OUT_OF_SERVICE = "x";

    public Timetable (String fileName, boolean debug)
    {
        _earliestDeparture = 0;
        _buses = new Vector<Bus>();
        _debug = debug;

        loadData(fileName);
    }

    public final int earliestDeparture ()
    {
        return _earliestDeparture;
    }

    public final Bus busToCatch ()
    {
        int busDepartureTime = _buses.elementAt(0).nextDeparture();
        Bus bs = null;

        for (int i = 0; i < _buses.size(); i++)
        {
            if (_buses.elementAt(i).nextDeparture() < busDepartureTime)
            {
                busDepartureTime = _buses.elementAt(i).nextDeparture();
                bs = _buses.elementAt(i);
            }
        }

        return bs;
    }

    private void loadData (String inputFile)
    {
        /*
         * Open the data file and read it in.
         */

        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(inputFile));
            String line = null;

            while ((line = reader.readLine()) != null)
            {
                if (_earliestDeparture == 0)
                {
                    _earliestDeparture = Integer.parseInt(line);

                    if (_debug)
                        System.out.println("Earliest departure time: "+_earliestDeparture+"\n");
                }
                else
                {
                    String[] busData = line.split(SEPARATOR);

                    for (int i = 0; i < busData.length; i++)
                    {
                        if (OUT_OF_SERVICE.equals(busData[i]))
                        {
                            if (_debug)
                                System.out.println("Loaded out of service bus.");
                        }
                        else
                        {
                            Bus bs = new Bus(Integer.parseInt(busData[i]), _earliestDeparture);

                            if (_debug)
                                System.out.println("Loaded "+bs);

                            _buses.add(bs);
                        }
                    }
                }
            }
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (Throwable ex)
            {
            }
        }
    }

    private int _earliestDeparture;
    private Vector<Bus> _buses;
    private boolean _debug;
}
@@ -327,32 +327,96 @@
 leName);%0A    %7D%0A%0A
+    // https://en.wikipedia.org/wiki/Chinese_remainder_theorem%0A%0A
     public final
@@ -416,19 +416,20 @@
 c final 
-int
+long
  earlies
@@ -429,25 +429,25 @@
 earliest
-Departure
+Timestamp
  ()%0A    
@@ -460,159 +460,219 @@
     
-return _earliestDeparture
+Bus theBus = _buses.get(_buses.size() - 1)
 ;%0A    
-%7D%0A%0A
     
-public final Bus busToCatch 
+long remainder = theBus.getRemainder
 ()
+;
 %0A    
-%7B%0A
     
-    int busDepartureTime = _buses.elementAt(0).nextDeparture(
+long theMod = theBus.getID();%0A%0A        System.out.println(%22starting with %22+remainder+%22 and %22+theMod
 );%0A
+%0A
         
 Bus 
@@ -671,22 +671,42 @@
     
-Bus bs = null;
+// go down through the other buses
 %0A%0A  
@@ -723,23 +723,16 @@
 (int i =
- 0; i %3C
  _buses.
@@ -737,21 +737,32 @@
 s.size()
-; i++
+ - 2; i %3E=0; i--
 )%0A      
@@ -777,20 +777,25 @@
         
-if (
+theBus = 
 _buses.e
@@ -809,129 +809,194 @@
 t(i)
-.nextDeparture() %3C busDepartureTime)%0A            %7B%0A                busDepartureTime = _buses.elementAt(i).nextDeparture()
+;%0A%0A            long mod = theBus.getID();%0A            long rem = theBus.getRemainder();%0A     %0A            while (remainder %25 mod != rem)%0A            %7B%0A                remainder += theMod
 ;%0A  
@@ -1009,80 +1009,132 @@
     
+%7D%0A
     
-bs = _buses.elementAt(i);%0A            %7D%0A        %7D%0A%0A        return bs
+  %0A            theMod *= mod;%0A        %7D%0A%0A        System.out.println(%22returning %22+remainder);%0A%0A        return remainder
 ;%0A  
@@ -2318,34 +2318,17 @@
 ta%5Bi%5D), 
-_earliestDeparture
+i
 );%0A%0A    

Update Timetable.java