Skip to content

Commit

Permalink
Defibrillators
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesfranciscodev committed Oct 21, 2024
1 parent f6fb1fe commit 5be0013
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
| ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art) ★, [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) | Strings |
| Unary 1️⃣ | [Python](./puzzles/python3/unary), [TypeScript](./puzzles/ts/unary) ★, [Haskell](./puzzles/haskell/unary), [C#](./puzzles/cs/unary) | Strings, Encoding |
| MIME Type 🎵 | [Python](./puzzles/python3/mime-type) ★, [Kotlin](./puzzles/kotlin/src/mime-type), [TypeScript](./puzzles/ts/mime-type), [C#](./puzzles/cs/mime-type) | Strings, Hash Tables |
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) ★, [Kotlin](./puzzles/kotlin/src/defibrillators), [TypeScript](./puzzles/ts/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) ★, [Kotlin](./puzzles/kotlin/src/defibrillators), [JavaScript](./puzzles/js/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
| Racing Duals 🏁 | [Python](./puzzles/python3/horse-racing-duals) ★, [Kotlin](./puzzles/kotlin/src/horse-racing-duals), [JavaScript](./puzzles/js/horse-racing-duals), [Ruby](./puzzles/ruby/horse-racing-duals) | Arrays, Sorting |

### Medium Puzzles
Expand Down
36 changes: 0 additions & 36 deletions puzzles/js/defibrillators.js

This file was deleted.

96 changes: 96 additions & 0 deletions puzzles/js/defibrillators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Defibrillators

## Problem Description

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.

### Inputs

1. **User Location:**
- Line 1: User's longitude (in degrees, comma-separated).
- Line 2: User's latitude (in degrees, comma-separated).
- Line 3: The number `N` of defibrillators in the city.

2. **Defibrillators Data:**
Each of the next `N` lines contains the following fields separated by semicolons (`;`):
- A number identifying the defibrillator.
- Name of the defibrillator.
- Address.
- Contact Phone number.
- Longitude (degrees, comma-separated).
- Latitude (degrees, comma-separated).

### Output

The program should display the **name** of the defibrillator that is **closest** to the user’s current position.

### Distance Formula

The distance `d` between the user and a defibrillator is calculated as:

```
x = (longitudeB - longitudeA) * cos((latitudeA + latitudeB) / 2)
y = latitudeB - latitudeA
distance = hypot(x, y) * 6371
```

### Constraints

- 0 < `N` < 10,000

### Example

#### Input
```
3,879483
43,608177
3
1;Maison de la Prevention Sante;6 rue Maguelone 340000 Montpellier;;3,87952263361082;43,6071285339217
2;Hotel de Ville;1 place Georges Freche 34267 Montpellier;;3,89652239197876;43,5987299452849
3;Zoo de Lunaret;50 avenue Agropolis 34090 Mtp;;3,87388031141133;43,6395872778854
```

#### Output
```
Maison de la Prevention Sante
```

## Code Example

```javascript
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);

```
34 changes: 34 additions & 0 deletions puzzles/js/defibrillators/defib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
6 changes: 0 additions & 6 deletions puzzles/python3/defibrillators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

"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.

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.

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.

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.

## Example Code

```python
Expand Down
Loading

0 comments on commit 5be0013

Please sign in to comment.