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

lazySailor2

by Sebastian Ullmann

Status: Passed
Results: 249022 (cyc: 5, node: 411)
CPU Time: 1.037
Score: 49804.8
Submitted at: 2010-11-11 00:24:15 UTC
Scored at: 2010-11-11 00:24:47 UTC

Current Rank: 2451st (Highest: 7th )

Comments
Sebastian Ullmann
11 Nov 2010
simple safety distance to border
Please login or create a profile.
Code
function [thrustR, thrustC] = solver2(chart, aIndex, bIndex, maxThrottle)

  windR = chart(:,:,1);
  windC = chart(:,:,2);
  
  [nR,nC] = size(windR);
  [AR,AC] = ind2sub([nR,nC],aIndex);
  [BR,BC] = ind2sub([nR,nC],bIndex);
  
  posR = AR;
  posC = AC;
  velR = 0;
  velC = 0;

  [X,Y] = meshgrid(-maxThrottle:maxThrottle);
  pick = abs(X(:))+abs(Y(:)) < maxThrottle;
  tryThrustR = X(pick);
  tryThrustC = Y(pick);
  
  goingHome = false;
  
  thrustR = [];
  thrustC = [];
  
  for i=1:1000
  
    tryVelR = velR + tryThrustR + windR(posR,posC);
    tryVelC = velC + tryThrustC + windC(posR,posC);
    tryPosR = posR + tryVelR;
    tryPosC = posC + tryVelC;
    
    velFactor = 2;
    inside = find(tryPosR<size(windR,1) ...
                & tryPosR>1 ...
                & tryPosC<size(windR,2) ...
                & tryPosC>1 ...
                & (tryPosR + velFactor*tryVelR) < size(windR,1) ...
                & (tryPosR + velFactor*tryVelR) > 1 ...
                & (tryPosC + velFactor*tryVelC) < size(windR,2) ...
                & (tryPosC + velFactor*tryVelC) > 1);
    if goingHome
      distance    = (posR-AR).^2 + (posC-AC).^2;
      tryDistance = (tryPosR(inside)-AR).^2 + (tryPosC(inside)-AC).^2;
    else
      distance    = (posR-BR).^2 + (posC-BC).^2;
      tryDistance = (tryPosR(inside)-BR).^2 + (tryPosC(inside)-BC).^2;
    end
    
    improveDistance = inside(tryDistance < distance);
    if isempty(improveDistance)
      if ~goingHome
        goingHome = true;
        continue
      else
        return
      end
    end
    [~,I] = min(abs(tryThrustR(improveDistance))+abs(tryThrustC(improveDistance)));
    I = improveDistance(I);
      
    posR = tryPosR(I);
    posC = tryPosC(I);
    
    velR = tryVelR(I);
    velC = tryVelC(I);
    
    thrustR(end+1) = tryThrustR(I);
    thrustC(end+1) = tryThrustC(I);
         
  end

end