-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdefib.js
34 lines (27 loc) · 1.08 KB
/
defib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function toRadians(degrees) {
return degrees * Math.PI / 180;
}
// Parse the user's longitude and latitude
const LON = parseFloat(readline().replace(',', '.'));
const LAT = parseFloat(readline().replace(',', '.'));
// Number of defibrillators
const N = parseInt(readline());
let closestDefibName = '';
let closestDistance = Infinity;
// Loop over each defibrillator to calculate the distance
for (let i = 0; i < N; i++) {
const DEFIB = readline().split(';');
const defibLon = parseFloat(DEFIB[4].replace(',', '.'));
const defibLat = parseFloat(DEFIB[5].replace(',', '.'));
// Calculate the distance using the provided formula
const x = (defibLon - LON) * Math.cos(toRadians((LAT + defibLat) / 2));
const y = defibLat - LAT;
const distance = Math.hypot(x, y) * 6371; // Earth's radius in kilometers
// Check if this defibrillator is closer than the current closest
if (distance < closestDistance) {
closestDistance = distance;
closestDefibName = DEFIB[1];
}
}
// Output the name of the closest defibrillator
console.log(closestDefibName);