# Diffing "Survival of the Fittest !!!!" and "Survival of the Obfuscated !!"

 Title: Survival of the Fittest !!!! Survival of the Obfuscated !! Author: David David Submitted: 2006-04-12 14:55:40 UTC 2006-04-12 15:56:27 UTC Status: Passed Passed Score: 847.508 847.496 Result: 84348 84348 CPU Time: 66.4078 66.384 Code: ```function moves=solver(board,nMoves) [rows, cols] = size(board); moves = ones(nMoves,3); count=0; function mv=solver(B,nmv) [R, C]=size(B); mv=ones(nmv,3); J=0; r=rand(16,1); % set move calculation parameters if (rows*cols) > (10.5+(rand-0.5))*nMoves next_move_count = 40; choke_factor = .847; depth_factor = 2.93; rot = 1.566; elseif (rows*cols) > (8.5+(rand-0.5))*nMoves next_move_count = 29; choke_factor = .847; depth_factor = 2.93; rot = 1.566; elseif rows*cols >= 256 + round((rand-0.5)*10) next_move_count = 26; choke_factor = .951; depth_factor = 2.8; rot = 1.4; if (R*C)>(10.5+(rand-0.5))*nmv next_move_J=40; chokeF=.847; depthF=2.93; rot =1.566; elseif (R*C)>(8.5+(rand-0.5))*nmv next_move_J=29; chokeF=.847; depthF=2.93; rot =1.566; elseif R*C >= 256+round((rand-0.5)*10) next_move_J=26; chokeF=.951; depthF=2.8; rot=1.4; else next_move_count = 26; choke_factor = 0.945; depth_factor = 2.97; rot = 1.46; next_move_J=26; chokeF=0.945; depthF=2.97; rot =1.46; end % adjust block weights board = nthroot(board,rot); depth = ceil(depth_factor*nMoves/10); flip = floor((0.66+(rand-0.5)*0.1)*nMoves); B=nthroot(B,rot); depth=ceil(depthF*nmv/10); flip=floor((0.66+(rand-0.5)*0.1)*nmv); while 1 % restore original colour values half-way through game if count==flip, board = nthroot(board,2/(1+rot)); end % find highest value moves [cell_list,value_list,mask] = CalculateMoves(rows,cols,board); if isempty(cell_list), [swapmv,board]=CheckForSwap(rows,cols,board); if swapmv(1) count = count+1; moves(count,:) = swapmv; if (count==nMoves) break; end continue end break end % check future move values and re-weight current moves if count0,i); B(:,i)=0;B(R-numel(k)+1:R,i)=k; k=B(B(:,i)>0,i); B(:,i)=0;B(R-numel(k)+1:R,i)=k; end function [swapmv,outboard]=CheckForSwap(rows,cols,board) swapper=[-1 rows 1 -rows]; lentmp=rows*cols; function [swapmv,outB]=CheckForSwap(R,C,B) swapper=[-1 R 1 -R]; lentmp=R*C; swapmv=zeros(1,3); outboard=board; outB=B; maxswap=0; list=find(board~=0)'; list=find(B~=0)'; nList=numel(list); for k=1:nList jj=list(k); for swapdir=1:4 np=jj+swapper(swapdir); if np>0 && np<=lentmp && board(np)>0 && board(np)~=board(jj) tboard=DoSwap(board,jj,np); [s_ceil,s_value]=CalculateMoves(rows,cols,tboard); if (~isempty(s_value)) if (max(s_value)> maxswap) maxswap=max(s_value); idxq=jj;idxr=swapdir;idxs=np; end else for k2=max(1,k-10):min(nList,k+10) jj2=list(k2); for swapdir2=1:4 np2=jj2+swapper(swapdir2); if np2>0 && np2<=lentmp && tboard(np2)>0 && tboard(np2)~=tboard(jj2) tboard2=DoSwap(tboard,jj2,np2); [s_ceil2,s_value2,s_mask2]=CalculateMoves(rows,cols,tboard2); if (~isempty(s_value2) && max(s_value2)>maxswap) [s_max,s_idx]=max(s_value2); Y=s_ceil2(s_idx); while (s_mask2(Y)~=Y) Y = s_mask2(Y); end if (Y==np || Y==jj) maxswap=max(s_value2); idxq=jj;idxr=swapdir;idxs=np; end end end end end end end end jj=list(k); for swapdir=1:4 np=jj+swapper(swapdir); if np>0 && np<=lentmp && B(np)>0 && B(np)~=B(jj) tB=DoSwap(B,jj,np); [s_ceil,s_value]=Calculatemv(R,C,tB); if (~isempty(s_value)) if (max(s_value)> maxswap) maxswap=max(s_value); idxq=jj;idxr=swapdir;idxs=np; end else for k2=max(1,k-10):min(nList,k+10) jj2=list(k2); for swapdir2=1:4 np2=jj2+swapper(swapdir2); if np2>0 && np2<=lentmp && tB(np2)>0 && tB(np2)~=tB(jj2) tB2=DoSwap(tB,jj2,np2); [s_ceil2,s_value2,s_M2]=Calculatemv(R,C,tB2); if (~isempty(s_value2) && max(s_value2)>maxswap) [s_max,s_idx]=max(s_value2); Y=s_ceil2(s_idx); while (s_M2(Y)~=Y) Y=s_M2(Y); end if (Y==np || Y==jj) maxswap=max(s_value2); idxq=jj;idxr=swapdir;idxs=np; end end end end end end end end end if (maxswap>0) swapmv = [mod(idxq-1,rows)+1,ceil(idxq/rows),idxr]; outboard=DoSwap(board,idxq,idxs); swapmv=[mod(idxq-1,R)+1,ceil(idxq/R),idxr]; outB=DoSwap(B,idxq,idxs); end function board = DoSwap(board,el1,el2) board([el1,el2]) = board([el2,el1]); function B=DoSwap(B,el1,el2) B([el1,el2])=B([el2,el1]);```