Non-Stop Travel
Difficulty: Hard

Fast Phil works the late shift and leaves his company's parking lot at precisely 2:00 AM every morning. His route home is by a straight road which has one or more traffic signals. Phil has always wondered if, given the locations and cycles of each of the traffic signals, are there velocities he can travel home without ever having to speed up or slow down on account of a red light. You are to write a program to satisfy his curiosity.  
Your program should find all integer speeds (in miles per hour) which can be used for Phil's trip home. Each speed is a rate (in miles per hour) he can maintain the moment he leaves the parking lot at 2:00 AM until he arrives home (we assume Phil has a long driveway in which to decelerate) such that he never passes through a red signal. He is allowed to pass throgh a signal at the exact moment it turns from yellow to red, or at the exact moment a red signal turns green. Since Phil is a relatively law-abiding citizen, you need only consider speeds less than or equal to 60 mph. Likewise, Phil isn't interested in travelling too slowly, so you should not consider speeds lower than 30 mph.  

**Input:**
Input will consist of one or more sets of data describing a set of traffic signals, followed by the integer -1. The first integer in each set will contain the value N (specifying the number of traffic signals). N will be no larger than 6. This value will be followed by N sets of numbers, each containing values (in order) for L, G, Y and R. L is a positive real number indicating the location of a traffic signal, in miles, from the parking lot. G, Y and R are the lengths of time (in seconds) of the green, yellow, and red periods of the corresponding traffic signal's cycle. Phil has learned from an informant in the Highway Department that all N traffic signals start their green period precisely at 2:00 AM.  

**Output:**
Output should consist of the input case number (starting with 1) followed by a list of all valid integer speeds Phil may drive to avoid the red signals. Consecutive integer speeds should be specified in interval notation of the form L-H, where L and H are the lowest and highest speeds for the interval. Intervals of the form L-L (that is, an interval of length 1) shold just be written as L. Intervals should be separated by commas. If there are no valid speeds, you program should display the phrase No acceptable speeds. The Expected Output below illustrates this format.

**Sample Input:**  
1  
5.5  40 8 25  
  
3  
10.7  10 2 75  
12.5  12 5 57  
17.93 15 4 67  

-1		

**Expected Output:**  
Case 1: 30, 32-33, 36-38, 41-45, 48-54, 59-60  
Case 2: No acceptable speeds.  


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Go):
    #include <stdio.h>
    #include <math.h>

    typedef struct {
    double dist;
    int g, y, r;   /* actually, all you need is g + y, they are equivalent */
    } Light;


    int LightIsGreenishYellow(Light *light, double time) {
        /* mod out any complete cycles
           for example, if the timings for green, yellow and red
           are 1, 2, 3, then a cycle is 6 seconds long.  If the
           time is 20 seconds, then 20=(6*3)+2, so it is 2 seconds
           into the cycle */
    time = fmod(time, light->g + light->y + light->r);

        /* if we are in the green or yellow parts of the cycle,
           we're all good */
    return time <= (light->g + light->y);
    }


    /* check if a given speed will work for the given pattern
       of traffic lights */
    int MustGoFaster_MustGoFaster(Light lights[6], int n_lights, int speed) {
    double time;
    int light_no;

    for (light_no = 0; light_no < n_lights; light_no++) {
            /* compute the time at which Fast Eddie, I mean,
               Fast Phil, will hit this light */
        time = lights[light_no].dist / speed * 3600.0;

            /* check what part of its cycle this light will be
               at at that time */
        if ( ! LightIsGreenishYellow(lights+light_no, time)) return 0;
    }
    return 1;
    }



    int main() {
    int c=1, n, i, speed;
    Light lights[6];
    int speeds[62];     /* a flag for each valid speed, with [61]==0 */
    int any;            /* were there any speeds that worked? */
    FILE *inf = fopen("prob_4.in", "r");
    double f;           /* need to declare this just to the floating
                           point formats get linked */

    while (fscanf(inf, "%d", &n), n != -1) {
            /* read in the light definitions */
        for (i=0; i<n; i++) {
            fscanf(inf, "%lf %d %d %d", &f, &lights[i].g,
                  &lights[i].y, &lights[i].r);
            lights[i].dist = f;
        }
        any = 0;
        speeds[61] = 0;         /* put a terminator at the end so
                               the exit condition is easier */

                            /* try each possible speed */
        for (speed=30; speed <= 60; speed++) {
            speeds[speed] = MustGoFaster_MustGoFaster(lights, n, speed);
            any = any || speeds[speed];
        }

        printf("Case %d: ", c++);

        if (!any) {
            printf("No acceptable speeds.\n");
        } else {
            int span_start, span_end, first = 1;
            speed = 30;

                /* find all the spans of valid speeds */
            while (speed <= 60) {
                while (!speeds[speed] && speed <= 60) speed++;
                if (speed > 60) break;
                span_start = speed;
                while (speeds[speed]) span_end = speed++;

                if (first) {
                    first = 0;
                } else {
                    printf(", ");
                }

                if (span_start == span_end) {
                    printf("%d", span_start);
                } else {
                    printf("%d-%d", span_start, span_end);
                }
            }
            putchar('\n');
        }
    }

    return 0;
    }