[Community Puzzle] Darts

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

I have a question on this puzzle Coding Games and Programming Challenges to Code Better
Does the dartboard look like this?
lol.jpg - directupload.net

2 Likes

Yes, you are correct. That is what the dartboard looks like.

2 Likes

Hello,
I come here to report a problem with the “darts” puzzle: my code in C# is only passing the first 2 tests / 4, but what a surprise when I submitted: I got 100%!
Here is the pastebin of my code, I hope this is allowed on the forum, since it’s not a very good solution anyway. I don’t understand yet why I don’t pass all the tests, but I’ll find out :slight_smile: I just wanted you to know that my wrong solution is passing all the validators, which is obviously undesired.
I output wrong scores in the tests 3 and 4, and I don’t even care about sorting the names by appearance when their score is equal.
Thanks!

I support this topic. Just solved puzzle. It shows only 3 tests passed, of 4 existing. According to my mind the trouble is about equal results of players and order of names to show. The 4th test didn’t pass because player Will was before player Bill, both had same score. After some corrections test 4 passed, but test 3 failed - result checker expected to see player Joe, and did not like player Brandon before him. So how can author pay attention to puzzle - in final results should be sorted by names too or not?

pinging the author @WilliamMorrison and validators of the contributions: @Uljahn, @Jakque and @JBM
maybe they can help

Can’t check fully I’m on lame Internet here but IIRC the puzzle statement explicitly says how the results should be sorted—and it’s not by name.

4 Likes

Thanks. Just did it.

Hello,

[reference] : Coding Games and Programming Challenges to Code Better

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 ?

Thank you in advance.

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,

Thx for replying.

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

@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