{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-16T00:12:35.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-16T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":2265,"title":"2048 Next Move","description":"Given a board in the game 2048 (see the game here: \u003chttp://gabrielecirulli.github.io/2048/ 2048\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\r\n\r\nMove and merge blocks as required by the game's rules, but for simplicity *do not* insert a new 2.\r\n\r\nExample 1\r\n\r\n board = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16]\r\n dir = 'up'\r\n newBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ]\r\n\r\nExample 2\r\n\r\n board = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ]\r\n dir = 'right'\r\n newBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ]\r\n\r\nExample 3. Resolving Ambiguity\r\n\r\nIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \"lowest\" blocks. See below.\r\n\r\n board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\r\n\r\nInspired by a suggestion from \u003chttp://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe Nick Howe\u003e.","description_html":"\u003cp\u003eGiven a board in the game 2048 (see the game here: \u003ca href = \"http://gabrielecirulli.github.io/2048/\"\u003e2048\u003c/a\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\u003c/p\u003e\u003cp\u003eMove and merge blocks as required by the game's rules, but for simplicity \u003cb\u003edo not\u003c/b\u003e insert a new 2.\u003c/p\u003e\u003cp\u003eExample 1\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16]\r\n dir = 'up'\r\n newBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ]\u003c/pre\u003e\u003cp\u003eExample 2\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ]\r\n dir = 'right'\r\n newBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ]\u003c/pre\u003e\u003cp\u003eExample 3. Resolving Ambiguity\u003c/p\u003e\u003cp\u003eIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \"lowest\" blocks. See below.\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\u003c/pre\u003e\u003cp\u003eInspired by a suggestion from \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe\"\u003eNick Howe\u003c/a\u003e.\u003c/p\u003e","function_template":"function newBoard = nextMove(board,dir)\r\n  newBoard = zeros(4);\r\nend","test_suite":"%%\r\nboard = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16];\r\ndir = 'up';\r\nnewBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\nboard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ];\r\ndir = 'right';\r\nnewBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\nboard = [ ...\r\n    0     8     0     4\r\n    2    64    16     2\r\n    2     8     8    16\r\n    4     2     2     4 ];\r\ndir = 'left';\r\nnewBoard = [ ...\r\n    8     4     0     0\r\n    2    64    16     2\r\n    2    16    16     0\r\n    4     4     4     0 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\n board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n","published":true,"deleted":false,"likes_count":10,"comments_count":12,"created_by":7,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":93,"test_suite_updated_at":"2016-03-03T15:27:04.000Z","rescore_all_solutions":false,"group_id":33,"created_at":"2014-03-31T20:55:49.000Z","updated_at":"2026-02-07T15:56:54.000Z","published_at":"2014-03-31T20:55:59.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a board in the game 2048 (see the game here:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://gabrielecirulli.github.io/2048/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e2048\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMove and merge blocks as required by the game's rules, but for simplicity\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003edo not\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e insert a new 2.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     0     0     2\\n    0     0     4     4\\n    0     0     0    16\\n    0     0     0    16]\\n dir = 'up'\\n newBoard = [ ...\\n    0     0     4     2\\n    0     0     0     4\\n    0     0     0    32\\n    0     0     0     0 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     2   128     4\\n    0    16     4    32\\n    0     8     0     0\\n    0     0     2     0 ]\\n dir = 'right'\\n newBoard = [ ...\\n    0     2   128     4\\n    0    16     4    32\\n    0     0     0     8\\n    0     0     0     2 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 3. Resolving Ambiguity\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \\\"lowest\\\" blocks. See below.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     4     4     4\\n    4     4     4     0\\n    2     2     2     2\\n    0     0     0     0 ]\\n dir = 'left'\\n newBoard = [ ...\\n    8     4     0     0\\n    8     4     0     0\\n    4     4     0     0\\n    0     0     0     0 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInspired by a suggestion from\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eNick Howe\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2266,"title":"2048 tile game","description":"The popular 2048 game has been implemented here:\r\n\r\nhttp://gabrielecirulli.github.io/2048/\r\n\r\nGiven the board like this:\r\n\r\n  [2 4 8 0\r\n   4 0 0 0\r\n   2 0 0 0\r\n   2 0 4 0]\r\n\r\nYou give the direction\r\n\r\n1 (left)\r\n2 (up)\r\n3 (right)\r\n4 (down)\r\n\r\nThe system here will keep calling your solver.  Score for each board is the biggest tile achieved.  The Cody score is the maximum board score plus the average score across all 200 boards.","description_html":"\u003cp\u003eThe popular 2048 game has been implemented here:\u003c/p\u003e\u003cp\u003e\u003ca href = \"http://gabrielecirulli.github.io/2048/\"\u003ehttp://gabrielecirulli.github.io/2048/\u003c/a\u003e\u003c/p\u003e\u003cp\u003eGiven the board like this:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[2 4 8 0\r\n 4 0 0 0\r\n 2 0 0 0\r\n 2 0 4 0]\r\n\u003c/pre\u003e\u003cp\u003eYou give the direction\u003c/p\u003e\u003cp\u003e1 (left)\r\n2 (up)\r\n3 (right)\r\n4 (down)\u003c/p\u003e\u003cp\u003eThe system here will keep calling your solver.  Score for each board is the biggest tile achieved.  The Cody score is the maximum board score plus the average score across all 200 boards.\u003c/p\u003e","function_template":"function direction = getMove(board)\r\n  direction = 1;\r\nend","test_suite":"%%\r\nfh=fopen('main.m','wt');\r\nfprintf(fh, '%s \\n', 'function out = main(n)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'result = 0;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'for i = 1:n    ');\r\nfprintf(fh, '%s \\n', '    board = zeros(4);');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', '    %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    direction = 1;');\r\nfprintf(fh, '%s \\n', '    while (direction ~= 0)');\r\nfprintf(fh, '%s \\n', '        %pause(0.1)');\r\nfprintf(fh, '%s \\n', '        %clc');\r\nfprintf(fh, '%s \\n', '        direction = getMove(board);');\r\nfprintf(fh, '%s \\n', '        board = updateBoard(board, direction);');\r\nfprintf(fh, '%s \\n', '        %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    end');\r\nfprintf(fh, '%s \\n', '    %figure(1)');\r\nfprintf(fh, '%s \\n', '    %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    %figure(2)');\r\nfprintf(fh, '%s \\n', '    %hist(result,[2 4 8 16 32 64 128 256 512 1024,2048])');\r\nfprintf(fh, '%s \\n', '    %drawnow');\r\nfprintf(fh, '%s \\n', '    result(i) = max(board(:));');\r\nfprintf(fh, '%s \\n', '    %disp([i result(i) max(result)])');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = max(result) + mean(result)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = collapse(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'in = squish(in);');\r\nfprintf(fh, '%s \\n', 'for i = 1:3');\r\nfprintf(fh, '%s \\n', '    result = miniCollapse(in(i:i+1));');\r\nfprintf(fh, '%s \\n', '    out(i:i+1) = result;');\r\nfprintf(fh, '%s \\n', '     in(i:i+1) = result;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = squish(in);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = squish(in);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = in(in ~= 0);');\r\nfprintf(fh, '%s \\n', 'if numel(out) ~= 4');\r\nfprintf(fh, '%s \\n', '    out(4) = 0;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = miniCollapse(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if in(1) == in(2)');\r\nfprintf(fh, '%s \\n', '    out(1) = in(1) * 2;');\r\nfprintf(fh, '%s \\n', '    out(2)  = 0;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    out = in;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = saltBoard(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if nnz(in) == 16');\r\nfprintf(fh, '%s \\n', '    out = nan;');\r\nfprintf(fh, '%s \\n', '    return;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'vi = find(in == 0);');\r\nfprintf(fh, '%s \\n', 'selected = randi(numel(vi));');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'thresh = 0.1;');\r\nfprintf(fh, '%s \\n', 'if (rand \u003c thresh)');\r\nfprintf(fh, '%s \\n', '    salt = 4;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    salt = 2;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = in;');\r\nfprintf(fh, '%s \\n', 'out(vi(selected)) = salt;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function board = updateBoard(board, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'boardOriginal = board;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'board = collapseBoard(board,direction);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if ~isequal(boardOriginal, board)');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function dispBoard(board)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'clf');\r\nfprintf(fh, '%s \\n', 'r = 1;');\r\nfprintf(fh, '%s \\n', 'c = 1;');\r\nfprintf(fh, '%s \\n', 'v = 2;');\r\nfprintf(fh, '%s \\n', 'for r = 1:4');\r\nfprintf(fh, '%s \\n', '    for c = 1:4');\r\nfprintf(fh, '%s \\n', '        v = board(r,c);');\r\nfprintf(fh, '%s \\n', '        dispSquare(r,c,v)');\r\nfprintf(fh, '%s \\n', '    end');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function dispSquare(r,c,v)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'cmap = autumn(12);');\r\nfprintf(fh, '%s \\n', 'absIndex = (r-1)*4 + c;');\r\nfprintf(fh, '%s \\n', 'if v == 0 ');\r\nfprintf(fh, '%s \\n', '    cMapIndex = 1;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    cMapIndex = log2(v) + 1;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'h = subplot(4,4,absIndex);');\r\nfprintf(fh, '%s \\n', 'set(h,''xtick'',[])');\r\nfprintf(fh, '%s \\n', 'set(h,''ytick'',[])');\r\nfprintf(fh, '%s \\n', 'set(h,''color'',cmap(cMapIndex,:))');\r\nfprintf(fh, '%s \\n', '%axis off');\r\nfprintf(fh, '%s \\n', 'text(0.5,0.5,num2str(v), ''fontsize'', 20)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function flag = isMoveDirection(board, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'originalBoard = board;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'board = updateBoard(board, direction);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'flag = ~isequal(board, originalBoard);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = collapseBoard(in, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'rotDirection = direction-1;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'in = rot90(in,rotDirection);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'for r = 1:4');\r\nfprintf(fh, '%s \\n', '    out(r,:) = collapse(in(r,:));');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = rot90(out,-rotDirection);');\r\n\r\nfclose(fh);\r\n\r\nrehash path\r\n%%\r\nn = 200;\r\nscore = main(n)\r\nscore = 4056 - score;\r\n\r\nassert(score \u003c (4056 - 256))\r\nassignin('caller','score',score)","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":240,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2014-04-02T17:36:53.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-04-01T20:01:55.000Z","updated_at":"2026-02-03T10:02:13.000Z","published_at":"2014-04-02T14:29:03.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe popular 2048 game has been implemented here:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://gabrielecirulli.github.io/2048/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://gabrielecirulli.github.io/2048/\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven the board like this:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[2 4 8 0\\n 4 0 0 0\\n 2 0 0 0\\n 2 0 4 0]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou give the direction\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 (left) 2 (up) 3 (right) 4 (down)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe system here will keep calling your solver. Score for each board is the biggest tile achieved. The Cody score is the maximum board score plus the average score across all 200 boards.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":2265,"title":"2048 Next Move","description":"Given a board in the game 2048 (see the game here: \u003chttp://gabrielecirulli.github.io/2048/ 2048\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\r\n\r\nMove and merge blocks as required by the game's rules, but for simplicity *do not* insert a new 2.\r\n\r\nExample 1\r\n\r\n board = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16]\r\n dir = 'up'\r\n newBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ]\r\n\r\nExample 2\r\n\r\n board = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ]\r\n dir = 'right'\r\n newBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ]\r\n\r\nExample 3. Resolving Ambiguity\r\n\r\nIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \"lowest\" blocks. See below.\r\n\r\n board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\r\n\r\nInspired by a suggestion from \u003chttp://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe Nick Howe\u003e.","description_html":"\u003cp\u003eGiven a board in the game 2048 (see the game here: \u003ca href = \"http://gabrielecirulli.github.io/2048/\"\u003e2048\u003c/a\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\u003c/p\u003e\u003cp\u003eMove and merge blocks as required by the game's rules, but for simplicity \u003cb\u003edo not\u003c/b\u003e insert a new 2.\u003c/p\u003e\u003cp\u003eExample 1\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16]\r\n dir = 'up'\r\n newBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ]\u003c/pre\u003e\u003cp\u003eExample 2\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ]\r\n dir = 'right'\r\n newBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ]\u003c/pre\u003e\u003cp\u003eExample 3. Resolving Ambiguity\u003c/p\u003e\u003cp\u003eIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \"lowest\" blocks. See below.\u003c/p\u003e\u003cpre\u003e board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\u003c/pre\u003e\u003cp\u003eInspired by a suggestion from \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe\"\u003eNick Howe\u003c/a\u003e.\u003c/p\u003e","function_template":"function newBoard = nextMove(board,dir)\r\n  newBoard = zeros(4);\r\nend","test_suite":"%%\r\nboard = [ ...\r\n    0     0     0     2\r\n    0     0     4     4\r\n    0     0     0    16\r\n    0     0     0    16];\r\ndir = 'up';\r\nnewBoard = [ ...\r\n    0     0     4     2\r\n    0     0     0     4\r\n    0     0     0    32\r\n    0     0     0     0 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\nboard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     8     0     0\r\n    0     0     2     0 ];\r\ndir = 'right';\r\nnewBoard = [ ...\r\n    0     2   128     4\r\n    0    16     4    32\r\n    0     0     0     8\r\n    0     0     0     2 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\nboard = [ ...\r\n    0     8     0     4\r\n    2    64    16     2\r\n    2     8     8    16\r\n    4     2     2     4 ];\r\ndir = 'left';\r\nnewBoard = [ ...\r\n    8     4     0     0\r\n    2    64    16     2\r\n    2    16    16     0\r\n    4     4     4     0 ];\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n%%\r\n board = [ ...\r\n    0     4     4     4\r\n    4     4     4     0\r\n    2     2     2     2\r\n    0     0     0     0 ]\r\n dir = 'left'\r\n newBoard = [ ...\r\n    8     4     0     0\r\n    8     4     0     0\r\n    4     4     0     0\r\n    0     0     0     0 ]\r\nassert(isequal(nextMove(board,dir),newBoard))\r\n\r\n","published":true,"deleted":false,"likes_count":10,"comments_count":12,"created_by":7,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":93,"test_suite_updated_at":"2016-03-03T15:27:04.000Z","rescore_all_solutions":false,"group_id":33,"created_at":"2014-03-31T20:55:49.000Z","updated_at":"2026-02-07T15:56:54.000Z","published_at":"2014-03-31T20:55:59.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a board in the game 2048 (see the game here:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://gabrielecirulli.github.io/2048/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e2048\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e) and a direction ('up','down','left', or 'right'), move the game forward one turn.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMove and merge blocks as required by the game's rules, but for simplicity\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003edo not\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e insert a new 2.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     0     0     2\\n    0     0     4     4\\n    0     0     0    16\\n    0     0     0    16]\\n dir = 'up'\\n newBoard = [ ...\\n    0     0     4     2\\n    0     0     0     4\\n    0     0     0    32\\n    0     0     0     0 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     2   128     4\\n    0    16     4    32\\n    0     8     0     0\\n    0     0     2     0 ]\\n dir = 'right'\\n newBoard = [ ...\\n    0     2   128     4\\n    0    16     4    32\\n    0     0     0     8\\n    0     0     0     2 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 3. Resolving Ambiguity\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two \\\"lowest\\\" blocks. See below.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ board = [ ...\\n    0     4     4     4\\n    4     4     4     0\\n    2     2     2     2\\n    0     0     0     0 ]\\n dir = 'left'\\n newBoard = [ ...\\n    8     4     0     0\\n    8     4     0     0\\n    4     4     0     0\\n    0     0     0     0 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInspired by a suggestion from\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/players/17831-nicholas-howe\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eNick Howe\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2266,"title":"2048 tile game","description":"The popular 2048 game has been implemented here:\r\n\r\nhttp://gabrielecirulli.github.io/2048/\r\n\r\nGiven the board like this:\r\n\r\n  [2 4 8 0\r\n   4 0 0 0\r\n   2 0 0 0\r\n   2 0 4 0]\r\n\r\nYou give the direction\r\n\r\n1 (left)\r\n2 (up)\r\n3 (right)\r\n4 (down)\r\n\r\nThe system here will keep calling your solver.  Score for each board is the biggest tile achieved.  The Cody score is the maximum board score plus the average score across all 200 boards.","description_html":"\u003cp\u003eThe popular 2048 game has been implemented here:\u003c/p\u003e\u003cp\u003e\u003ca href = \"http://gabrielecirulli.github.io/2048/\"\u003ehttp://gabrielecirulli.github.io/2048/\u003c/a\u003e\u003c/p\u003e\u003cp\u003eGiven the board like this:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[2 4 8 0\r\n 4 0 0 0\r\n 2 0 0 0\r\n 2 0 4 0]\r\n\u003c/pre\u003e\u003cp\u003eYou give the direction\u003c/p\u003e\u003cp\u003e1 (left)\r\n2 (up)\r\n3 (right)\r\n4 (down)\u003c/p\u003e\u003cp\u003eThe system here will keep calling your solver.  Score for each board is the biggest tile achieved.  The Cody score is the maximum board score plus the average score across all 200 boards.\u003c/p\u003e","function_template":"function direction = getMove(board)\r\n  direction = 1;\r\nend","test_suite":"%%\r\nfh=fopen('main.m','wt');\r\nfprintf(fh, '%s \\n', 'function out = main(n)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'result = 0;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'for i = 1:n    ');\r\nfprintf(fh, '%s \\n', '    board = zeros(4);');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', '    %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    direction = 1;');\r\nfprintf(fh, '%s \\n', '    while (direction ~= 0)');\r\nfprintf(fh, '%s \\n', '        %pause(0.1)');\r\nfprintf(fh, '%s \\n', '        %clc');\r\nfprintf(fh, '%s \\n', '        direction = getMove(board);');\r\nfprintf(fh, '%s \\n', '        board = updateBoard(board, direction);');\r\nfprintf(fh, '%s \\n', '        %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    end');\r\nfprintf(fh, '%s \\n', '    %figure(1)');\r\nfprintf(fh, '%s \\n', '    %dispBoard(board)');\r\nfprintf(fh, '%s \\n', '    %figure(2)');\r\nfprintf(fh, '%s \\n', '    %hist(result,[2 4 8 16 32 64 128 256 512 1024,2048])');\r\nfprintf(fh, '%s \\n', '    %drawnow');\r\nfprintf(fh, '%s \\n', '    result(i) = max(board(:));');\r\nfprintf(fh, '%s \\n', '    %disp([i result(i) max(result)])');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = max(result) + mean(result)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = collapse(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'in = squish(in);');\r\nfprintf(fh, '%s \\n', 'for i = 1:3');\r\nfprintf(fh, '%s \\n', '    result = miniCollapse(in(i:i+1));');\r\nfprintf(fh, '%s \\n', '    out(i:i+1) = result;');\r\nfprintf(fh, '%s \\n', '     in(i:i+1) = result;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = squish(in);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = squish(in);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = in(in ~= 0);');\r\nfprintf(fh, '%s \\n', 'if numel(out) ~= 4');\r\nfprintf(fh, '%s \\n', '    out(4) = 0;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = miniCollapse(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if in(1) == in(2)');\r\nfprintf(fh, '%s \\n', '    out(1) = in(1) * 2;');\r\nfprintf(fh, '%s \\n', '    out(2)  = 0;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    out = in;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = saltBoard(in)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if nnz(in) == 16');\r\nfprintf(fh, '%s \\n', '    out = nan;');\r\nfprintf(fh, '%s \\n', '    return;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'vi = find(in == 0);');\r\nfprintf(fh, '%s \\n', 'selected = randi(numel(vi));');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'thresh = 0.1;');\r\nfprintf(fh, '%s \\n', 'if (rand \u003c thresh)');\r\nfprintf(fh, '%s \\n', '    salt = 4;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    salt = 2;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = in;');\r\nfprintf(fh, '%s \\n', 'out(vi(selected)) = salt;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function board = updateBoard(board, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'boardOriginal = board;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'board = collapseBoard(board,direction);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'if ~isequal(boardOriginal, board)');\r\nfprintf(fh, '%s \\n', '    board = saltBoard(board);');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function dispBoard(board)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'clf');\r\nfprintf(fh, '%s \\n', 'r = 1;');\r\nfprintf(fh, '%s \\n', 'c = 1;');\r\nfprintf(fh, '%s \\n', 'v = 2;');\r\nfprintf(fh, '%s \\n', 'for r = 1:4');\r\nfprintf(fh, '%s \\n', '    for c = 1:4');\r\nfprintf(fh, '%s \\n', '        v = board(r,c);');\r\nfprintf(fh, '%s \\n', '        dispSquare(r,c,v)');\r\nfprintf(fh, '%s \\n', '    end');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function dispSquare(r,c,v)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'cmap = autumn(12);');\r\nfprintf(fh, '%s \\n', 'absIndex = (r-1)*4 + c;');\r\nfprintf(fh, '%s \\n', 'if v == 0 ');\r\nfprintf(fh, '%s \\n', '    cMapIndex = 1;');\r\nfprintf(fh, '%s \\n', 'else');\r\nfprintf(fh, '%s \\n', '    cMapIndex = log2(v) + 1;');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'h = subplot(4,4,absIndex);');\r\nfprintf(fh, '%s \\n', 'set(h,''xtick'',[])');\r\nfprintf(fh, '%s \\n', 'set(h,''ytick'',[])');\r\nfprintf(fh, '%s \\n', 'set(h,''color'',cmap(cMapIndex,:))');\r\nfprintf(fh, '%s \\n', '%axis off');\r\nfprintf(fh, '%s \\n', 'text(0.5,0.5,num2str(v), ''fontsize'', 20)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function flag = isMoveDirection(board, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'originalBoard = board;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'board = updateBoard(board, direction);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'flag = ~isequal(board, originalBoard);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'function out = collapseBoard(in, direction)');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'rotDirection = direction-1;');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'in = rot90(in,rotDirection);');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'for r = 1:4');\r\nfprintf(fh, '%s \\n', '    out(r,:) = collapse(in(r,:));');\r\nfprintf(fh, '%s \\n', 'end');\r\nfprintf(fh, '%s \\n', '');\r\nfprintf(fh, '%s \\n', 'out = rot90(out,-rotDirection);');\r\n\r\nfclose(fh);\r\n\r\nrehash path\r\n%%\r\nn = 200;\r\nscore = main(n)\r\nscore = 4056 - score;\r\n\r\nassert(score \u003c (4056 - 256))\r\nassignin('caller','score',score)","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":240,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":"2014-04-02T17:36:53.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-04-01T20:01:55.000Z","updated_at":"2026-02-03T10:02:13.000Z","published_at":"2014-04-02T14:29:03.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe popular 2048 game has been implemented here:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://gabrielecirulli.github.io/2048/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://gabrielecirulli.github.io/2048/\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven the board like this:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[2 4 8 0\\n 4 0 0 0\\n 2 0 0 0\\n 2 0 4 0]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou give the direction\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 (left) 2 (up) 3 (right) 4 (down)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe system here will keep calling your solver. Score for each board is the biggest tile achieved. The Cody score is the maximum board score plus the average score across all 200 boards.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"2048\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"2048\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"2048\"","","\"","2048","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaa20\u003e":null,"#\u003cMathWorks::Search::Field:0x00007faf50dda980\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007faf50dda0c0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaca0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007faf50ddac00\u003e":50,"#\u003cMathWorks::Search::Field:0x00007faf50ddab60\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007faf50ddaac0\u003e":"tag:\"2048\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaac0\u003e":"tag:\"2048\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"2048\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"2048\"","","\"","2048","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaa20\u003e":null,"#\u003cMathWorks::Search::Field:0x00007faf50dda980\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007faf50dda0c0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaca0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007faf50ddac00\u003e":50,"#\u003cMathWorks::Search::Field:0x00007faf50ddab60\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007faf50ddaac0\u003e":"tag:\"2048\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007faf50ddaac0\u003e":"tag:\"2048\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":2265,"difficulty_rating":"medium"},{"id":2266,"difficulty_rating":"medium-hard"}]}}