I am creating a script to optimize sequences of nominal chemistries by minimizing croppage (measured in feet). The "chemistries" are referred to by codes(ex: 1234AB, 5665SG) that contain four numbers and two letters.

What I want to do is look at all possible combinations of N codes for the length of the given sequence, choosing each code once, and minimizing the resulting croppage inbetween codes. Essentially, if i have N codes in a given sequence, there will be N-1 croppages (croppage in between each pair of codes, but not at the front or the back of the sequence). My example data set is for an 8 code sequence. I want to know the ordering (out of all 8! combinations of codes) that will give me the minimum amount of total croppage.

My current script pulls the applicable codes for the current sequence from a lookup table that contains all possible combinations (pairs) of codes in existence, and their respective croppage amounts. The resulting data table is attached for an example sequence with 8 codes.

Basically I want to minimize the sum of all the "(Sum_Crops)" that would exist in between each code for each permutation of the 8 codes.

"Code_1 (Sum_Crop_1_2) Code_2 (Sum_Crop_2_3) Code_3 (Sum_Crop_3_4) Code_4 (Sum_Crop_4_5) Code_5 (Sum_Crop_5_6) Code_6 (Sum_Crop_6_7) Code_7 (Sum_Crop_7_8) Code_8"

Any ideas on how to proceed?