Compare commits

..

2 Commits

Author SHA1 Message Date
9752ef451f Cleanup 2023-12-02 23:14:09 -04:00
42cb09f3bd Pass tests without introducing new function 2023-12-02 23:12:22 -04:00
3 changed files with 24 additions and 20 deletions

View File

@@ -75,30 +75,36 @@ void destroyDiskQueue(DiskQueue *queue) {
free(queue);
}
int calcDistance(int position, int destination) {
if (position > destination) {
return destination + (9999 + 1 - position);
} else {
return destination - position;
}
}
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
DiskRequest *current = queue->head;
DiskRequest *closest = NULL;
DiskRequest *next = NULL;
DiskRequest *first = NULL;
while (current != NULL) {
if (current->time <= time) { // Filter out requests that haven't come in yet
if (closest == NULL) { // If there is no closest yet, set it to the current
closest = current;
if (first == NULL) {
first = current;
} else {
int closestDistance = calcDistance(position, closest->position); // Distance from the closest to the current position, negative means down (bad)
int currentDistance = calcDistance(position, current->position); // Distance from the current to the current position, negative means down (bad
if (currentDistance < closestDistance) { // If the current is closer than the closest
closest = current;
if (current->position < first->position) {
first = current;
}
}
int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad)
if (currentDistance > 0) { // If the current is up
if (next == NULL) { // If there is no next yet, set it to the current
next = current;
} else {
int nextDistance = next->position - position; // Distance from the next to the current position, negative means down (bad)
if (currentDistance < nextDistance) { // If the current is closer than the next
next = current;
}
}
}
}
current = current->next;
}
return closest;
}
// If we didn't find a request that is above the current position, return the first as a fallback
if (next == NULL) {
return first;
}
return next;
}

View File

@@ -26,5 +26,3 @@ DiskRequest *dequeue(DiskQueue *queue);
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
void destroyDiskQueue(DiskQueue *queue);
int calcDistance(int position, int destination);

View File

@@ -92,4 +92,4 @@ int main(int argc, char **argv) {
destroyDiskQueue(queue);
return 0;
}
}