[Community Puzzle] Hello, World!

Thanks for the feedback. Yes, 3 or 2 digits of longitude degree can make quite a difference.
I updated the puzzle description with some more specific hints on formula usage, coordinate conversions, 64-bit floats.

Hi! In python3 I use:
sign = 1 if a[0] in ['N', 'E'] else -1
for latitude and longtitude. But cases#2 and #6 failed.
Also I’ve used formula from wiki and got distances and answers in 2nd case:
[‘N373800’, ‘E0230800’] [‘N373800’, ‘E0230800’] 0.0
[‘N373800’, ‘E0230800’] [‘N513025’, ‘W0000542’] 2986.695524262495
[‘N373800’, ‘E0230800’] [‘N404532’, ‘W0735906’] 10801.48909842209
Chairete!
[‘N513025’, ‘W0000542’] [‘N373800’, ‘E0230800’] 2986.695524262495
[‘N513025’, ‘W0000542’] [‘N513025’, ‘W0000542’] 0.0
[‘N513025’, ‘W0000542’] [‘N404532’, ‘W0735906’] 8248.265905476199
Haile to thee, Noble Master!
[‘N404532’, ‘W0735906’] [‘N373800’, ‘E0230800’] 10801.48909842209
[‘N404532’, ‘W0735906’] [‘N513025’, ‘W0000542’] 8248.265905476199
[‘N404532’, ‘W0735906’] [‘N404532’, ‘W0735906’] 0.0
Hello, Dolly!
[‘N450915’, ‘E0125549’] [‘N373800’, ‘E0230800’] 1384.6340794800697
[‘N450915’, ‘E0125549’] [‘N513025’, ‘W0000542’] 1608.8048156436273
[‘N450915’, ‘E0125549’] [‘N404532’, ‘W0735906’] 9669.583300888533
Chairete!
[‘N505540’, ‘W0235856’] [‘N373800’, ‘E0230800’] 5432.102120089192
[‘N505540’, ‘W0235856’] [‘N513025’, ‘W0000542’] 2656.872234692642
[‘N505540’, ‘W0235856’] [‘N404532’, ‘W0735906’] 5592.914273079193
Haile to thee, Noble Master!
[‘N522335’, ‘W0411458’] [‘N373800’, ‘E0230800’] 7319.282253075043
[‘N522335’, ‘W0411458’] [‘N513025’, ‘W0000542’] 4577.035810425796
[‘N522335’, ‘W0411458’] [‘N404532’, ‘W0735906’] 3689.931510056004
Hello, Dolly!
[‘N450815’, ‘E0125649’] [‘N373800’, ‘E0230800’] 1382.0882714191703
[‘N450815’, ‘E0125649’] [‘N513025’, ‘W0000542’] 1611.2665451244145
[‘N450815’, ‘E0125649’] [‘N404532’, ‘W0735906’] 9671.397965775572
Chairete!
[‘N451015’, ‘E0125449’] [‘N373800’, ‘E0230800’] 1387.1800778182628
[‘N451015’, ‘E0125449’] [‘N513025’, ‘W0000542’] 1606.3434727884764
[‘N451015’, ‘E0125449’] [‘N404532’, ‘W0735906’] 9667.768787025907
Chairete!
[‘N512335’, ‘W0511458’] [‘N373800’, ‘E0230800’] 8380.007524859508
[‘N512335’, ‘W0511458’] [‘N513025’, ‘W0000542’] 5688.124812698316
[‘N512335’, ‘W0511458’] [‘N404532’, ‘W0735906’] 2576.530104476686

Where is my mistake?

You should (re)read all hints of statement.

1 Like

For distance formula, see https://en.wikipedia.org/wiki/Great-circle_distance +
Use the 1st (arccos) formula from the above wikipedia page.
yes:
fi1, fi2 = torad(a[1]), torad(b[1]) #to radians
t = math.sin(fi1)*math.sin(fi2)
p = math.cos(fi1)*math.cos(fi2)
q = math.cos(abs(torad(a[0]) - torad(b[0])))
r = 6371*math.acos(t + p*q)

Use 64-bit precision float to avoid errors due to loss of precision.
used that:
math.isclose(dists[j], min(dists), abs_tol=0.00001):
Don’t forget to convert degrees, arcminutes, arcseconds to decimal degrees.
yes:
decdeg = deg + (mn + sec/60)/60
Don’t forget to handle North/South and East/West as the sign of decimal degrees.
yes:
sign = 1 if a[0] in ['N', 'E'] else -1
Don’t forget to convert decimal degrees to radians (distance formula is in radian!)
yes
math.radians(decdeg*sign)

And for separating degrees, minutes and seconds:
deg = int(a[1:4]) if a[0] in ['E', 'W'] else int(a[1:3])
mn = int(a[-4:-2])
sec = int(a[-2:])

Don’t mind…

1 Like

Hi,

Unfortunately I don’t know Python so I don’t find what is the problem in the code you wrote. Checking my solution I noticed that all your calculated distances are wrong.

Maybe this could help: Here is the debug output from IDE TEST CASE 02, endpoint #3:
(Note: as per puzzle statement I rounded the distance to integer, so here it is already printed as integer. Below I print all coordinates in DMS, in decimal degrees and radian degrees)

Travel point #3: N450915 E0125549
= in DEG: (45.154166666667, 12.930277777778)
= in RAD: (0.78808887932761, 0.22567592041968)

distance = 1191 to Epidaurus:
N373800 E0230800
= in DEG: (37.633333333333, 23.133333333333)
= in RAD: (0.6568255751672, 0.40375283362802)

distance = 1191 to The_Globe_Theatre:
N513025 W0000542
= in DEG: (51.506944444444, -0.095)
= in RAD: (0.89896576819736, -0.0016580627893946)

distance = 6733 to Broadway:
N404532 W0735906
= in DEG: (40.758888888889, -73.985)
= in RAD: (0.71137681056564, -1.291281847088)

Result: closest capitals indexes = 0, 1; distance = 1191

4 Likes

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

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