MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Solution 102334

Submitted on 25 Jun 2012 by Richard Zapor

Correct

195Size
`This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.`

### Test Suite

Test
Code Input and Output
1
Pass

```%%
feval(@assignin,'caller','score',2000);
```

2
Pass

```%%
format short
format compact
global net_time
%fn='http://tinyurl.com/zapor-Tiles-sample-mat';
fn='http://tinyurl.com/matlab-tiles-mat';
testSuiteFile = 'raz_tiles.mat';
urlwrite(fn,testSuiteFile);
brd=59;
tiles = tests.testsuite(brd).tiles;
rows = tests.testsuite(brd).r;
cols = tests.testsuite(brd).c;
boardSize = [rows, cols];
[board,orientation]=board_perfect(boardSize,tiles); % run twice for timing
t0=clock;
[board,orientation]=board_perfect(boardSize,tiles);
dt=etime(clock,t0)*1e3;
% verify score
t=tiles;
ntiles=size(tiles,1);
te = [t; t(:,[2:4,1]); t(:,[3:4,1:2]); t(:,[4,1:3])];
% build check arrays UD, LR
LR=zeros(rows,2*cols);
UD=zeros(2*rows,cols);
for r=1:rows
for c=1:cols
tptr=board(r,c);
tor=orientation(tptr);
UD(2*r-1,c)=te(tptr+ntiles*(tor-1),1);
UD(2*r,c)=te(tptr+ntiles*(tor-1),3);
LR(r,2*c-1)=te(tptr+ntiles*(tor-1),4);
LR(r,2*c)=te(tptr+ntiles*(tor-1),2);
end
end
checksum=sum([LR(:,1)' LR(:,end)' UD(1,:) UD(end,:)]);
for idx=2:2:2*rows-2 % LR Square array assumed here
checksum=checksum+sum(LR(:,idx)-LR(:,idx+1))+sum(UD(idx,:)-UD(idx+1,:));
end
assert(checksum==0,sprintf('Checksum = %s\n',num2str(checksum)));
net_time=dt
```
```net_time =
169.9280
```
3
Pass

```%%
global net_time
temp=net_time; % anti-cheat
%fn='http://tinyurl.com/zapor-Tiles-contest-mat';
fn='http://tinyurl.com/matlab-tilesC-mat';
testSuiteFile = 'raz_tiles.mat';
urlwrite(fn,testSuiteFile);
brd=6;
tiles = tests.testsuite(brd).tiles;
rows = tests.testsuite(brd).r;
cols = tests.testsuite(brd).c;
boardSize = [rows, cols];
[board,orientation]=board_perfect(boardSize,tiles); % run twice for timing
t0=clock;
[board,orientation]=board_perfect(boardSize,tiles);
dt=etime(clock,t0)*1e3
% verify score
t=tiles;
ntiles=size(tiles,1);
te = [t; t(:,[2:4,1]); t(:,[3:4,1:2]); t(:,[4,1:3])];
% build check arrays UD, LR
LR=zeros(rows,2*cols);
UD=zeros(2*rows,cols);
for r=1:rows
for c=1:cols
tptr=board(r,c);
tor=orientation(tptr);
UD(2*r-1,c)=te(tptr+ntiles*(tor-1),1);
UD(2*r,c)=te(tptr+ntiles*(tor-1),3);
LR(r,2*c-1)=te(tptr+ntiles*(tor-1),4);
LR(r,2*c)=te(tptr+ntiles*(tor-1),2);
end
end
checksum=sum([LR(:,1)' LR(:,end)' UD(1,:) UD(end,:)]);
for idx=2:2:2*rows-2 % LR Square array assumed here
checksum=checksum+sum(LR(:,idx)-LR(:,idx+1))+sum(UD(idx,:)-UD(idx+1,:));
end
assert(checksum==0,sprintf('Checksum = %s\n',num2str(checksum)));
net_time=(dt+temp)/2
```
```dt =
165.6650
net_time =
167.7965
```
4
Pass

```%%
global net_time
% Limit Score to 2000 for graph quality
t=mtree('board_perfect.m','-file');
scr=floor(length(t.nodesize)/10+net_time);
scr=min(scr,2000)
feval(@assignin,'caller','score',floor(scr));
%fh=fopen('board_perfect.m','wt');
%fprintf(fh,'%s\n',repmat('1;',[1,round(scr/2)]));
%fclose(fh);
```
```scr =
195
```