my girlfriend told me the following riddle:
there was a king with a beautiful daughter who he wanted to marry off to someone worthy. so he said that anyone who could solve the riddle could marry his daughter. anyone who tried but failed would be beheaded. the riddle was about 12 pearls. one was real and the others were fake. the only difference between them was that the real one was a grain diffent in weight (you don't know if it's heavier or lighter). given a balance and three attempts, figure out which one is the real pearl and tell whether it is heavier or lighter.
after beating my head against a wall for a few hours i came up with the following solution:
step 1: weigh 1-4 against 5-8. if they weigh the same, we know the real one's in 9-12 and proceed to 2a otherwise proceed to step 2b. step 2a : weigh 9-11 against 1-3. if they're the same, we know it's 12. weigh 12 against any of the others to determine if it's lighter or heavier then you're done. if 9-11 was heavier than 1-3, you know that the real one is heavy. weigh 9 against 10. if they're equal 11 is the real one. otherwise, the heavier of 9 and 10 is the real one. step 2b: weigh 1,2,5,6 against 9,10,3,7. (assume 1,2,3,4 was heavier than 5,6,7,8 in step 1. the technique is symmetric; i'll leave it as an exercise to the reader to work out how to do it if it was lighter) if 1,2,3,6 is heavier than we know that either 1 or 2 is heavy or 7 was light. if 1,2,5,6 was lighter, we know that 5 or 6 was light or 3 was heavy. if they were the same, we know that 4 was heavy or 8 was light (test either one against a known fake and you're done). in the case of (1,2) heavy or 7 light, weigh 1,7 against X,X (X is any known fake). if 1,7 is heavier, 1 is the heavy pearl; if lighter, 7 is the light pearl; if the same, 2 is the heavy pearl. same idea if (5,6) light or 3 heavy: just test 5,3 against X,X |
which is really complicated and i don't think explains it that clearly. to both make sure i'd figured it out and to explain it more fully, i wrote some perl code:
|
that's functional, demonstrates that it works, and is relatively easy to read but was not written for brevity.
so the golf is to recode the find_pearl sub. as the comments say, it gets passed a list of 12 weights and must return the index of the one that is different and whether it is lighter or heavier. you have access to the weigh() subroutine which takes references to two arrays and tells you which was heavier. weigh() is the only way that you're allowed to examine the weights themselves. any other testing of the contents of the list by any means is grounds for disqualification. naturally, you can't exceed the 3 test limit either.
solutions don't have to run under strict (10 stroke bonus if it does though).
the weights aren't restricted to the 0,1,2 that i use in the example code. this shouldn't matter though since you can only access them through weigh().
i'm thinking that with the symmetric nature of the algorithm, a good strategy may be generating code on the fly and eval()ing it. good luck.
update: by popular request, added <readmore> tags.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (GOLF) pearls and perl
by tachyon (Chancellor) on Apr 04, 2002 at 13:17 UTC | |
|
Re: (GOLF) pearls and perl
by particle (Vicar) on Apr 04, 2002 at 14:10 UTC | |
by thraxil (Prior) on Apr 04, 2002 at 15:39 UTC | |
by particle (Vicar) on Apr 13, 2002 at 15:00 UTC | |
|
Re: (GOLF) pearls and perl
by jwest (Friar) on Apr 04, 2002 at 16:04 UTC | |
by thraxil (Prior) on Apr 04, 2002 at 16:57 UTC | |
|
Interesting, but please edit
by RMGir (Prior) on Apr 05, 2002 at 16:40 UTC |