function [message,results,timeElapsed] = runcontest
%RUNCONTEST Test an entry.
% [MESSAGE,RESULTS,TIME] = RUNCONTEST runs the M-file solver.m against all
% the problems defined in testsuite_sample.mat. MESSAGE returns a summary
% of the testing. RESULTS measures how well the entry solved the problem
% and TIME measures the time the entry took to compute its answer.
%
% Copyright 2005 The MathWorks, Inc.
% Define constants.
load testsuite_sample testsuite
% Run the submission for each problem in the suite.
n = numel(testsuite);
solution = cell(n,1);
time0 = cputime;
for i = 1:n
puzzle = testsuite(i).puzzle;
list = testsuite(i).list;
solution{i} = solver(puzzle,list);
end
timeElapsed = cputime-time0;
% Grade all answers.
score = zeros(n,1);
for i = 1:n
score(i) = grade(testsuite(i).puzzle,testsuite(i).list,solution{i});
end
% Report results.
results = sum(score);
message = sprintf('results: %.2f\ntime: %.2f',results,timeElapsed);
%===============================================================================
function score = grade(puzzle,list,sol)
% Check validity of solutions.
if ~isa(sol,'double') || ~isreal(sol)
error('Solver must return a non-complex and double board')
end
if size(sol,1)~=9 || size(sol,2)~=9 || numel(sol) ~= 81
error('Solver must return a 9x9 board.')
end
h = puzzle~=0;
if any(puzzle(h)~=sol(h))
error('Fixed boxes in the puzzle were modifyied.')
end
if ~all(ismember(sol(~h),list))
error('The value in one or more boxes were not taken from the provided list.')
end
if numel(unique(sol))~=81
error('One or more of the numbers in the list were used twice')
end
% Score output.
h = reshape(1:9,3,3);
g = ceil((1:9)/3);
h = (h(g,g)-1)*9 + repmat(h,3);
sums = [sum(sol(h)) sum(sol) sum(sol,2)'];
score = sum(abs(mean(sums)-sums));