Skip to content

Commit 0fef012

Browse files
committed
pgm 22.13 - bug fix to handle requested departure time between last and first departures of the day
1 parent f76d0e1 commit 0fef012

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

data/flights.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
0:20 2:30
12
4:35 6:15
23
8:00 10:16
34
9:43 11:52
@@ -17,4 +18,4 @@
1718
21:00 23:10
1819
21:45 23:58
1920
22:10 0:15
20-
23:30 1:20
21+
23:20 1:20

lib/find-departure.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#define LINE_SIZE (TIME_STR_SIZE * 2 + 2)
44

5-
struct file_info {
5+
typedef struct file_info {
66
size_t size;
77
int data[][2];
8-
};
8+
} FileInfo;
99
static bool is_file_valid(char *f)
1010
{
1111
struct stat fs;
@@ -18,14 +18,14 @@ static bool is_file_valid(char *f)
1818
return true;
1919
}
2020

21-
static struct file_info *load_data(char *filename)
21+
static FileInfo *load_data(char *filename)
2222
{
2323
FILE *fp;
2424
char line[LINE_SIZE];
2525
char d[TIME_STR_SIZE], a[TIME_STR_SIZE];
2626
int m;
2727
bool read_error = false, data_error = false;
28-
struct file_info *fi = NULL; /* NULL so first realloc is just like malloc() */
28+
FileInfo *fi = NULL; /* NULL so first realloc is just like malloc() */
2929
size_t l;
3030

3131
if (!is_file_valid(filename))
@@ -37,7 +37,7 @@ static struct file_info *load_data(char *filename)
3737
}
3838
for (l = 0;fgets(line, sizeof(line), fp) != NULL; l++)
3939
{
40-
if (!(fi = realloc(fi, sizeof(struct file_info) + (sizeof(int (*)[2])) * (l + 1)))) {
40+
if (!(fi = realloc(fi, sizeof(FileInfo) + (sizeof(int (*)[2])) * (l + 1)))) {
4141
fprintf(stderr, "realloc: %s:%d\n", __FILE__, __LINE__);
4242
exit(EXIT_FAILURE);
4343
}
@@ -72,11 +72,27 @@ static struct file_info *load_data(char *filename)
7272
}
7373
return fi;
7474
}
75-
void find_closest_flight(int requested_departure, int *departure_time, int *arrival_time)
75+
static void overnight_search(FileInfo *flights, int requested_departure, int *departure, int *arrival)
76+
{
77+
78+
/* adjust first flight in the morning to be
79+
* a number 'larger than midnight' to
80+
* simplify the comparison.
81+
*/
82+
int morning_departure = flights->data[0][0] + (24*60);
83+
int night_deaparture = flights->data[flights->size - 1][0];
84+
if (requested_departure < flights->data[0][0])
85+
requested_departure += 24*60;
86+
87+
*departure = flights->data[0][0];
88+
*arrival = flights->data[0][1];
89+
if (abs(requested_departure - night_deaparture) < abs(requested_departure - morning_departure)) {
90+
*departure = night_deaparture;
91+
*arrival = flights->data[flights->size - 1][1];
92+
}
93+
}
94+
static void single_day_search(FileInfo *flights, int requested_departure, int *departure, int *arrival)
7695
{
77-
struct file_info *flights = load_data(FILE_PATH);
78-
int diff;
79-
size_t f;
8096
/* start as max possible diff */
8197
/*
8298
* for each entry in departures list:
@@ -88,14 +104,23 @@ void find_closest_flight(int requested_departure, int *departure_time, int *arri
88104
* store the difference as 'mininum_difference' for use in next comparison
89105
* store the array index as 'closest' for use in accessing the flight info
90106
*/
91-
int minimum_difference = 23*60+59;
107+
int diff, minimum_difference = 23*60+59;
108+
size_t f;
92109
for (f = 0; f < flights->size; f++) {
93110
diff = abs(requested_departure - flights->data[f][0]);
94111
if (diff < minimum_difference) {
95112
minimum_difference = diff;
96-
*departure_time = flights->data[f][0];
97-
*arrival_time = flights->data[f][1];
113+
*departure = flights->data[f][0];
114+
*arrival = flights->data[f][1];
98115
}
99116
}
117+
}
118+
void find_closest_flight(int requested_departure, int *departure_time, int *arrival_time)
119+
{
120+
FileInfo *flights = load_data(FILE_PATH);
121+
if (requested_departure < flights->data[0][0] || requested_departure > flights->data[flights->size -1][0])
122+
overnight_search(flights, requested_departure, departure_time, arrival_time);
123+
else
124+
single_day_search(flights, requested_departure, departure_time, arrival_time);
100125
free(flights);
101126
}

0 commit comments

Comments
 (0)