I have a number of slots and pegs arranged in a straight line. The pegs can be moved and need to be moved to a slot each. A slot can be left empty only if all pegs are taken. When a peg is moved, it is not allowed to go past another peg. In other words the order of the pegs must be maintained. Preferably, the total distance moved by all pegs should be kept at a minimum. As far as possible, a peg should be placed in the nearest available slot.
All I want to know is: What field of mathematics deals with such a problem? What are the names of any well known algorithms which deal with similar problems? I am looking for Google fodder. Some keywords.
+--oooo-+--+---+---o--+------+--+-ooo+o-+-------o--+-----o-o-+-o
+ - Slots
o - Pegs
EDIT: I think that this visualization makes more sense. They are two separate tracks that need to line up.
Slots: +-------+--+---+------+------+--+----+--+----------+---------+--
Pegs: ---oooo------------o--------------ooo-o---------o--------o-o---o
EDIT: Just want to make it clear that the number of slots can be greater than, less than or equal to the number of pegs.