// A propósito: esta versión de trips.c tiene un bug.
//
// Lee de la entrada el nombre de un archivo. Cada línea del archivo es un viaje:
//
//   Chosica 18
#include <stdio.h>
#include <string.h>

#define NAME_SIZE 20
#define MAX_ROUTES 8
#define PATH_SIZE 128
#define LINE_SIZE 64

struct route {
    char name[NAME_SIZE];
    int trips;
    int passengers;
};

int remove_newline(char s[]) {
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] == '\n') {
            s[i] = '\0';
            return 1;
        }
    }
    return 0;
}

// Devuelve la posición de la ruta con ese nombre, o -1 si no está.
int find_route(const struct route routes[], int n, const char name[]) {
    for (int i = 0; i < n; i++) {
        if (strcmp(routes[i].name, name) == 0) {
            return i;
        }
    }
    return -1;
}

// Cuenta un viaje. Si la ruta es nueva, la agrega. Devuelve cuántas rutas hay ahora.
int add_trip(struct route routes[], int n, const char name[], int passengers) {
    int pos = find_route(routes, n, name);
    if (pos == -1) {
        if (n == MAX_ROUTES) {
            fprintf(stderr, "Demasiadas rutas: se omite %s\n", name);
            return n;
        }
        pos = n;
        strcpy(routes[pos].name, name);
        routes[pos].trips = 0;
        routes[pos].passengers = 0;
        n = n + 1;
    }
    routes[pos].trips = routes[pos].trips + 1;
    routes[pos].passengers = passengers;
    return n;
}

// Lee el archivo en routes. Devuelve cuántas rutas hay, o -1 si no pudo abrirlo.
int read_trips(const char path[], struct route routes[]) {
    FILE *file = fopen(path, "r");
    if (file == NULL) {
        perror(path);
        return -1;
    }
    char line[LINE_SIZE];
    int number = 0;
    int n = 0;
    while (fgets(line, LINE_SIZE, file) != NULL) {
        number++;
        char name[NAME_SIZE];
        int passengers;
        char extra[2];
        if (sscanf(line, "%19s %d %1s", name, &passengers, extra) == 2 && passengers >= 0) {
            n = add_trip(routes, n, name, passengers);
        } else {
            fprintf(stderr, "Línea %d: mal formada, se omite\n", number);
        }
    }
    fclose(file);
    return n;
}

double average_passengers(struct route r) {
    return (double) r.passengers / r.trips;
}

// Posición de la ruta con más pasajeros, o -1 si no hay rutas.
int busiest_route(const struct route routes[], int n) {
    if (n <= 0) {
        return -1;
    }
    int best = 0;
    for (int i = 1; i < n; i++) {
        if (routes[i].passengers > routes[best].passengers) {
            best = i;
        }
    }
    return best;
}

void print_routes(const struct route routes[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%-8s %d viajes  %3d pasajeros  %5.2f por viaje\n", routes[i].name, routes[i].trips,
               routes[i].passengers, average_passengers(routes[i]));
    }
}

int main(void) {
    char path[PATH_SIZE];
    if (fgets(path, PATH_SIZE, stdin) == NULL) {
        fprintf(stderr, "Falta el nombre del archivo\n");
        return 1;
    }
    remove_newline(path);
    struct route routes[MAX_ROUTES];
    int n = read_trips(path, routes);
    if (n < 0) {
        return 1;
    }
    if (n == 0) {
        printf("No hay viajes\n");
        return 0;
    }
    print_routes(routes, n);
    printf("Más pasajeros: %s\n", routes[busiest_route(routes, n)].name);
    return 0;
}
