Finish 2005-05-18 09:00:00 UTC

justwant1topass

by J LeBlanc

Status: Passed
Results: 36438.3650
CPU Time: 143.41
Score: 3855.25
Submitted at: 2005-05-18 15:22:00 UTC
Scored at: 2005-05-18 18:53:45 UTC

Current Rank: 1565th

Comments
J LeBlanc
18 May 2005
Just thought this would help to learn Matlab quickly,
wanted to see what I could get on my own, without
looking around (too much) at everyone else's work.
Please login or create a profile.
Code
function [dy,dx,mark,carry] = solver(M,F,A,Sin)
%SOLVER The ant brain.
%   30900 before I broke something :)

S=Sin;

% squares I can't reach
BAD=M<0;
if BAD(7)||BAD(9)||BAD(17)||BAD(19)
    if BAD(7)
        BAD(1)=1;
        BAD(2)=BAD(2)||BAD(8);
        BAD(3)=BAD(3)||(BAD(8)&&BAD(9));
        BAD(6)=BAD(6)||BAD(12);
        BAD(11)=BAD(11)||(BAD(12)&&BAD(17));
    end;
    if BAD(9)
        BAD(4)=BAD(4)||BAD(8);
        BAD(5)=1;
        BAD(10)=BAD(10)||BAD(14);
    end;
    if BAD(17)
        BAD(16)=BAD(16)||BAD(12);
        BAD(21)=1;
        BAD(22)=BAD(22)||BAD(18);
    end;
    if BAD(19)
        BAD(15)=BAD(15)||(BAD(9)&&BAD(14));
        BAD(20)=BAD(20)||(BAD(14));
        BAD(23)=BAD(23)||(BAD(17)&&BAD(18));
        BAD(24)=BAD(24)||(BAD(18));
        BAD(25)=1;
    end;
end;
% modify current board based on reachable regions
M(BAD)=-1;
F(BAD)=0;
S(BAD)=0;
B=zeros(5,5);
B(BAD)=1;

% anthills I can reach
homes=find(M(:)==1);
F(homes)=0;

% size and direction of biggest scent trail
S(13)=0;
S(A>0)=S(A>0)-4; 
[Smax,Hdir]=max(S(:));

% markings indicate the way home
if not(isempty(homes))    
    mark=100;
elseif Smax>15
    mark=max(Smax-rem(Smax,5)-Sin(13),0);
else
    mark=max(15-Sin(13),0);
end;
if mark>100
    mark=100;
end;


if F(13)>0                          % bring food home
    carry = true;
    mark=max(15-Sin(13),0);
    if not(isempty(homes))
        target=homes(1);
    elseif Smax>15 
        target=Hdir;
    else
        target=26-(Hdir);
    end;
else
    carry = false;
    ff=F-A; % free food
    if max(ff(:))>0                 % go towards visible free food
        mark=mark-10;
        ffwhere = find(ff>0);
        n1=1;                           
        target=ffwhere(1);
        while and( B(target)==1, n1<length(ffwhere) )
            n1=n1+1;
            target=ffwhere(n1);
        end;
    else                                % no visible free food
        if (rand(1)>0.25)&&(B(26-(Hdir))==0)
            target=26-(Hdir);
        else
            myod = setdiff([7,8,9,12,14,17,18,19],BAD(:));
            if isempty(myod)
                target=13;
            else
                any=(ceil(length(myod).*rand(1)));
                target = myod(any);
            end;
        end;
    end;
end;

[dy,dx] = go1(target);

if B(3+dy,3+dx)==1
    myod = setdiff([7,8,9,12,14,17,18,19],BAD(:));
    if length(myod)==1
        [dy,dx] = go1(myod(1));
    else
        any=(ceil(length(myod).*rand(1)));
        if (dx==0)||(dy==0) % target among [3,23,11,15]
            if B(3+dx+dy,3+dx+dy)==0 % try diagonal
                dx=dx+dy;
                dy=dx;
            elseif B(3+dy-dx,3+dx-dy)==0 % other diagonal
                dy1=dy-dx;
                dx1=dx-dy;
                dy=dy1;
                dx=dx1;
            else 
                [dy,dx]=go1(myod(any)); % can only move away anyhow
           end;
        elseif ismember(target,[1,5,21,25]) % want to go to a corner
            if B(3+dy,3)==0
                dx=0;
            elseif B(3,3+dx)==0
                dy=0;
            else 
                [dy,dx]=go1(myod(any)); 
            end;
        elseif ismember(target,[2,4,22,24])
            if B(3,3+dx)==0
                dy=0;
            else 
                [dy,dx]=go1(myod(any)); 
            end;
        else  % target among [6,10,16,20]
            if B(3+dy,3)==0
                dx=0;
            else 
                [dy,dx]=go1(myod(any)); 
            end;
        end;
    end;
end;
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [DY,DX] = go1(n)
EW = zeros(5,5);
EW(1:10) = -1;
EW(16:25) = 1;
NS = EW';
DY = NS(n);
DX = EW(n);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%