Horse Racing Duals puzzle discussion

Hi,

Why do you use hashset ? It’s slower to iterate through it.Try with an arrayList.
First put all the data into list. Sort your list before doing the diff to find the minimum :slight_smile:

tn i got it… i thought sth else that was so different from problem

Exactement le même problème ici, et je n’en comprends pas la cause.

Hello everyone,
I’m trying this challenge in bash (for the achievement of course :stuck_out_tongue: ) and it works fine when in editor (even on the large number of horses), but when I submit it doesn’t pass all tests.
I’m assuming it’s not timeout/performance related since the non-passing tests are

Shorter difference on the first 2 horses (100 pts)
Shorter difference on the last 2 horses (100 pts)
Only 2 horses (100 pts)

And if the last one is what it says (only two horses) then performance shouldn’t be a problem. I have tried building my own testcases with things that might look like those three failed tests, but it seems to work fine (in editor). Any idea ?

[EDIT] : Oh well… It seems like it did matter anyway. I decided to work on optimization for a bit before I got this issue fixed, and got my program to run waaaaay faster (could’nt really measure, but really faster), and now it does pass all tests both in the editor and when submiting. Weird.

Les tests pour l’évaluation ne sont pas exactement les mêmes que pour les validations dans l’IDE. Mêmes principes, mais pas mêmes valeurs.

J’ai déjà tenté de feinter un test avec un brutal «si les conditions entrées X et Y alors la réponse est Z», pour passer les tests de l’IDE. Ça foire au test externe. Je pense que c’est justement pour ce genre de cas que les valeurs ne sont pas les mêmes :wink:

After I long trial and error finally managed to solve the problem in Bash. I’m a Bash novice and was very confused about the fact that the same solution for other languages didn’t work here (talking about the case with large number of horses)

As others have stated in this forum the bottleneck is really in accessing elements in the array, especially when the index is quite large. The hint is to try optimizing array access by reusing a value from previous iteration.

3 Likes

At first I tried using brute force (compare each value by each value), but it breaks for the third test (too long).
Then, it does work well sorting and then compare each with next ( i and i+1 ).

I used Javascript, third test past in 39ms.

Hi there!

I’m stuck at the “Bash Rider” Challenge. Everything works fine, but as I submit it, the “Horses in disorder”-Test fails. Does anyone know what exactly is tested there or even provide a test case for me?

I tried some cases with unsorted numbers (i.e. 5 4 7 9) and I get the expected results from my script…

I really would appreciate any help on this!

Thanks man,
I have follow your advice and I have 100% on bash
with

  • read variables
  • sort

Hello, friends

I wrote my code in JavaScript and I also passed tests except the last but one.
Can someone tell me how to check whats going on

why do i need to solve in bash to get 100%? it pass all the tests in C#.

Because there is an extra challenge here.

In Bash version in my case helped to get rid of arithmetic in array index. So instead of having

$((arr[i+1] - arr[i]))

I preloaded first value into variable and then in each cycle get the n-th element of the array compare them and then I assigned the newer value to the variable with older value, thus eliminating the addition.

Hope this helps somebody :smiley:

3 Likes

Of course, if you use linux and do small scripts, it’s quite easy. I’ve never done anything like this, but with a combination of pipes and unix commands like “sort”, you don’t even have to use arrays or anything complex.

But sort is forbidden here.

But that’s dumb. I’ve made tens of scripts in bash and anytime I need something for which there is already a command, I use this command (sort, head, tail, sed…).

I’ve already implemented sort functions in many languages. I don’t need to implement in a bash.

I’ve also used the sort function in python in all the puzzles that required it. I think that it is important to know what functions are built-in and use them whenever possible.

Sort (as well as other Linux commands) are not forbidden and I actually used it in my solution. Just output and pipe into a variable

1 Like

Damn it, I tried it with a pipe and something was wrong.
I’ll try it again.

Hello guys,
I’m using Java to solve this puzzle. I have a problem in the optimization. here’s my source code: https://github.com/Sphinx01/CodinGame/blob/master/horse_racing_duals.java
Can you please suggest a more optimized method(s) to do the optimization?

for me, your solution looks optimal as mine is the same (different language)
except in java i would use method http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#min(int,%20int)