Skip to content

Factory stack balancer

This simulation problem is about translating a goal into safe, repeatable actions. A robot moves along a row of stacks and can carry at most one box.

At each call, the program receives:

  • clawPos: the index of the stack under the robot, starting at 0.
  • boxes: the number of boxes in each stack.
  • boxInClaw: whether the robot is carrying one box.

Return exactly one command: 'LEFT', 'RIGHT', 'PICK', or 'PLACE'. The robot moves one position for a move command. It may pick only when it is not carrying a box and the current stack is nonempty. It may place only when carrying. For this lesson, null means the task is complete. The caller invokes the function again with the updated state after each command.

The target is an even distribution. If there are leftover boxes, give one extra box to each of the leftmost stacks. For example, [5, 0, 0] has five boxes across three stacks, so the targets are [2, 2, 1].

function nextCommand(clawPos, boxes, boxInClaw) {
if (boxes.length === 0) return null;
// Count the carried box too, so targets stay fixed between commands.
const total = boxes.reduce((sum, count) => sum + count, 0) + (boxInClaw ? 1 : 0);
const base = Math.floor(total / boxes.length);
const remainder = total % boxes.length;
const targets = boxes.map((_, index) => base + (index < remainder ? 1 : 0));
if (boxInClaw) {
// Deliver the carried box to the first stack still below its target.
const destination = boxes.findIndex((count, index) => count < targets[index]);
if (destination === -1) return null; // inconsistent state: nothing needs this box
if (clawPos < destination) return 'RIGHT';
if (clawPos > destination) return 'LEFT';
return 'PLACE';
}
// Pick from a stack that has more than its target.
const source = boxes.findIndex((count, index) => count > targets[index]);
if (source === -1) return null;
if (clawPos < source) return 'RIGHT';
if (clawPos > source) return 'LEFT';
return 'PICK';
}

The function always chooses a source with a surplus before choosing a destination with a deficit. Picking reduces one surplus; placing fills one deficit. Moving closer to the selected stack makes progress, and the function recalculates its plan from the updated state on the next call.

There are five boxes and three stacks, so base is 1, remainder is 2, and the targets are [2, 2, 1].

  1. At stack 0, not carrying: return 'PICK' because stack 0 has a surplus.
  2. After the pick, the state is [4, 0, 0], carrying: return 'RIGHT' to move toward stack 1.
  3. At stack 1, carrying: return 'PLACE', giving [4, 1, 0].
  4. Repeated calls pick from stack 0 and fill the stacks below target until the result is [2, 2, 1]. Then the function returns null.

The problem guarantees nonnegative integer counts, a valid clawPos, and an accurate boxInClaw flag. It also guarantees the robot starts at one of the stack positions and can move between neighboring stacks. A production simulator should validate those conditions and define what to do with an inconsistent state, a blocked path, or a failed command.

This strategy is easy to reason about, but it does not promise the shortest possible route. A useful follow-up is to count commands for different starting positions and compare alternative source/destination choices.