Cody

Solution 322501

Submitted on 23 Sep 2013 by Alfonso Nieto-Castanon
  • Size: 5508
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% feval(@assignin,'caller','score',6000);

2   Pass
%% max_moves=4000; % Interactive approx 1000 moves maxLives=6; map=[... repmat('a',1,28); 'accccccccccccaacccccccccccca'; 'acaaaacaaaaacaacaaaaacaaaaca'; 'acaaaacaaaaacaacaaaaacaaaaca'; 'acaaaacaaaaacaacaaaaacaaaaca'; 'acccccccccccccccccccccccccca'; 'acaaaacaacaaaaaaaacaacaaaaca'; 'acaaaacaacaaaaaaaacaacaaaaca'; 'accccccaaccccaaccccaacccccca'; 'aaaaaacaaaaabaabaaaaacaaaaaa'; 'aaaaaacaaaaabaabaaaaacaaaaaa'; 'aaaaaacaabbbbbbbbbbaacaaaaaa'; 'aaaaaacaabaaabbaaabaacaaaaaa'; 'aaaaaacaabalbbbblabaacaaaaaa'; 'bbbbbbcbbbabbbbbbabbbcbbbbbb'; 'aaaaaacaabalbbbblabaacaaaaaa'; 'aaaaaacaabaaaaaaaabaacaaaaaa'; 'aaaaaacaabbbbbbbbbbaacaaaaaa'; 'aaaaaacaabaaaaaaaabaacaaaaaa'; 'aaaaaacaabaaaaaaaabaacaaaaaa'; 'accccccccccccaacccccccccccca'; 'acaaaacaaaaacaacaaaaacaaaaca'; 'acaaaacaaaaacaacaaaaacaaaaca'; 'acccaacccccccbdcccccccaaccca'; 'aaacaacaacaaaaaaaacaacaacaaa'; 'aaacaacaacaaaaaaaacaacaacaaa'; 'accccccaaccccaaccccaacccccca'; 'acaaaaaaaaaacaacaaaaaaaaaaca'; 'acaaaaaaaaaacaacaaaaaaaaaaca'; 'acccccccccccccccccccccccccca'; repmat('a',1,28);]; map=map-'b'; [nr, nc]=size(map); mapdelta=[-1 nr 1 -nr]; % Valid as long as not on an edge gmovxy=[0 -1;1 0;0 1;-1 0]; tunnel=find(map(:,1)==0); % tunnelptr tunnel=[tunnel tunnel+nr*(nc-1)]; % Entrance/Exit Tunnel [pmr, pmc]=find(map==2); % pi 24 row pj 15 column of map ptrpac=find(map==2); ptrpac=find(map==2); ptrpac_start=ptrpac; ptrg_start=find(map>2); map(ptrg_start)=[10 20 30 40]; [gstartx, gstarty]=find(map>2); lives=0; % Lives movepac=0; while lives<maxLives && any(mod(map(:),10)==1) && movepac<max_moves for pac2x=1:2 if lives>maxLives-1 || ~any(mod(map(:),10)==1),break;end % Died or Completed movepac=movepac+1; [curdir]=PACMAT8_2Xsolver(map); [pmr, pmc]=find(map==2); if curdir>0 if map(ptrpac+mapdelta(curdir))==-1 % Do nothing - Ran into a Wall elseif map(ptrpac+mapdelta(curdir))>2 % ran into ghost map(ptrpac)=0; % remove PAC from the board lives=lives+1; %if lives==0,break;end % reset the board [ptrgx, ptrgy]=find(map>2); ptrg=find(map>2); map(ptrg)=mod(map(ptrg),10); map(ptrpac_start)=2; map(ptrg_start)=[10 20 30 40]; ptrpac=find(map==2); continue; % Lost a Life else % legal move map(ptrpac)=0; % Eat Dot and clear PAC ptrpac=ptrpac+mapdelta(curdir); if ptrpac==tunnel(1),ptrpac=tunnel(2)-nr;end if ptrpac==tunnel(2),ptrpac=tunnel(1)+nr;end map(ptrpac)=2; end end % curdir >0 end % pac2x if lives>maxLives-1 || ~any(mod(map(:),10)==1),break;end % Completed % Ghosts for i=1:4 ghosts=find(map>2); ptrpac=find(map==2); % Target dot=false; [gptrx, gptry]=find(map==10*i); gidx=find(map==10*i); if isempty(gidx) [gptrx, gptry]=find(map==10*i+1); % ghost must be on a dot gidx=find(map==10*i+1); dot=true; end % Find valid ghost moves using gmap % mapdelta=[-1 nr 1 -nr]; gmov=find(map(gidx+mapdelta)==2); % adjacent to PACMAT if gidx==15+nr && ptrpac==nr*(nc-2)+15 % Ghost Tunnel Adj gmov=4; end if gidx==nr*(nc-2)+15 && ptrpac==15+nr % Ghost Tunnel Adj gmov=2; end if ~isempty(gmov) % PAC adjacent lives=lives+1; % reset the board [pmr, pmc]=find(map==2); % PACMAT erase coords map(map==2)=0; [ptrgx, ptrgy]=find(map>2); ptrg=find(map>2); map(ptrg)=mod(map(ptrg),10); map(ptrpac_start)=2; map(ptrg_start)=[10 20 30 40]; ptrpac=find(map==2); break; % Ghost move loop else % gmap avoid walls and other ghosts Walls gmap=map; ptctr=0; gmap(gmap>=0)=Inf; gmap(ghosts)=-1; % other ghosts are like walls Ghosts_004/5 gmap(gidx)=Inf; % Ultimate target gmap(ptrpac)=1; % Start at PACMAT and expand to ghost while gmap(gidx)>101 && ptctr<100 % potential boxed dot % find dots, add a counter to distance form location, keep min value % when ptrpac gets a value it will be from nearest dot % find side with dmap(ptrpac)-1 ptctr=ptctr+1; dpts=find(gmap==ptctr); newpt_idx=repmat(dpts,1,4)+repmat(mapdelta,length(dpts),1); % Ghost Tunnel Access tunL=find(newpt_idx==15); tunR=find(newpt_idx==nr*nc-16); if ~isempty(tunL) newpt_idx(tunL)=nr*(nc-2)+15; end if ~isempty(tunR) newpt_idx(tunR)=15+nr; end gmap(newpt_idx(:))=min(gmap(newpt_idx(:)),ptctr+1); end % Simplified by ghosts are walls: No Ghost Jumping if ~isinf(gmap(gidx)) % Path(s) to Ghost found % Tunnel Check gmov=[]; if gidx==15+nr % Tunnel gmov=2; if gmap(gidx+mapdelta(2))==gmap(gidx)-1 gmov=2; else gmov=4; % Possible error ghost onto ghost end end if gidx==nr*(nc-2)+15 % Tunnel if gmap(gidx+mapdelta(4))==gmap(gidx)-1 gmov=4; else gmov=2; % Possible error ghost onto ghost end end if isempty(gmov) % Non-Tunnel Move for gmov=1:4 % execute with a find? if gmap(gidx+mapdelta(gmov))==gmap(gidx)-1,break;end end end else gmov=[]; % No path to PACMAT found end if ~isempty(gmov) % valid g move : ghost may not stand on ghost map(gptrx,gptry)=mod(map(gptrx,gptry),10); %tunLtf=false; %tunRtf=false; if gidx==nr+15 if gmov==2 % Left Tunnel but go Right map(gidx+mapdelta(gmov))=map(gidx+mapdelta(gmov))+10*i; else % Tunnel map(nr*(nc-2)+15)=10*i; %tunLtf=true; end elseif gidx==nr*(nc-2)+15 if gmov==4 % Right Tunnel but go Left map(gidx+mapdelta(gmov))=map(gidx+mapdelta(gmov))+10*i; else % Tunnel map(nr+15)=10*i; %tunRtf=true; end else % Standard move map(gidx+mapdelta(gmov))=map(gidx+mapdelta(gmov))+10*i; end end % ~isempty(gmov) standard move - no capture end % ~isempty(gmov) PACMAT adjacent end % i ghost moves end % while any dots and < max_moves maxLives % dots=length(find(mod(map,10)==1)); % fprintf('moves %i\n',movepac) fprintf('dots %i\n',dots) fprintf('Lives Spent %i\n',lives) % % To Pass need to leave at most 0 dots assert(dots==0,sprintf('Max Dots 0, Dots Remaining %i\n',dots)) score= movepac + 1000*lives; % All dots must be removed feval( @assignin,'caller','score',floor(min( 6000,score )) );

moves 508 dots 0 Lives Spent 5