Finish 2010-11-17 12:00:00 UTC

Not that advanced

by Christian Ylämäki

Status: Passed
Results: 280687 (cyc: 17, node: 1252)
CPU Time: 2.052
Score: 56145.7
Submitted at: 2010-11-11 02:08:38 UTC
Scored at: 2010-11-11 02:11:32 UTC

Current Rank: 2467th (Highest: 14th )
Basis for: Not that advanced (bugfix) (diff)

Comments
Please login or create a profile.
Code
function [bestRow, bestCol] = solver(chart, aIndex, bIndex, maxThrottle)

[aRow, aCol] = ind2sub(size(chart(:,:,1)),aIndex);
[bRow, bCol] = ind2sub(size(chart(:,:,1)),bIndex);

[bestRow, bestCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 1);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 1);
bestRow = [bestRow, thrustRow2];
bestCol = [bestCol, thrustCol2];
bestScore = runsolution(bestRow, bestCol, chart, aRow, aCol, bRow, bCol);

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 2);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 2);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 3);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 3);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 2);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, bRow, bCol, maxThrottle, 3);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 1);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end


[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 3);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, bRow, bCol, maxThrottle, 2);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 1);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end


[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 1);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 2);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 1);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 3);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 2);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 1);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

[thrustRow, thrustCol, r, c,] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle, 3);
[thrustRow2, thrustCol2, r, c] = solver2(chart, r,c, aRow, aCol, maxThrottle, 1);
thrustRow = [thrustRow, thrustRow2];
thrustCol = [thrustCol, thrustCol2];
score = runsolution(thrustRow, thrustCol, chart, aRow, aCol, bRow, bCol);
if score < bestScore
    bestScore = score;
    bestRow = thrustRow;
    bestCol = thrustCol;
end

return
end

function [thrustRow, thrustCol, aRow, aCol] = solver2(chart, aRow, aCol, bRow, bCol, maxThrottle,dir)

rowWind = chart(:,:,1);
colWind = chart(:,:,2);

rWind = rowWind(aRow, aCol);
cWind = colWind(aRow, aCol);

if dir == 1
    dirR = sign(bRow-aRow);
    dirC = sign(bCol-aCol);
end
if dir == 2
    dirR = sign(bRow-aRow);
    dirC = 0;
end
if dir == 3
    dirR = 0;
    dirC = sign(bCol-aCol);
end

thrustRow = -rWind + dirR;
thrustCol = -cWind + dirC;

for i=1:1000;
    aRow = aRow + dirR;
    aCol = aCol + dirC;
    rDist = abs(bRow-aRow);
    cDist = abs(bCol-aCol);
    if dir==1 && (aRow<1 || aCol<1 || rDist <=0 || cDist <=0 )
        break
    end
    if dir==2 && (aRow<1 || rDist <=0)
        break
    end
    if dir==3 && ( aCol<1 || cDist <=0 )
        break
    end
    
    rWind = rowWind(aRow, aCol);
    cWind = colWind(aRow, aCol);
    if maxThrottle >= abs(rWind)+abs(cWind)
        thrustRow = [thrustRow, -rWind];
        thrustCol = [thrustCol, -cWind];       
    end
end

rWind = rowWind(aRow, aCol);
cWind = colWind(aRow, aCol);
thrustRow = [thrustRow, -rWind-dirR];
thrustCol = [thrustCol, -cWind-dirC];

return
end


function [score] = runsolution(thrustRow, thrustCol, chart, AR, AC, BR, BC)
% RUNSOLUTION Simulates the navigation trajectory given the winds and the
% motor thrust.

rowWind = chart(:,:,1);
colWind = chart(:,:,2);
[nR,nC] = size(rowWind);
%[AR,AC] = [aRow, aCol]: %ind2sub([nR,nC],aIndex);
%[BR,BC] = [bRow, bCol]; %ind2sub([nR,nC],bIndex);

% Initialize variables at start point (A)
fR = AR; fC =AC;
fvR = 0; fvC = 0;
dB = (fR-BR)^2 + (fC-BC)^2;

for i = 1:numel(thrustRow)
    ivR = fvR + thrustRow(i) + rowWind(fR,fC);
    ivC = fvC + thrustCol(i) + colWind(fR,fC);
    iR = fR + ivR;
    iC = fC + ivC;
    if iR>nR || iR<1 || iC>nC || iC<1
        break % out of bounds
    end
    fR = iR;
    fC = iC;
    fvR = ivR;
    fvC = ivC;
    % Verify if this is the closest point to B
    if ((fR-BR)^2 + (fC-BC)^2) < dB
         dB = (fR-BR)^2 + (fC-BC)^2;    
    end
end
dA = (fR-AR)^2 + (fC-AC)^2; % Final distance to A

score = dB + dA + sum(abs(thrustRow)) + sum(abs(thrustCol));
end