Puzzle
You are running a factory that produces widgets. The widgets come off the assembly line in random order, and are so stacked. However, in order to be shipped, the widgets must stacked in a specific order.
You have a computer that tracks the order the widgets come off the asssembly line, but cannot afford to automate the process of reordering them. You must therefore rely on an error prone human operator to reorder the stacks.
Given an unordered stack and the required order for the stack, produce a list of sequential instructions so that the human can reorder the stack. To make it easy for the operator, and to minimize the number of errors, the instruction list must:
- Only refer to 3 instructions:
- TOP X
- Place widget X on top of the stack
- BELOW X, Y
- Insert widget Y below widget X
- SWAP X, Y
- Swap widgets X and Y
- Be as short as possible
- Be as easy as possible. That is: if there is more than one instruction list of minimal length, choose the easiest one. Placing widgets on top of the stack is easier than inserting widgets within the stack, which is easier than swapping two widgets (i.e. the instructions were defined in order from easiest to hardest).
End with the no-op instruction DONE.
Example
Here is some code to generate assembly line outputs:
PROPERLY_ORDERED_WIDGETS =: ;: 'RED ORANGE YELLOW GREEN BLUE INDIGO VIOLET'
assembly_line =: ({~ ?~@:#) bind PROPERLY_ORDERED_WIDGETS
] AL =: assembly_line ''
+----+------+------+------+------+---+-----+
|BLUE|INDIGO|VIOLET|ORANGE|YELLOW|RED|GREEN|
+----+------+------+------+------+---+-----+
human_sort AL
1. TOP RED
2. BELOW RED, ORANGE
3. BELOW ORANGE, YELLOW
4. BELOW YELLOW, GREEN
5. DONE.
Exposition
If the widgets are properly ordered thus:
- RED
- ORANGE
- YELLOW
- GREEN
- BLUE
- INDIGO
- VIOLET
and they came off the assembly line thus:
- BLUE
- INDIGO
- VIOLET
- ORANGE
- YELLOW
- RED
- GREEN
then you should produce the instruction list:
TOP RED
BELOW RED , ORANGE_
BELOW ORANGE, YELLOW
BELOW YELLOW, GREEN
DONE.
Which would cause the operator to produce, after each step:
TOP RED
- RED
- BLUE
- INDIGO
- VIOLET
- ORANGE
- YELLOW
- GREEN
BELOW RED, ORANGE
- RED
- ORANGE
- BLUE
- INDIGO
- VIOLET
- YELLOW
- GREEN
BELOW ORANGE, YELLOW
- RED
- ORANGE
- YELLOW
- BLUE
- INDIGO
- VIOLET
- GREEN
BELOW YELLOW, GREEN
- RED
- ORANGE
- YELLOW
- GREEN
- BLUE
- INDIGO
- VIOLET
DONE.
Note that the following instruction list, while effective and equally short, is harder. Therefore it is not to be produced:
TOP RED
SWAP VIOLET , GREEN
SWAP YELLOW , INDIGO
SWAP BLUE , ORANGE
DONE.
Solutions
Post your solutions here.
Comments
You can use @SIG@ to sign your comments. This section is possibly temporary(?).
- This seems like a better sequence for the "human" in the above example:
TOP GREEN
TOP YELLOW
TOP ORANGE
TOP RED
DONE
-- Raul Miller 2005-11-04 04:22:24
- To generate a sequence of TOP instructions sufficient to sort the widgets (any better sequence which contains BELOW or SWAP instructions must be shorter) you might use:
tsort=: (}. #~ [: +./\ 2: </\ ])@\:@i. { ]
PROPERLY_ORDERED_WIDGETS tsort AL
+-----+------+------+---+
|GREEN|YELLOW|ORANGE|RED|
+-----+------+------+---+-- Raul Miller 2005-11-04 04:29:44
