Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 425583

Submitted on 3 Apr 2014 by Tim

Correct

3Size
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
Code Input and Output
1
Pass
 
%%
fh=fopen('main.m','wt');
fprintf(fh, '%s \n', 'function out = main(n)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'result = 0;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'for i = 1:n    ');
fprintf(fh, '%s \n', '    board = zeros(4);');
fprintf(fh, '%s \n', '    board = saltBoard(board);');
fprintf(fh, '%s \n', '    board = saltBoard(board);');
fprintf(fh, '%s \n', '    %dispBoard(board)');
fprintf(fh, '%s \n', '    direction = 1;');
fprintf(fh, '%s \n', '    while (direction ~= 0)');
fprintf(fh, '%s \n', '        %pause(0.1)');
fprintf(fh, '%s \n', '        %clc');
fprintf(fh, '%s \n', '        direction = getMove(board);');
fprintf(fh, '%s \n', '        board = updateBoard(board, direction);');
fprintf(fh, '%s \n', '        %dispBoard(board)');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    %figure(1)');
fprintf(fh, '%s \n', '    %dispBoard(board)');
fprintf(fh, '%s \n', '    %figure(2)');
fprintf(fh, '%s \n', '    %hist(result,[2 4 8 16 32 64 128 256 512 1024,2048])');
fprintf(fh, '%s \n', '    %drawnow');
fprintf(fh, '%s \n', '    result(i) = max(board(:));');
fprintf(fh, '%s \n', '    %disp([i result(i) max(result)])');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = max(result) + mean(result)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function out = collapse(in)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'in = squish(in);');
fprintf(fh, '%s \n', 'for i = 1:3');
fprintf(fh, '%s \n', '    result = miniCollapse(in(i:i+1));');
fprintf(fh, '%s \n', '    out(i:i+1) = result;');
fprintf(fh, '%s \n', '     in(i:i+1) = result;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = squish(in);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function out = squish(in);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = in(in ~= 0);');
fprintf(fh, '%s \n', 'if numel(out) ~= 4');
fprintf(fh, '%s \n', '    out(4) = 0;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function out = miniCollapse(in)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if in(1) == in(2)');
fprintf(fh, '%s \n', '    out(1) = in(1) * 2;');
fprintf(fh, '%s \n', '    out(2)  = 0;');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '    out = in;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function out = saltBoard(in)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if nnz(in) == 16');
fprintf(fh, '%s \n', '    out = nan;');
fprintf(fh, '%s \n', '    return;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'vi = find(in == 0);');
fprintf(fh, '%s \n', 'selected = randi(numel(vi));');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'thresh = 0.1;');
fprintf(fh, '%s \n', 'if (rand < thresh)');
fprintf(fh, '%s \n', '    salt = 4;');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '    salt = 2;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = in;');
fprintf(fh, '%s \n', 'out(vi(selected)) = salt;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function board = updateBoard(board, direction)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'boardOriginal = board;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'board = collapseBoard(board,direction);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if ~isequal(boardOriginal, board)');
fprintf(fh, '%s \n', '    board = saltBoard(board);');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function dispBoard(board)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'clf');
fprintf(fh, '%s \n', 'r = 1;');
fprintf(fh, '%s \n', 'c = 1;');
fprintf(fh, '%s \n', 'v = 2;');
fprintf(fh, '%s \n', 'for r = 1:4');
fprintf(fh, '%s \n', '    for c = 1:4');
fprintf(fh, '%s \n', '        v = board(r,c);');
fprintf(fh, '%s \n', '        dispSquare(r,c,v)');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function dispSquare(r,c,v)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'cmap = autumn(12);');
fprintf(fh, '%s \n', 'absIndex = (r-1)*4 + c;');
fprintf(fh, '%s \n', 'if v == 0 ');
fprintf(fh, '%s \n', '    cMapIndex = 1;');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '    cMapIndex = log2(v) + 1;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'h = subplot(4,4,absIndex);');
fprintf(fh, '%s \n', 'set(h,''xtick'',[])');
fprintf(fh, '%s \n', 'set(h,''ytick'',[])');
fprintf(fh, '%s \n', 'set(h,''color'',cmap(cMapIndex,:))');
fprintf(fh, '%s \n', '%axis off');
fprintf(fh, '%s \n', 'text(0.5,0.5,num2str(v), ''fontsize'', 20)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function flag = isMoveDirection(board, direction)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'originalBoard = board;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'board = updateBoard(board, direction);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'flag = ~isequal(board, originalBoard);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function out = collapseBoard(in, direction)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'rotDirection = direction-1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'in = rot90(in,rotDirection);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'for r = 1:4');
fprintf(fh, '%s \n', '    out(r,:) = collapse(in(r,:));');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = rot90(out,-rotDirection);');
fclose(fh);
rehash path
[Warning: Function diary has the same name as a MATLAB builtin. We suggest you rename the function
to avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function license has the same name as a MATLAB builtin. We suggest you rename the function
to avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function mex has the same name as a MATLAB builtin. We suggest you rename the function to
avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function home has the same name as a MATLAB builtin. We suggest you rename the function to
avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function keyboard has the same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function more has the same name as a MATLAB builtin. We suggest you rename the function to
avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function pause has the same name as a MATLAB builtin. We suggest you rename the function
to avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
[Warning: Function simulink has the same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.]
[> In verifyCode>evaluateCode at 238
  In verifyCode at 43
  In fevalJSON at 14]
2
Pass
 
%%
n = 200;
score = main(n)
score = 4056 - score;
assert(score < (4056 - 256))
assignin('caller','score',score)
out =
  690.7200
score =
  690.7200