Skip to content

Instantly share code, notes, and snippets.

@tmeers
Last active August 29, 2015 13:55
Show Gist options
  • Save tmeers/8701826 to your computer and use it in GitHub Desktop.
Save tmeers/8701826 to your computer and use it in GitHub Desktop.
Rules for generating Pinewood Derby Race Heats in a "Chaotic-Rotation Method"
Get number of heats based on LaneCount and RacerCount: totalHeats
Add a Race per Den
For each Heat found above:
Select random racers for the lineup based on a "Chaotic-Rotation Method"
Each Racer must race at least 3 times
Each racer must race the same number of times
Racers should be held in a race through as many heats as possible
Racers cannot compete against themselves
Racers should compete against as many different opponents as possible
Given data:
Den (Id, Name)
List of Racers (RacerId, ScoutId, CarNumber, Weight)
Number of Lanes (4 lanes is the easiest)
Data to Generate
-Number of Heats based on Lane and Racer counts
3 lanes is almost 1 lane per racer, unless it's not
4 lanes is 1 heat per racer
6 lanes is the most complex though I'm sure it's simple math (that I don't know)
-Race
id
denId
-Heats
id
raceId
- Contestants
heatId
racerId
laneNumber
@pizzapanther
Copy link

Here is my take on the problem:
https://github.com/pizzapanther/Race-Manager/blob/master/js/ctrl-race.js#L64-L189

I did things a little differently because I made some insights which I think could be good, but I'm not the best algorithm writer so it definitely needs improvement.

First instead of calculating heats I just make sure every racer races the correct amount of times and calculate races from that. I try to rotate them on the track with different opponents randomly. Also I noticed it is easier to calculate races if the number of racers is a multiple of the available tracks. So I fill in dummy racers to make this possible and hide them when displaying races. I also check to make sure a race has at least two real racers.

The downsides of this method is that because it is random it can fail a lot so I have to be lenient on the track requirement. Thus I allow a racer to end up on a track a max of two times.

I think I could get rid of this compromise of two times on a track if my algorithm was better. This reminds me of the 8-queens problem where you have to write an algorithm that is able to go back when a path fails. http://en.wikipedia.org/wiki/Eight_queens_puzzle

I failed that question on an Amazon interview so I guess I should have learned it now :-)

Lastly, I used Mario Kart scoring for each race which gives the 1st place racer the most points. At the end, I total everything and you get a winner.

The app is in the Chrome Store now: https://chrome.google.com/webstore/detail/race-manager/flgdapbfafoghifpocjbmhboaiikncoh

I'll probably create an Android Bundle too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment