Step 1: 9 Balls
A, B, C = (3, 3, 3)
If A = B Goto 2A //Target in C
If A != B Goto 2B //Target in A or B
Step 2A (3 Balls)
D, E, F = (1, 1, 1 | C)
If D = E Then Result = F
If D != E Goto Step 3A //Target is D or E
Step 2B (6 Balls)
G, H, I = (2, 2, 2 : (A,B))
If G = H Goto 3B //Target in I
If G != H Goto 3C //Target in G or H
Step 3A
If D = F Then Result = E
Else Result = D
Step 3B (2 Balls)
If I(1) = Any other ball not in I Then Result = I(2)
Else Result = I(1)
Step 3C (4 Balls)
If G = I Goto 4A (Ball in H)
If G != I Goto 4B (Ball in G)
Step 4A
If H(1) = Any other ball not in I Then Result = H(2)
Else Result = H(1)
Step 4B
If G(1) = Any other ball not in I Then Result = I(2)
Else Result = I(1)