{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-06-05T00:10:21.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-06-05T00: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":94,"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-04-23T18:24:29.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":13,"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-05-24T21:25:06.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":{"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":94,"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-04-23T18:24:29.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":13,"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-05-24T21:25:06.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\"}]}"}],"errors":[],"facets":[[{"value":"Computer Games I","count":1,"selected":false},{"value":"Computer Games II","count":1,"selected":false}],[{"value":"medium","count":2,"selected":false}]],"term":"tag:\"2048\"","page":1,"per_page":50,"sort":"map(difficulty_value,0,0,999) asc"}}