BigLug has asked for the wisdom of the Perl Monks concerning the following question:
I figure it's time to get onto this so that I'm ready for christmas already.
I have an group of people who must purchase presents for each other. However, they've been doing this for a couple of years and they don't want to get the same person again and again. So I have a data structure. A hash of lists where the hash keys are the people and the lists are the people they've bought for in the past:
%people = ( jim => ['betty', 'claire'], betty => ['frank', 'jim' ], frank => ['claire', 'john' ], claire => ['john', 'betty' ], john => ['jim', 'frank' ], nancy => ['', '' ], # new girl );
This means that jim wont get betty or claire this year, betty wont get frank or jim and so on.
So last year I took this data structure and ran 'trial and error' computing on it. I started with jim, grabbed a list of the keys, removed jim, betty and claire, then picked a random result. No worries, lets say he gets nancy. Takes about a nanosecond. However now nancy isn't available anymore so betty gets one less option! We work down the list, always getting a shorter pick list.
However extremely often we run out of people to pick from. Imagine if frank gets jim, betty gets nancy and jim gets bob. Claire then can only get frank.
So what I currently do is put all this in a loop only stopping once we've made it all the way down the line and everyone is matched. But next year they'll each have three names in their lists. This can take a minute or more!
There has to be a better way to do this. I've looked around and thought it through and I can't work it out. Has anyone come accross a similar problem before? If so, please enlighten me!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Kris Kringle / Secret Santa
by Abigail-II (Bishop) on Jan 09, 2004 at 11:36 UTC | |
|
Re: Kris Kringle / Secret Santa
by Abigail-II (Bishop) on Jan 09, 2004 at 10:57 UTC | |
|
Re: Kris Kringle / Secret Santa
by inman (Curate) on Jan 09, 2004 at 12:49 UTC | |
|
Re: Kris Kringle / Secret Santa
by tall_man (Parson) on Jan 10, 2004 at 01:19 UTC | |
by BigLug (Chaplain) on Jan 10, 2004 at 07:22 UTC | |
by tall_man (Parson) on Jan 10, 2004 at 23:32 UTC |