7
1
The twelve-balls problem is a famous problem where you are given twelve balls, one of which is a different weight, but you don't know whether it is heavier or lighter than the other balls. Using only three weighings of a two-sided scale, it is possible to find the differently weighted ball, and determine whether it is heavier or lighter.
Your task is to build a program that does the following:
Accept a number of balls
N
to be compared, from8
to100
. (In the traditional problem,N = 12
.)Produce two lists of numbers, representing the balls to be placed on each side of the balance. An equal number of balls must be placed on each side.
Accept an input signifying whether the left side of the scale is heavier, the right side is heavier, or the two sides are equal (this can be represented any way you want to: e.g.
-1
for left,0
for equal,1
for right, or1
for left,2
for right, and3
for equal), and in response, either produce another pair of lists of balls to be weighed, or a guess as to which ball is the different one and whether it is heavier or lighter.
Your score is the sum of the maximum number of weighings for each value of N
from 8
to 100
that it took to figure out the right answer for N
balls using your algorithm (each case of "ball x
is heavier/lighter" must be tested). Lowest score wins.
For example, if for N = 12
, your algorithm managed to get 3 weighings for every case except "ball 8 is heavy" where it took 4 weighings, your score for N = 12
is 4. If your maximum score was 10 weighings for each N
from 8 to 100, your final score would be 930
.
Your algorithm must return the correct answer for all possible test cases (for any N
, there are 2N
possible test cases, which is a total of 10,044). In addition, the source code of your solution may not exceed 51,200 bytes (50 KB) in size.
To me it seems like the n=12 problem requires at least 4 weighings – Cruncher – 2013-11-20T21:35:10.873
Actually, it seems much harder than that... I can determine IF it's heavier or lighter in 3, but not the actual ball.. The quickest I can think of for finding the ball would be 5. In general it looks like
3 + log2(n/3)
to be – Cruncher – 2013-11-20T21:40:58.123Probably even harder for n not divisible by 3 – Cruncher – 2013-11-20T21:46:17.277
1
Nope. It's possible for 3 weighings: http://www.primepuzzle.com/leeslatest/12_ball_solution.html
– Joe Z. – 2013-11-20T22:15:40.613Also, I think I know what your basic method is - and determining whether the ball is heavier or lighter without finding out which ball it is actually only takes two weighings (if it's divisible by 3). So it's
2 + log3(n/3)
, not3 + log2(n/3)
. – Joe Z. – 2013-11-21T01:34:13.267Where did you get
log3
from? Is that a typo? You're right about deciding heavy/light in 2 though forn%3==0
– Cruncher – 2013-11-21T13:36:02.020Remember that if two sides are equal, you know that the balls that weren't weighed contain the different ball. So you can split the balls up into three groups instead of just two. – Joe Z. – 2013-11-21T21:18:24.970
Ah, I see, I see. Essentially the same trick we used for finding lighter/heavier. I would suspect using this algorithm, whilst having a 4th "remainder" group for non-divisible by 3 N, would actually fare pretty well – Cruncher – 2013-11-21T21:25:45.537
How do you test to see how many weightings it takes? – Justin – 2013-11-21T22:16:42.103
You can probably build an automated client to do it. For each value of
N
and each value of1 <= M <= N
, assumeball M is heavy
and thenball M is light
and test the input of your program accordingly. – Joe Z. – 2013-11-21T22:24:12.673