2 ms·
Descriptively, scan through the slots in order, stacking reds up on the left, and blues up on the right, leaving greens in the middle. You only need to remembe
by white-flame 11y ago
Descriptively, scan through the slots in order, stacking reds up on the left, and blues up on the right, leaving greens in the middle. You only need to remember how many reds & blues you've stacked up.
Programmatically:
LastRed = -1, LastBlue = numSlots
- If the observed ball is red, swap with ++LastRed. Move the observing arm forward, as you've already observed the one swapped in.
- If the observed ball is blue, swap with --LastBlue.
- If the observed ball is green, leave it there. It might get swapped with a red later as LastRed grows. Move the observing arm forward.
If the observing arm is at LastBlue, we're done.
Loop.
- to3m 11y agoThanks! I managed to work it out in the end, after the other comment said it was possible :)