function moves = solver(sequence,target,budget)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Making the worst score for last place kudos!!!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
moves = ones(0,4);
%--------------------------------------------------------------------------
% First I shift and rotate whole sequence to make worst fit to target
%--------------------------------------------------------------------------
errNorm = zeros(1,length(target));
errFlip = zeros(1,length(target));
errNorm(1) = sum(abs(target-sequence));
errFlip(1) = sum(abs(target-fliplr(sequence)));
for i=2:length(sequence)
errNorm(i) = sum(abs( target - [sequence(i:end) sequence(1:i-1)] ));
errFlip(i) = sum(abs( target - fliplr([sequence(i:end) sequence(1:i-1)]) ));
end
errPoint = max(find([errNorm errFlip] == max([errNorm errFlip]))); %finds smallest and if there is an equal minima then the non-flipped version takes precedence
if errPoint > length(sequence)
len = length(sequence) - max(find(errFlip == min(errFlip)))+1;
ai = max(find(errFlip == max(errFlip)));
bi = 1;
flip = 0;
moves(end+1,:) = [len,ai,bi,flip]; %splice in the correct spot
len = length(sequence);
ai = 1;
bi = 1;
flip = 1;
moves(end+1,:) = [len,ai,bi,flip]; %flip the whole lot
sequence = fliplr([sequence(moves(1,2):end) sequence(1:moves(1,2)-1)]);
else
len = length(sequence) - max(find(errNorm == min(errNorm)))+1;
ai = max(find(errNorm == max(errNorm)));
bi = 1;
flip = 0;
moves(end+1,:) = [len,ai,bi,flip]; %ONLY splice in the correct spot
sequence = [sequence(moves(1,2):end) sequence(1:moves(1,2)-1)];
end
|