# Diffing "Nuke from orbit 7" and "ants just guess 89"

 Title: Nuke from orbit 7 ants just guess 89 Author: Alan Chalker David Submitted: 2008-11-12 11:56:25 UTC 2008-11-12 11:57:37 UTC Status: Passed Passed Score: 24045.5 24408.9 Result: 21201.30 (cyc: 32, node: 2273) 21201.30 (cyc: 32, node: 2273) CPU Time: 153.706 155.524 Code: ```function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ... myScentMap,opScentMap,myDeathMap,opDeathMap) x=ismember(diff(find(isnan(mainMap)==1)),[1 4 5 6]); Nx=numel(x); Nxx=Nx-nnz(x); if Nx==0 || Nxx0) % ignore food next to occupied hills adj = [2 6 2 6; 1 3 7 7; 2 4 8 8; 3 5 9 9; 4 10 4 10; 1 7 7 11; 2 6 8 12; 3 7 9 13; 4 8 10 14; 5 9 9 15; 6 12 12 16; 7 11 13 17 8 12 14 18; 9 13 15 19 10 14 14 20; 11 17 17 21 12 16 18 22; 13 17 19 23 14 18 20 24; 15 19 19 25 16 22 22 16; 17 21 23 17 18 22 24 18; 19 23 25 19 20 24 24 20]; food(adj(hills(opant(hills)>0),:)) = 0; end end myants = ants(13); maxs = max(scent(:)); % max reachable scent determines mark value [y,x] = find(maxs == scent); % location(s) of maxs so we can find distance bx = sum(x-3)/numel(x); by = sum(y-3)/numel(y); scent(13) = maxs- max(abs([bx by])) ; % new scent value desired mark = ((scent(13)-origscent)/myants)-0.5; % raise mark adjop = opant(n1); threat = opant(:)+min(adjop(via))'; % direct & blocking threats threat(threat>myants(:)/2) = threatScent; scent = abs(scent(:))+threatScent*threat; mins = min(scent); % get some basic neighborhood information nop = sum(adjop); probattack = .06*nop; numant = sum(ants(:)); numop = sum(opant(:)); agress = numant > 3 && numant > numop+2 ... && (numant > 3*(numop+1) || sum(death(:))==0); % defend? if mins >= threatScent || rand < (opant(13)+probattack)/myants || (hills(13) && rand <2/myants) dx = 0; dy = 0; action = -1; return end to = []; up = food(13); fao = max(food(:)-ants(:)-threat,0); if up % food to carry up fao([-2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2]*bx + [-2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2]*by >=0) = 0; % first check if more food is down sumf = sum(fao); if (sumf <= 3*up+1 || maxs < 10*rand || rand > 1/sqrt(myants/up)) d2b = abs((X-bx))+abs((Y-by)); % try to go up d2b(rocked(n1)) = inf; [mu,iu] = min(100*adjop+d2b'); if (d2b(iu) < d2b(5)) ... && ( ( mu < 100 && rand < up/myants) ... || (agress && 50*myants>mu && rand < min([up/myants,3/4])) ); to = iu; end end end if isempty(to)% we've decided not to carry maxf = max(fao); if maxf==0 % no excess food; go by scent dto = via(:,scent= .75*maxf); % go toward excess food dto = [dto(:); find(food(n1)>0 & ~ants(n1))]; % also like any untouched food end adjop(5) = 1-(up>0)*(myants<2); % we're willing to stay if we guard food sto = dto(adjop(dto)==0); % safe destinations if ~isempty(sto) % there are safe places to = sto(ceil(rand*numel(sto))); % go via any safe else to = dto(ceil(numel(dto)/2)); % attack? if ~agress || myants<=2*opant(to)+2 || rand > (2*opant(to)+1)/myants to = 5; % defend end end end if isempty(to) || (to==5 && nop==0) dto = find(~rocked(n1)); % reachable one-ring neighbors to = dto(ceil(rand*numel(dto))); end dx = X(to); dy = Y(to); if to==5 action = -1; elseif (dx-bx)^2+(dy-by)^2 < bx^2+by^2 % carry going uphill action = 1; else action = 0; end %###################################################################### %vectorize %function [dy,dx,action,mark]=solver2(main,food,ants,opant,scent,opscent,death,opdeath) function [dy,dx,action,mark]=solver2(main,food,ants,opant,scent,opscent,death,opdeath) X = [-1 -1 -1 0 0 0 1 1 1]; Y = [-1 0 1 -1 0 1 -1 0 1]; via=[ 1 1 2 2 3 1 1 2 3 3 4 4 5 6 6 4 7 8 9 6 7 7 8 8 9; 1 2 2 3 3 4 1 2 3 6 4 4 5 6 6 7 7 8 9 9 7 8 8 9 9]; n1 = [7; 8;9;12;13;14;17;18;19]; %adjust map for reachability ttf = 2:4; nbrock = isnan(main(ttf,ttf)); % neighboring rocks unreachable = all(nbrock(via));% unreachable due to rocks if any(unreachable) main(unreachable) = nan; food(unreachable) = 0; ants(unreachable) = 0; opant(unreachable) = 0; end % adjust scent for rocks & hills & threats origscent = scent(13); rocked = isnan(main); scent(rocked) = -1e5; hills = main(:)==1; if any(hills) scent(hills) = 299; food(hills) = 0; if any(opant(hills)>0) % ignore food next to occupied hills adj = [2 6 2 6; 1 3 7 7; 2 4 8 8; 3 5 9 9; 4 10 4 10; 1 7 7 11; 2 6 8 12; 3 7 9 13; 4 8 10 14; 5 9 9 15; 6 12 12 16; 7 11 13 17 8 12 14 18; 9 13 15 19 10 14 14 20; 11 17 17 21 12 16 18 22; 13 17 19 23 14 18 20 24; 15 19 19 25 16 22 22 16; 17 21 23 17 18 22 24 18; 19 23 25 19 20 24 24 20]; food(adj(hills(opant(hills)>0),:)) = 0; end end myants = ants(13); maxs = max(scent(:)); % max reachable scent determines mark value [y,x] = find(maxs == scent); % location(s) of maxs so we can find distance bvec = sum([x y]-3,1)/numel(x); scent(13) = maxs- max(abs(bvec)) ; % new scent value desired mark = ((scent(13)-origscent)/myants)-0.5; % raise mark adjop = opant(n1); threat = opant(:)+min(adjop(via))'; % direct & blocking threats threat(threat>myants(:)/2) = 300; scent = abs(scent(:))+300*threat; mins = min(scent); % get some basic neighborhood information nop = sum(adjop); probattack = .06*nop; numant = sum(ants(:)); numop = sum(opant(:)); agress = numant > 3 && numant > numop+2 ... && (numant > 3*(numop+1) || sum(death(:))==0); % defend? if mins >= 300 || rand < (opant(13)+probattack)/myants dx = 0; dy = 0; action = -1; return end to = []; up = food(13); fao = max(food(:)-ants(:)-threat,0); if up % food to carry up fao([-2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2]*bvec(1) + [-2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2]*bvec(2) >=0) = 0; % first check if more food is down sumf = sum(fao); if (sumf <= 3*up+1 || maxs < 10*rand || rand > 1/sqrt(myants/up)) d2b = (X-bvec(1)).^2+(Y-bvec(2)).^2; % try to go up d2b(rocked(n1)) = inf; [mu,iu] = min(100*adjop+d2b'); if (d2b(iu) < d2b(5)) ... && ( ( mu < 100 && rand < up/myants) ... || (agress && 50*myants>mu && rand < min([up/myants,3/4])) ); to = iu; end end end if isempty(to)% we've decided not to carry maxf = max(fao); if maxf==0 % no excess food; go by scent dto = via(:,scent= .75*maxf); % go toward excess food dto = [dto(:); find(food(n1)>0 & ~ants(n1))]; % also like any untouched food end adjop(5) = 1-(up>0)*(myants<2); % we're willing to stay if we guard food sto = dto(adjop(dto)==0); % safe destinations if ~isempty(sto) % there are safe places to = sto(ceil(rand*numel(sto))); % go via any safe else to = dto(ceil(numel(dto)/2)); % attack? if ~agress || myants<=2*opant(to)+2 || rand > (2*opant(to)+1)/myants to = 5; % defend end end end if isempty(to) || (to==5 && nop==0) dto = find(~rocked(n1)); % reachable one-ring neighbors to = dto(ceil(rand*numel(dto))); end dx = X(to); dy = Y(to); if to==5 action = -1; elseif (dx-bvec(1))^2+(dy-bvec(2))^2 < bvec(1)^2+bvec(2)^2 % carry going uphill action = 1; else action = 0; end```