local i,j,p,reel,reelLens,scrap
dimension reel(1)
reelLens = "985-1,580-2,450-3,345-4,275-5,265-6,230-7,190-8,150-9"
* NOTE: reelLens ::= length-reelNumber
scrap = 0
clear
*----- Load reels -----------
i = 1
p = at(",",reelLens)
if p = 0 then
p = len(reelLens)+1
endif
reel(i) = substr(reelLens,1,p-1)
do while p < len(reelLens)
reelLens = substr(reelLens,p+1)
p = at(",",reelLens)
if p = 0 then
p = len(reelLens)+1
endif
i = i+1
dimension reel(i)
reel(i) = substr(reelLens,1,p-1)
enddo
local totLen
totLen = 0
for i=1 to alen(reel)
totLen = totLen+val(reel)
next i
*----- Braid reels -----------
local mindiff,imin,jmin,smaller
do while .T.
if alen(reel) >= 3 then
mindiff = 99999999999
* sort descending (largest on top)
asort(reel,1,alen(reel),1)
* find 2 reel with minimum difference
* (exclude the largest)
for i=2 to alen(reel)
for j=i+1 to alen(reel)
if abs(val(reel(i))-val(reel(j))) < mindiff then
mindiff = abs(val(reel(i))-val(reel(j)))
imin = i
jmin = j
smaller = min(val(reel(i)),val(reel(j)))
endif
next i
next i
scrap = scrap+abs(val(reel(imin))-val(reel(jmin)))
?"scrap"+str(abs(val(reel(imin))-val(reel(jmin))));
+" total:"+str(scrap)
? "using "+substr(reel(imin),5);
+" "+substr(reel(jmin),5)
if imin > jmin then
aDelete(@reel,imin)
aDelete(@reel,jmin)
else
aDelete(@reel,jmin)
aDelete(@reel,imin)
endif
for i=1 to alen(reel) && find one >= 100 + smaller
if not inlist(i,imin,jmin) then
if val(reel(i)) >= smaller+100 then
reel(i)=alltrim(str(val(reel(i))-smaller));
+substr(reel(i),4) && use part of it
?" and "+str(i)
exit &&for
endif
endif
next i
if i > alen(reel) && none found >= 100 + smaller
for i=1 to alen(reel) && find one >= smaller
if val(reel(i)) >= smaller then
reel(i)=alltrim(str(val(reel(i))-smaller));
+substr(reel(i),4) && use part of it
?" and "+str(i)
scrap = scrap+val(reel(i)) && scap rest < 100'
?"scrap "+str(val(reel(i)));
+" total:"+str(scrap)
aDelete(@reel,i)
exit &&for
endif
next
endif
if i > alen(reel) && none found >= smaller
for i = 1 to alen(reel) && scrapp rest of reels
scrap = scrap+val(reel(1))
?"scrap "+str(val(reel(1)));
+" total:"+str(scrap)
aDelete(@reel,1)
next i
exit &&do
endif
else && scrap rest of reels
for i=1 to alen(reel)
scrap = scrap+val(reel(1))
?"scrap "+str(val(reel(1)))+" total:"+str(scrap)
aDelete(@reel,1)
next i
exit &&do
endif
enddo
*------ Results-----------
?
?"Wasted "+str(scrap)+" out of a total of "+str(totLen);
+" = "+str(100*scrap/totLen,1)+"%"
*==================================
PROCEDURE aDelete
lparameters arr,i, elem
local typ,j,u2
external array arr
elem = arr(i)
if pcount() = 3 then
elem = arr(i)
endif
if i > alen(arr,1) then
RETURN
endif
typ = type("arr")
=adel(arr,i)
u2 = alen(arr,2)
if u2 > 0 then
if alen(arr,1) > 1 then
dimension arr(alen(arr,1)-1,u2)
else
for j=1 to u2
do case
case typ = "C"
arr(1,j) = ""
case typ = "N"
arr(1,j) = 0
case typ = "D"
arr(1,j) = {//}
endcase
next j
endif
else
if alen(arr) > 1 then
dimension arr(alen(arr)-1)
else
do case
case typ = "C"
arr(1) = ""
case typ = "N"
arr(1) = 0
case typ = "D"
arr(1) = {//}
endcase
endif
endif
RETURN elem
*---------------