# [Community Puzzle] Hello, World!

I tried something and found your mistake…your distances are due to the fact you inverted longitude and latitude.

4 Likes

Hello,

Here are my impressions. When I started the puzzle, I was quite concerned about the rounding problems, and I thought that this puzzle would require a lot of precision, and debugging would probably be tideous. Then the small uncertainty about the formula (because you did not give it yourself) was frustrating, also because Wikipedia does not explain the N/S and W/E thing. It was really interesting to learn about longitude and latitude, but to my opinion the puzzle should be about coding first.

In the end, I found the errors in my code thanks to your debug output with the coordinates of Epidaurus etc. I was dividing by 360 instead of 3600… I am not sure I would have had enough patience to find it on my own, so I think this example should be part of the puzzle presentation.

Anyway, thank you for this puzzle!

2 Likes

Hi

I just finished this puzzle.
I took way more time to solve it that I’d like, but mainly because I’m a bad reader.
Everything is said on the description of the puzzle, all the mistakes I made were my own.

• Quick reading of the longitude & latitude format
• Bad rouding that I solved the lazy way
• And, most of all, I inverted latitude & longitude in the arcos formula, only because the input from the puzzle is not on the same order than the wiki page.

All those mistakes would have been avoided if I had correctly read the puzzle description.

So… Nice job on this one, TBali 1 Like

hi!
I have a problem with test 2 and 6, I think it is due to the number of bits because I only have 13 to 16 digits after the decimal point.
Or I was wrong in the calculations.
Can we increase the number of bits on php?
Thanks

Hi,

PHP on CG is by default 64bit with integers, and double with floats. So I think it might be a bug in the calculation.

1 Like

excuse me I post my calculation because I did find the errors.

function calculDistance(array \$travel, array \$capital){
return acos( sin(\$travel[‘lat’]) * sin(\$capital[‘lat’]) + cos(\$travel[‘lat’]) * cos(\$capital[‘lat’]) * cos( abs(\$capital[‘long’]-\$travel[‘long’]) ) ) * \$r;
}
if you can help me! I also use PHP. Your formula seems to be correct (assuming \$travel and \$capital has the lat and long already in radian! I use `deg2rad(...)` here.
After or before return you also need to round the result to integer kilometers. I use
`return intval(round(..))` for this.
Also, when filling \$travel and capital, make sure you convert DMS coordinates to degrees correctly, including signs for the N/S and W/E hemispheres.

2 Likes

I understood my error, I understood that there was a good answer by coordinates and not several suddenly I must resume my function of sorting the distances and display.
Thanks

Successful puzzle!
thank you very much i had fun and learned to manipulate coordinates. Just finished the puzzle • used python 3.
• I used the arccos algorithm
• run into the "E and W coordinates have a three-digit degree instead of two problem

Two things to note for future people that try it with python 3:

• rounding (everything equal or above x.5 goes up) worked somewhat, but not for test cases 02 “Border…” and some other test case due to rounding problems
• on that note DON’T trust the standard round() method of python (just execute “round(0.5)” and “round(1.5)” and look what python gives you)
• Ultimately I abandoned rounding of my distances and added a coarse value for the near check e.g. if two distances have a difference smaller or equal to the coarse value then they’re the same
• For my code a coarse value of 2 km passed all test cases but failed Validator 5
• good enough for me!

Hope that helps other people EDIT: A coarse value of 1.5 km passed all validators.

1 Like

Yes, unfortunately the default behaviour of Python `round(x)` is “rounding half to even” which is quite different from what is needed in this puzzle:

``````round(0.5) = 0
round(1.5) = 2
round(2.5) = 2
round(3.5) = 4
``````

With the decimal module you can use another rounding strategy.
https://docs.python.org/3/library/decimal.html#rounding-modes
The first answer in this thread shows an example how to use it properly:

There seems to be a problem with the input in test case 4, which is probably the cause of
much of the confusion:
Epidaurus N373800 E0230800
The longitude is given with 7 digits, not in accordance with the problem description.
Many programs will interpret it as 2° instead of 23°

And one of the latitudes in test case 6 is given as:
`W1753000` (also 7 digits, now with a trailing zero)

It is in accordance with the description.
Latitude has the format “Nddmmss” with 6 digits, and longitude has the format “Edddmmss” with 7 digits.

Just finished the problem myself so I can confirm it works as intended.

1 Like

Oh dear, i feel really stupid now thanks for clearing that up.

could somebody please show me the correct formula of the distance?
because I totally don’t understand why I just cannot calculate the real distance out
for example, in test case’s data below:
Travel point #3: N450915 E0125549
= in DEG: (45.154166666667, 12.930277777778) <–I made this
Epidaurus N373800 E0230800
= in DEG: (37.633333333333, 23.133333333333) <–I also made this

distance = 1191 <— I CANNOT made this!!

how? how to know travel point to this capital’s distance is 1191 km?? I copied some codes from the internet , and they still cannot help me to solve this puzzle.

I had solved ALL easy practice puzzle but the last one: this puzzle… and I know that reason why I cannot solve it because I don’t know how to USE the distance formula, that made me depressed

## #7 N451015 E0125449 Epidaurus distance= 1193 The_Globe_Theatre distance= 1189 Broadway distance= 6730

#8 N512335 W0511458
Epidaurus distance= 5826
The_Globe_Theatre distance= 3471

## #5 N123456 E0043000 Joe_s_Place distance= 18489 Jack_s_Place distance= 18489 Laplace distance= 18489

#6 S123456 E0123456
Joe_s_Place distance= 17964
Jack_s_Place distance= 18587
Laplace distance= 17964

You may have some rounding issues, here’s what I get for test 2 :

``````Location 1
Epidaurus distance: 0
The_Globe_Theatre distance: 2383

Location 2
Epidaurus distance: 2383
The_Globe_Theatre distance: 0

Location 3
Epidaurus distance: 7903
The_Globe_Theatre distance: 5568

Location 4
Epidaurus distance: 1191
The_Globe_Theatre distance: 1191

Location 5
Epidaurus distance: 3952
The_Globe_Theatre distance: 1658

Location 6
Epidaurus distance: 5132
The_Globe_Theatre distance: 2784

Location 7
Epidaurus distance: 1189
The_Globe_Theatre distance: 1194

Location 8
Epidaurus distance: 1194
The_Globe_Theatre distance: 1189

Location 9
Epidaurus distance: 5826
The_Globe_Theatre distance: 3471
3 Likes

Your result is very close. Two possibilities:

• Do you use rounding with the ‘half round up’ method? (Simply casting the float value to integer is like floor(), so not enough here).
• Do you use double precision floats during the computation and cast to integer only as the very last step? (Some languages double is the default or even the only available floating point type, in other languages you need to explicitly declare it.)

Because your result is usually just 1 off from the correct one, your distance formula seems okay, try to check the above two ideas.

2 Likes

it seems the problem , and I try to fix it to carry out test#2, but still cannot pass#6
here is my data updated:
#0
Joe_s_Place distance= 614
Jack_s_Place distance= 612 <— is this correct??
Laplace distance= 614
#1
Joe_s_Place distance= 10007
Jack_s_Place distance= 10007
Laplace distance= 10007
#2
Joe_s_Place distance= 10007
Jack_s_Place distance= 10007
Laplace distance= 10007
#3
Joe_s_Place distance= 10008
Jack_s_Place distance= 10008
Laplace distance= 10008
#4
Joe_s_Place distance= 19402
Jack_s_Place distance= 19402
Laplace distance= 19402
#5
Joe_s_Place distance= 18490
Jack_s_Place distance= 18490
Laplace distance= 18490
#6
Joe_s_Place distance= 17964
Jack_s_Place distance= 18588
Laplace distance= 17964