Defibrillators puzzle discussion

you have to resolve the puzzle using the standard input, not the live database.

I am… Your reply confirms that the standard input of the puzzle is not the live DB but a static clone.

Make sure to convert the longitudes and latitudes from degrees to radians. Also when doing your check to see if the distance is smaller than the current smallest distance make sure you are not checking if the smallest distance is equal to 0 when you have no smallest distances stored. You can do so by making the smallest distance -1 before any smallest distance is found and checking for that instead.

1 Like

I had to write my own parser since i’m using C. However it was surprisingly easy, and even more surprisingly fun! I encourage all of you to try it!

Having trouble with this one. Is it a live DB? It seems to fail on a calculation that if made using Google maps help seems to be wrong.

3.874054
43.606779
0: 109
1: Caisse d’assurance retraite et de la Sante au travail
2: 29 cours Gambetta 34000 MONTPELLIER
3: 04 67 12 94 72
4: 3,87064343057042
5: 43,6068847626242
6: 0.00015351261620476814
0: 107
1: Caisse Primaire d’Assurance Maladie
2: 29 cours Gambetta 34000 MONTPELLIER
3: 04 99 52 54 49
4: 3,87110915929521
5: 43,6065196099402
6: 0.00036326676951972675
0: 110
1: Caisse d’assurance retraite et de la Sante au travail
2: Century 2 , 101 place pierre Duhem le millenaire 34000 MONTPELLIER
3: 04 67 12 94 72
4: 3,91465549573187
5: 43,6068978500869
6: 0.0005391628970360712
0: 108
1: Caisse Primaire d’Assurance Maladie
2: 90 allee Almicare Calvetti 34000 Montpellier
3: 04 99 52 54 49
4: 3,82126953167633
5: 43,6322018829039
6: 0.035422805723830414
Caisse d’assurance retraite et de la Sante au travail

Top two values are the initial lat/lon for the third test. the application is complaining that that it expects “Caisse P” and getting “Caisse d”. The one I’m returning is 109. I think it wants 107, but when I plug those values into Google maps, Caisse P is 200m while Caisse d is 190m. Am I missing something here?

I compared my formula to this in forum, it seems be correct. But I get wrong results. Can someone tell me, what’s wrong?

static double getDistance(double loA, double laA, double loB, double laB)
{
    double x = (loB - loA) * Math.Cos((laA + laB) / 2.0);
    double y = laB - laA;
    
    return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) * 6371;
}

The result is:
Dist:278749528586.599 Addr:Maison de la Prevention Sante
Dist:278746142778.745 Addr:Hotel de Ville
Dist:278762364357.738 Addr:Zoo de Lunaret
Closest Addr: Hotel de Ville

edit:
Ok, got it. Didn’t noticed that the user’s longitude and latitude include commas, too.

2 Likes

Regarding my solution in python3:

My code fails to pass the “single possibility” test unless I hard-code the output being the first item’s address. Unfortunately I cannot debug the “hidden” tests, so I do not know where the error occurs. Even if I simulate a single correct input, it still passes any of the regular test cases.

This is my code with what I expect a “single possibility” test would look like:

lon = 3.879483
lat = 43.608177
defib = []
defib = defib.append('1;Maison de la Prevention Sante;6 rue Maguelone 340000 Montpellier;;3,87952263361082;43,6071285339217'.replace(',','.').split(';'))
'''
this means defib = [['1', 'Maison de la Prevention Sante', '6 rue Maguelone 340000 Montpellier', '', '3.87952263361082', '43.6071285339217']] and len(defib) = 1
'''
min = math.pi*6371
output = ''

for location in defib:
    x = (lon - float(location[4]))*math.cos((lat+float(location[5]))/2)
    y = lat - float(location[5])
    d = math.sqrt(x*x+y*y)*6371*(math.pi/180)
    if d < min:
        min = d
        output = location[1]

print(output)

This generates the output “Maison de la Prevention Sante” with min before being 20015.086796020572 and min after being 0.11662777224653707.

Yet, I fail at “single possibility” unless i hardcode the initial to output = defib[0][1]. Any clues? Maybe there is something with lists that I’m missing or the test case is a little buggy?

1 Like

Can someone tell me why am I getting this?
Answer.swift:59:14: error: cannot convert value of type 'String' to expected argument type 'Double'

Here’s line 59:
degToRad(Lat, b: b, c: Lon, d: d)

degToRad function declaration:
func degToRad (a: Double, b: Double, c: Double, d: Double) -> Double { //a is user lat //b is defib lat //c is user lon //d is defib lon .... }

b and d variable declaration:
var b: Double = Double(list[line][list[line].count - 1])! var d: Double = Double(list[line][list[line].count - 2])!

list is a bidimensional array with ‘sanitized’ values from DEFIB default variable, where each [line] is a defib.

Also, if any admins reading this, pls update swift to v3, or at the very least specify wich version is being used

I think there is a problem with the 3rd test case, my php script didn’t passed it but got 100% when submitting.
Here is the error message when testing 3rd case :

Failure
Found: CR
Expected: Ca

But my script calculate those values :

CRR = 0.40642026791872
Caisse MSA Languedoc = 0.69070446831732

It seems that other people are having the same problem, maybe you should check that case ? Got me stucked for an hour but when submitting I got 100%.

Did you convert the angles in radians?

Would I get 100% when submitting if I didn’t ?

If you do not make the conversion your distances mean nothing and they can’t be sorted correctly because function cos and sinus are not linear.
If cos and sin were linear your distances would be false but with a fixed error and their order would be correct.

The change can be made in 2 seconds so try it and you’ll tell us if you succeed, we can not say for sure that you would get 100% as we do not know what your code is.

Friends, I spent a few hours until I was able to solve 100%. Be carefull! Error of the majority is that the “$LON and $LAT” also contain a number of coma. Respectively before you perform calculations necessary to convert them to numbers with a point.
Standard Output Stream:
’$LON = 3,874054’ must be '$LON = 3.874054’
’$LAT = 43,606779’ must be '$LAT = 43.606779’

1 Like

First time i calculate the value of y using latitudeA and latitudeB in radians and i solved two cases. When i calculate value of y in degrees then i solved all cases. I think that in description must be pointed that only for x latitude must be in radians, but who knows, maybe it’s only my mistake… Sorry my bad english…)

‘strsep’ in this C implementation seems to be buggy, returning pointer values shifted by FFFF800000000000 upward. Had to compensate it by subtracting the deviation from all the ‘strsep’ return values.

I guess that you forgot to #include <string.h>

Nope, string.h is included.

Then, since strsep() is not standard C, you have to #define _BSD_SOURCE at the beginning, before the include. Alternatively, you can use strtok() instead.

Yes, that was it! Thank you!

[Removed the full code, please just show the useful part]

Can anybody have a look at this code please? I’ve been sitting here for hours and tests 2 and 3 keep failing.