Skip to content

Commit 307d0a8

Browse files
Defibrillators
1 parent f6fb1fe commit 307d0a8

File tree

6 files changed

+301
-49
lines changed

6 files changed

+301
-49
lines changed

puzzles/js/defibrillators.js

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Defibrillators
2+
3+
## Problem Description
4+
5+
The city of Montpellier has equipped its streets with defibrillators, and the data corresponding to their positions is available. The task is to write a program that helps users find the nearest defibrillator to their current location.
6+
7+
### Inputs
8+
9+
1. **User Location:**
10+
- Line 1: User's longitude (in degrees, comma-separated).
11+
- Line 2: User's latitude (in degrees, comma-separated).
12+
- Line 3: The number `N` of defibrillators in the city.
13+
14+
2. **Defibrillators Data:**
15+
Each of the next `N` lines contains the following fields separated by semicolons (`;`):
16+
- A number identifying the defibrillator.
17+
- Name of the defibrillator.
18+
- Address.
19+
- Contact Phone number.
20+
- Longitude (degrees, comma-separated).
21+
- Latitude (degrees, comma-separated).
22+
23+
### Output
24+
25+
The program should display the **name** of the defibrillator that is **closest** to the user’s current position.
26+
27+
### Distance Formula
28+
29+
The distance `d` between the user and a defibrillator is calculated as:
30+
31+
```
32+
x = (longitudeB - longitudeA) * cos((latitudeA + latitudeB) / 2)
33+
y = latitudeB - latitudeA
34+
distance = hypot(x, y) * 6371
35+
```
36+
37+
### Constraints
38+
39+
- 0 < `N` < 10,000
40+
41+
### Example
42+
43+
#### Input
44+
```
45+
3,879483
46+
43,608177
47+
3
48+
1;Maison de la Prevention Sante;6 rue Maguelone 340000 Montpellier;;3,87952263361082;43,6071285339217
49+
2;Hotel de Ville;1 place Georges Freche 34267 Montpellier;;3,89652239197876;43,5987299452849
50+
3;Zoo de Lunaret;50 avenue Agropolis 34090 Mtp;;3,87388031141133;43,6395872778854
51+
```
52+
53+
#### Output
54+
```
55+
Maison de la Prevention Sante
56+
```
57+
58+
## Code Example
59+
60+
```javascript
61+
function toRadians(degrees) {
62+
return degrees * Math.PI / 180;
63+
}
64+
65+
// Parse the user's longitude and latitude
66+
const LON = parseFloat(readline().replace(',', '.'));
67+
const LAT = parseFloat(readline().replace(',', '.'));
68+
69+
// Number of defibrillators
70+
const N = parseInt(readline());
71+
72+
let closestDefibName = '';
73+
let closestDistance = Infinity;
74+
75+
// Loop over each defibrillator to calculate the distance
76+
for (let i = 0; i < N; i++) {
77+
const DEFIB = readline().split(';');
78+
const defibLon = parseFloat(DEFIB[4].replace(',', '.'));
79+
const defibLat = parseFloat(DEFIB[5].replace(',', '.'));
80+
81+
// Calculate the distance using the provided formula
82+
const x = (defibLon - LON) * Math.cos(toRadians((LAT + defibLat) / 2));
83+
const y = defibLat - LAT;
84+
const distance = Math.hypot(x, y) * 6371; // Earth's radius in kilometers
85+
86+
// Check if this defibrillator is closer than the current closest
87+
if (distance < closestDistance) {
88+
closestDistance = distance;
89+
closestDefibName = DEFIB[1];
90+
}
91+
}
92+
93+
// Output the name of the closest defibrillator
94+
console.log(closestDefibName);
95+
96+
```

puzzles/js/defibrillators/defib.js

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

puzzles/python3/defibrillators/README.md

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

33
"Defibrillators" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given the location of a person and the location of several defibrillators in a city, and is asked to determine the closest defibrillator to the person.
44

5-
The input contains the location of the person as longitude and latitude, as well as the longitude and latitude of each defibrillator and its name. The player is asked to calculate the distance between the person and each defibrillator using the Haversine formula, which takes into account the curvature of the Earth's surface.
6-
7-
The challenge consists of writing a program that takes as input the location of the person and the locations of the defibrillators, and outputs the name of the closest defibrillator to the person.
8-
9-
The challenge is designed to help players learn and practice programming skills such as geographic coordinate manipulation, distance calculation, and data structures. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle.
10-
115
## Example Code
126

137
```python

0 commit comments

Comments
 (0)