# [darts]

Hello,

[reference] : https://www.codingame.com/ide/puzzle/darts

I pass all tests but not the third validator “Chaotic Ties”.

This is my algo in java :
(because of Polygon object in java doesn’t include boundaries, we have to make it manually)

i compute radius with width/2, ofc.
if at least one coordinates are not in [-radius;radius], it’s 0 point.

i check if x and y are positivie, so i can check if we are in the diamond square.
For example, you have a straight between coordinates [-radius;0] to [0;-radius]. you can parametrize your straight with y + x = -radius. So i check if y+x >= radius.
I do it for the 4 combinaisons of -radius <= x || y <= radius.
If one test is ok, it’s +15 points.

If not, i check if it’s in the circle with the classic sqrt(x²+y²) <= radius

After i sort by values, with a stream.

Anyone had this problem ? Or maybe anyone has a test data that can represent this validator ?

Your logic looks like tangling together. Reasonable to assume your code is no better organized than this. That can conceal lots of bugs.

Suggest you split the “point-in-shape” logic from the scoring logic.
Write 3 methods to test a point, is it

• in diamond?
• in circle?
• in square?
According to the 3 boolean results, calculate the score.

Also DO NOT use sqrt() in this case (and in many other similar cases in other puzzles).
Use square (a*a) to replace the inaccurate and slow sqrt().

Not sure is the above helpful in any way?

1 Like

Hello,

At least it help for the tip to test x²+y² <= radius² instead of using Math.sqrt.

However, i will try to make the 3 static methods but i’m not really sure it would help. Because it would be a c/c of code with just a return.
But, never said never, i’ll try and reply later. thx

Hi,

It doesn’t change anything.
It’s really frustrating to not have all errors cases taken into account in units tests because we can’t even debug what’s wrong with our tests in “submission mode”.

If later someone find this topic and found a solution, i would be glad to know it, even if it’s several years later

@Pox First of all, all the computations can be done using integers, avoid `sqrt` and divisions and make sure to reduce everything to exact integer comparisons (to avoid floating point inaccuracies). You should also make sure that you output the names in the required order.
If this does not solve the problem, you can send me your code in PM, I’ll have a look at it.

1 Like

So here the news.
Thanks to the comrade @Niako, i founded the tip.

Here was the problem and the solution (in java)
I used a Map<String,Integer> to represent Names and the score.

The compute score algo was correct. The incorrect thing was with the sort.
I had an assumption that when i try a
“sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))”, it sort by reverse score (it’s asked) but the names order displayed still remain when there’s a tie.
It’s a complete mistake, maps doesn’t preserve order of adding of keys when there’s a tie during the comparingByValue.

So i tried to manage “people” as an object with name/score and then use Comparator and thenComparing method to add another criteria when there’s a tie.

Hope it will help someone someday.

1 Like

A good debug story to share. Also let you know that in java you can use TreeMap which supports sorting. To have custom sorting, e.g on value, add a Comparator to define how you want to sort.

I’m not sure if there’s a bug in the first test case or not. According to the instructions, “Darts landing on the edge of a shape are considered within the shape”: thus, if a square has side length X, a dart landing at (X, X) should be within the square. I’m puzzled, then, why on the first test case Will doesn’t get 5 points for hitting the corner of the target.

“The center of the square, circle and diamond is at (0,0)” as said in the last line of statement.

Ah. It’s the total width of the square. Duh.

Somebody pass the coffee… facedesk