Defibrillators puzzle discussion

Doing the problem in JavaScript

I just had a hell of a time with this problem and my issue ended up being simple: I had been using parseFloat() when reading in the readline() for LON and LAT. Since the coordinates have commas, it would truncate the input to 3 and 43. This caused me such a headache, since I kept on double checking my math over and over… ugh. So yeah, don’t do what I did. Also, had a few issues with running an input through parseFloat() and Number(), but they ended up still being strings, so when I went to use + to sum, it actually concatenated. So if using javaScript look out for that…

Thxs for this one

Lo mismo me pasa a mí solo que con el 1 por más que traté de simplificar el código o meter más validaciones me sigue dando 75%

[In English, please (google traduction below):
The same thing happens to me, only with 1, no matter how much I tried to simplify the code or put more validations, it still gives me 75%]

Just as Salman said!

The proposed correct algorithm to this Defribillators Puzzle is NOT correct.

It is accepting Maison de la Prevention as the location of the nearest defribillator whereas Hotel de Ville distance (4867207623.741239566) is shorter than that of Maison de la Prevention (4867208230.410655004).

What “proposed correct algorithm” ?

I have the same result for the Complete file test.
*$g++ -o main .cpp
$main
Caisse Primaire d’Assurance Maladie<-- distance is–>5.10351
CRR<-- distance is -->0.40642

I wrote a separate code to test these two.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <regex>
using namespace std;

struct Point{
     long double longitude, latitude;
 };

long  double toRadians(const long  double degree) 
{ 
    // cmath library in C++  
    // defines the constant 
    // M_PI as the value of 
    // pi accurate to 1e-30 
    long  double one_deg = (M_PI) / 180; 
    return (one_deg * degree); 
}

Point GetPoint(long double lon, long double lat)
{
   Point point;
   point.longitude = toRadians(lon);
   point.latitude = toRadians(lat);
   
   return point;
} 

 
long double GetDistance(Point A, Point B)
{
    long double distance = 0.0;
    long double x = 0.0;
    long double y = 0.0;

    x = (B.longitude - A.longitude) * cos((A.latitude + B.latitude)/2);
    y = B.latitude -A.latitude;

    return distance = std::sqrt( pow(x, 2) +pow(y, 2)) *6371;
}


int main()
{
    Point playerpos = GetPoint(3.874054, 43.606779);

    Point defifPos = GetPoint(3.82126953167633, 43.6322018829039);
    long double d = GetDistance(playerpos, defifPos);
    cout<<"Caisse Primaire d'Assurance Maladie" << "<-- distance is-->" << d<<endl;
    
    Point defifPos1 = GetPoint(3.87409666178277,43.610433894746);
    long double d1 = GetDistance(playerpos, defifPos1);
    cout<<"CRR" << "<-- distance is -->" << d1<<endl;
    
    return 0;
}

Hi everyone !

I’m trying to resolve the puzzle but when I’m trying the 2, 3 and 4 solution, the DEFIB input looks incomplete…

Here is the input I receive from DEFIB : string DEFIB; getline(cin, DEFIB) and I print it with cerr:

18;Poste de police Hotel de ville;789 chemin de moulares;;3,89399056177745;43,5988579879724

20;Palais des sports Pierre-de-Coubertin;Avenue de

ARA;263 Rue du Caducee 34000 MONTPELLIER;;3,83221040954412;43,6433843496942

155;Communaute d Agglomeration de Montpellier;50 place Zeus;;3,8906414844389;43,6076132052153

156;D.D.S.I.S. HERAULT;2 Rue Duval-Jouve 34000 MONTPELLIER;;3,86542459122745;43,610311327355

I tried to resolve my bug for several hours and I’m kind of lost now ahah

Thank you,

1 Like

The size of the error console is limited that’s why it shows as incomplete but you should receive the input completely

I supposed that was the error but if I tried simply printing the i of the for loop and everything gets displayed

This is what I needed. Thanks, what a blunder on my end. You saved me a lot of time.

Hello, I am using C# and stuck on this trying to store the DFIB inputs as variable so that they can be split / manipulated to isolate the LON / LAT value of each input sequence. I am new to this so it is likely that I am going about this the wrong way but I’m not sure. Any suggestions would be appreciated!

The issue I’m having with the below code is that this stores each input as an index in the list. However, now if I try something like defibsAllData[i].Split(";"); for example, I get an error for trying to convert string [] to string.

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

class Solution
{
static void Main(string[] args)
{
double LON = Double.Parse(Console.ReadLine().Replace(",","."));
double LAT = Double.Parse(Console.ReadLine().Replace(",","."));
int N = int.Parse(Console.ReadLine());
var defibsAllData = new List();

    for (int i = 0; i < N; i++)
    {
        defibsAllData.Add(Console.ReadLine().Replace(",","."));
    }

What you’re showing should work, from what I gather you probably wrote something like
string s = defibsAllData[i].split(";")
which would not be allowed since Split returns an array of strings (aka string[] here).

1 Like

Hi, I’m actually working on Defibrillators puzzle
I’ve having issues to get the good distance, I rand my code again and again and compare it to the formulas but I’m not able to see what’s wrong with what I’ve done…

Maybe someone will see ? (I whould be so gratefull :))
My code in c# below :

Code deleted by a moderator

Are you converting from degrees to radians?

After my C code passed all tests in the IDE, I submitted my code and the validator says I failed to pass the first test because it suspects that I have “hard coded” the answer but I didn’t. The validator is invalid.

It’s a generic message to say that your code failed, that doesn’t mean you hardcoded anything.
As for the validity, who’s more likely to be wrong, the 60k people that passed it or you ?

1 Like

Because it thinks my answer is not 100% correct, it’s not letting me publish my answer and I can’t view the answers of others. So here it is.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

long double getCoord(char *s){
    char *dot = strchr(s, ',');
    if (dot) *dot = '.';
    char *end;
    return strtold(s, &end);
}

int main()
{
    char LON[51];
    scanf("%s", LON);
    char LAT[51];
    scanf("%s", LAT);
    long double lonA = getCoord(LON);
    long double latA = getCoord(LAT);
    int N;
    char answer[100];
    long double distance = 6371.0;
    scanf("%d", &N); fgetc(stdin);
    for (int i = 0; i < N; i++) {
        char DEFIB[257];
        scanf("%[^\n]", DEFIB); fgetc(stdin);
        char * c = strrchr(DEFIB, ';');
        long double latB = getCoord(c+1);
        *c = ' ';
        c = strrchr(DEFIB, ';');
        long double lonB = getCoord(c+1);
        long double x = (lonB - lonA) * cosl((latA + latB)/2);
        long double y = latB - latA;
        long double d = sqrtl(x*x + y*y) * 6371;
        if (d < distance){
            distance = d;
            char * a = strchr(DEFIB, ';');
            *(strchr(++a, ';')) = 0;
            strcpy(answer, a);
        }
    }

    printf("%s\n", answer);

    return 0;
}

Before attempting this problem in C I had successfully completed it in C++.

Now I got 100% validation. Thank you so much :).

Hi. isn’t there any hint in the game? i’m just starting