Finish 2012-11-07 16:00:00 UTC

Spiral wShifts

by Richard Zapor

Status: Passed
Results: 105409 (cyc: 9, node: 395)
CPU Time: 1.163
Score: 105421.0
Submitted at: 2012-11-02 00:41:20 UTC
Scored at: 2012-11-02 00:46:27 UTC

Current Rank: 1684th (Highest: 66th )

Comments
Please login or create a profile.
Code
function xyout = solver(a, xyin, wts)

nr=size(xyin,1);
awork=a; % when placed zero out columns
xyout=xyin;
unmoved=ones(nr,1);
dxy=[1 1; -1 -1; -1 1;1 -1;1 0;0 1;-1 0;0 -1];

sc=0; % spiral count
xy_center=[0 0];

while any(awork(:))
 sa=[(1:nr)' sum(awork,2)];
 sa=sortrows(sa,-2);
 node=sa(1);
 numadj=sa(1,2);
 d=2*ceil(numadj/8); % circle radius 2 to 3
 
 adj=find(awork(node,:)); % identify unplaced adjacent nodes
 xy_offset=[round(d*cosd(360/numadj*(0:numadj-1))); round(d*sind(360/numadj*(0:numadj-1)))]';
 if numadj==8,xy_offset(8)=2;end
 
 if mod(numadj,2)==0
  xyout(node,:)=xy_center-[-1 1]; % offset center to avoid knottiness was [0 1]
 else
  xyout(node,:)=xy_center-[0 1]; % odd down
 end
 awork(node,:)=0;
 awork(:,node)=0;
 awork(adj,:)=0;
 awork(:,adj)=0;
 unmoved([node adj])=0;
 
 for j=1:length(adj)
  xyout(adj(j),:)=xy_center+xy_offset(j,:);
 end
 
 sc=sc+1;
 xy_center=[round(16*sc*cosd(17*sc)) round(16*sc*sind(17*sc))]; % was 5
  
end % while nodes to place

% Unmoved may have been over-mapped. Need to fix
% Also moves singles next to adjacency nodes to reduce knots
unmov_vec=find(unmoved);
for j=1:length(unmov_vec)
 ptr=find(a(unmov_vec(j),:)==1,1,'first'); % adjacency of unmoved
 for idxy=1:8
  if ~ismember(xyout(ptr,:)+dxy(idxy,:),xyout,'rows') % found an adjacent point
   xyout(unmov_vec(j),:)=xyout(ptr,:)+dxy(idxy,:);
   break
  end
 end % idxy
end % moving singles and unmoved next to adjacents

if size(unique(xyout,'rows'),1)<nr,xyout=xyin;end % safety net

end