Horse Racing Duals puzzle discussion

right, if you want challenging test go to expert mode :wink:

Any bash rider here? :stuck_out_tongue: hell more difficult to finish this puzzle within those condition

If you dunno what i’m talking about, check the achievements :wink:

Doesn’t seem that hard and I’m far from being a bash pro. However, I wasn’t able to have a solution which passes both “Horses in disorder” and “Numerous Horses” and ended up using a faster but not completly reliable method when N is large. Basically if there were a test with a large N and tied horses, I’d fail. So if anyone could explain me the right way, I’ll be glad to learn some bash tricks.

Edit: finally managed to get a legit 100%. The way you read input and the moment you sort can make a huge difference.

Hi,

I’ve finally found a solution, in Bash, fast enough.

The principle was described in the Niskaz’s post:

It’s quite easy to implement. But you have to take care about how you perform arthitmetic expressions: it is better to use $(( Pi - Pj )) than expr Pi - Pj.
This little detail allowed me to pass the test with a huge number of horses.

I hope this will help !

3 Likes

Or try a Mars Lander II from medium, be it better left unmentioned


Ton idĂ©e a marchĂ© dans mon cas pourtant. Peut ĂȘtre que le tri n’était pas si rapide que ça :slight_smile:

Salut,

en Bash, pour lire l’input, NE FAITES PAS read line (effacez le du code gĂ©nĂ©rĂ©) et faites cat /dev/stdin Ă  la place. C’est bourrin mais ça vous fera gagner le trophĂ©e Bash.
Bonne chance.

4 Likes

je dois dire que j’ai reussi avec le code par dĂ©faut, donc pas la peine de trickser :slight_smile:

I did it with the default code, no need to trick :wink:

1 Like

My current method is failing at case 3.

My method is to append all the input to a list, sort the list, and compare all pairs. The first 2 cases work fine. But at the third, my output is 1, but 4 is supposedly correct. Any ideas? I am using python2

1 Like

Bonjour Ă  tous,

Quelqu’un saurait comment trier un tableau en bash
 je galùre un peu mais je sens que je suis proche du but


franchement, je savais pas non plus mais une petite recherche sur google et tu sauras, stackoverflow par exemple a de bons morceau de code pour t’apprendre à trier un tableau.

Merci de la réponse,

Je vais regarder plus attentivement
 j’avais dĂ©jĂ  pas mal recherchĂ© avant mais je n’arrive pas Ă  utiliser comme tableau les donnĂ©es triĂ©es


Hello
When i run the tests it all goes fine, but when i submit i got 2 errors (on Simple Case and Numerous
). Anyone encountered this?

Salut, pour le Bash j’ai utilisĂ© un tableau mais cela n’a pas fonctionnĂ© sur le test des grandes donnĂ©es car cela prend trop de temps. Aussi j’ai abordĂ© le problĂšme en utilisant la commande ‘awk’ qui m’a permis de passer tous les tests.
Si ton code fonctionne avec un tableau je suis curieux de voir comment tu as codé.
Merci

1 Like

Hi guys,

Please tell me how to pass “Horses in desorder”. I am stuck

Thanks,

I’m having the same problem. There might be a submission error.

I don’t know what language you’re using, but I used a JavaScript array.sort() function before comparing strengths. When comparing, have a couple variables like “smallestDiff” and “currentDiff” in the loop. If the current difference is smaller than smallest, set smallest equal to current.
That might be a little confusing, but I hope it helps.

1 Like

I am using C++. Thanks for your help!

You sir deserve a cookie.

It took me hours to try to optimise different parts of that code, but I didn’t realise using expr makes such a difference. Thanks for the tip.

1 Like

I tried the bash rider achievement without luck for a time. Checking elements two by two in the sorted array was too slow for me (strangely, it gets slower and slower as I go into higher indexes).

I finally just did a one-liner with sed / sort / awk. :slight_smile:
It could be considered cheating as there is not a line of actual bash, on the other hand, for me this is the mentality of bash / command-line: use whatever tool you have at your disposal to get the job done.

See for instance: http://franklinchen.com/blog/2011/12/08/revisiting-knuth-and-mcilroys-word-count-programs/

1 Like