[Community Puzzle] Hello, World!

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

2 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!

1 Like

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 :wink:

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

thanks for your quick reply, i will look at my calculations

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! :disappointed_relieved:

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.:+1:

Just finished the puzzle :slight_smile:

  • 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 :cat:

EDIT: A coarse value of 1.5 km passed all validators.

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: