Puzzles/ErrorProneSort

From J Wiki
Jump to navigation Jump to search

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:

1. 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 1. Be as short as possible 1. 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). 1. 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:

1. RED 1. ORANGE 1. YELLOW 1. GREEN 1. BLUE 1. INDIGO 1. VIOLET

and they came off the assembly line thus:

1. BLUE 1. INDIGO 1. VIOLET 1. ORANGE 1. YELLOW 1. RED 1. GREEN

then you should produce the instruction list:

1. TOP __RED__ 1. BELOW __RED__ , __ORANGE___ 1. BELOW __ORANGE__, __YELLOW__ 1. BELOW __YELLOW__, __GREEN__ 1. DONE.

Which would cause the operator to produce, after each step:

1. TOP __RED__ 1. RED 1. BLUE 1. INDIGO 1. VIOLET 1. ORANGE 1. YELLOW 1. GREEN 1. BELOW __RED__, __ORANGE__ 1. RED 1. ORANGE 1. BLUE 1. INDIGO 1. VIOLET 1. YELLOW 1. GREEN 1. BELOW __ORANGE__, __YELLOW__ 1. RED 1. ORANGE 1. YELLOW 1. BLUE 1. INDIGO 1. VIOLET 1. GREEN 1. BELOW __YELLOW__, __GREEN__ 1. RED 1. ORANGE 1. YELLOW 1. GREEN 1. BLUE 1. INDIGO 1. VIOLET 1. DONE.

Note that the following instruction list, while effective and equally short, is harder. Therefore it is not to be produced:

1. TOP __RED__ 1. SWAP __VIOLET__ , __GREEN__ 1. SWAP __YELLOW__ , __INDIGO__ 1. SWAP __BLUE__ , __ORANGE__ 1. 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:

1. TOP GREEN 1. TOP YELLOW 1. TOP ORANGE 1. TOP RED 1. DONE

-- Raul Miller <<DateTime(2005-11-04T04:22:24Z)>>

  • 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 <<DateTime(2005-11-04T04:29:44Z)>>