{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.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":"2025-12-14T00: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":44757,"title":"Lights Out 6 - 5x5, 13 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require 13 moves to solve. For example, if\r\n\r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\n\r\nan answer is:\r\n\r\n moves = [1:5 8 13 18 21:25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44756 5x5, 10 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves 5x5, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require 13 moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\u003c/pre\u003e\u003cp\u003ean answer is:\u003c/p\u003e\u003cpre\u003e moves = [1:5 8 13 18 21:25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44756\"\u003e5x5, 10 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves\"\u003e5x5, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_6(board) % 5x5 board, 13 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_6(board); % [1:5 8 13 18 21:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board); % [1:13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_6(board); % [1:2:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 1 1 1 0  \r\n          1 1 0 0 0  \r\n          1 1 0 1 0  \r\n          0 1 0 0 1  \r\n          1 0 1 1 0];\r\nmoves = lights_out_6(board); % [1:3 6 8:11 16:19 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 0 1 1 1  \r\n          0 0 0 0 1  \r\n          0 0 1 0 1  \r\n          1 0 0 0 0];\r\nmoves = lights_out_6(board); % [1 4 6:9 12:16 19 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 1 0 0  \r\n          0 1 0 1 1];\r\nmoves = lights_out_6(board); % [1 3 9 11 14 16 19 20:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_6(board); % [1:2 4:6 10 13 16 20:22 24:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          1 1 1 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 1 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 1 0 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 1 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          1 1 0 1 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 0 0 0  \r\n          0 1 1 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 0 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":16,"test_suite_updated_at":"2018-11-15T13:42:34.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T19:15:14.000Z","updated_at":"2025-11-29T13:54:32.000Z","published_at":"2018-11-15T13:38:34.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require 13 moves to solve. For example, if\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 = [0 1 0 1 0  \\n          1 0 1 0 1  \\n          0 1 1 1 0  \\n          1 0 1 0 1  \\n          0 1 0 1 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\u003ean answer is:\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[ moves = [1:5 8 13 18 21:25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44756\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 10 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44756,"title":"Lights Out 5 - 5x5, 10 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require ten moves to solve. For example, if\r\n\r\n board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1]\r\n\r\nan answer is:\r\n\r\n moves = [1 2 3 4 5 16 17 18 19 20]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves 5x5, 8 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves 5x5, 13 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require ten moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1]\u003c/pre\u003e\u003cp\u003ean answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 2 3 4 5 16 17 18 19 20]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\"\u003e5x5, 8 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves\"\u003e5x5, 13 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_5(board) % 5x5 board, 10 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1];\r\nmoves = lights_out_5(board); % [1 2 3 4 5 16 17 18 19 20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 0 1  \r\n          0 0 1 0 1];\r\nmoves = lights_out_5(board); % [1 2 3 11 13 14 16 17 21 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_5(board); % [1 2 3 4 6 7 8 11 12 16]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          0 0 1 1 0  \r\n          1 1 0 1 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_5(board); % [3 6:7 11 13:15 19 22:23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 0 1 0  \r\n          1 0 1 1 0];\r\nmoves = lights_out_5(board); % [2 3 9 10 14 16 17 20 23 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 1 0 0 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 1 0 1];\r\nmoves = lights_out_5(board); % [2 4 7 9 11 12 17 19 20 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 0 1 1  \r\n          1 0 1 0 0  \r\n          1 0 1 0 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 1];\r\nmoves = lights_out_5(board); % [1 4 6 12 14 15 18 21 23 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          0 0 0 1 1  \r\n          1 1 0 0 0  \r\n          1 0 0 1 0  \r\n          1 1 1 1 0];\r\nmoves = lights_out_5(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 1  \r\n          1 1 0 0 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 1 1 1 0  \r\n          0 1 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 1 0  \r\n          0 1 1 1 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          0 0 0 1 1  \r\n          1 0 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 1 0 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 0 1 1 0  \r\n          0 1 0 0 0  \r\n          0 1 1 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          0 0 0 1 1  \r\n          0 1 1 0 0  \r\n          1 1 1 1 0  \r\n          0 0 1 1 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)","published":true,"deleted":false,"likes_count":3,"comments_count":8,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2018-11-13T13:07:28.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T18:53:12.000Z","updated_at":"2025-11-29T13:41:10.000Z","published_at":"2018-11-12T15:53:32.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require ten moves to solve. For example, if\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 = [0 1 1 0 1  \\n          1 1 1 1 1  \\n          1 1 1 1 1  \\n          1 1 1 1 1  \\n          0 1 1 0 1]]]\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\u003ean answer is:\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[ moves = [1 2 3 4 5 16 17 18 19 20]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 8 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 13 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44759,"title":"Lights Out 7 - 5x5, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44757 5x5, 13 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i 5x5, light-only solution? I\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44757\"\u003e5x5, 13 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i\"\u003e5x5, light-only solution? I\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_7(board) % 5x5 board, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 0 0 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_7(board); % [13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 0 1 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_7(board); % [7 13 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_7(board); % [7:9 12:14 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board); % [7 9 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_7(board); % [1 5 7 9 13 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0];\r\nmoves = lights_out_7(board); % [1:5 11:15 21:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          0 0 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_7(board); % [18:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_7(board); % [1:6 10:11 15:16 20:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_7(board); % [1:2 4:7 9:10 16:17 19:22 24:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 0 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_7(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          0 1 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 1 0  \r\n          0 1 1 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 0 1 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-30T12:52:15.000Z","updated_at":"2025-11-29T15:18:21.000Z","published_at":"2018-12-03T13:24:38.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44757\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 13 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? I\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44755,"title":"Lights Out 4 - 5x5, 8 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require eight moves to solve. For example, if\r\n\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0]\r\n\r\nthe answer is:\r\n\r\n moves = [2 4 6 10 16 20 22 24]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves 5x5, 6 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves 5x5, 10 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require eight moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 4 6 10 16 20 22 24]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\"\u003e5x5, 6 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves\"\u003e5x5, 10 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_4(board) % 5x5 board, 8 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_4(board); % [2 4 6 10 16 20 22 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board); % [7 8 9 12 14 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 1 0  \r\n          0 0 1 1 0];\r\nmoves = lights_out_4(board); % [1 2 5 10 16 21 24 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_4(board); % [1 5 7 9 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board); % [4 5 8 12 14 19 22 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_4(board); % [1 2 3 4 5 7 9 13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_4(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 1 1  \r\n          1 1 1 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 0  \r\n          1 0 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 1 0 0 0  \r\n          0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 1 0 1 0  \r\n          1 0 1 1 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 1  \r\n          1 1 0 1 0  \r\n          0 1 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 1 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 1 0 0 1  \r\n          0 1 0 1 1  \r\n          1 0 1 1 0  \r\n          0 1 0 0 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          1 0 0 0 1  \r\n          0 0 1 1 1  \r\n          0 1 0 1 1  \r\n          1 0 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2018-11-09T14:19:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T18:36:00.000Z","updated_at":"2025-11-29T14:28:48.000Z","published_at":"2018-11-09T14:19:17.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require eight moves to solve. For example, if\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 = [0 1 0 1 0  \\n          1 0 0 0 1  \\n          0 0 0 0 0  \\n          1 0 0 0 1  \\n          0 1 0 1 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\u003ethe answer is:\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[ moves = [2 4 6 10 16 20 22 24]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 10 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44760,"title":"Lights Out 8 - 5x5, light-only solution? I","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44759 5x5, any number of moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44761 5x5, light-only solution? II\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44759\"\u003e5x5, any number of moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44761\"\u003e5x5, light-only solution? II\u003c/a\u003e\u003c/p\u003e","function_template":"function tf = lights_out_8(board) % 5x5 board, lights-only solution\r\n tf = 0;\r\nend","test_suite":"%% all true cases first\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nassert(lights_out_8(board)); % [2 4 6 10 16 20 22 24]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nassert(lights_out_8(board)); % [1 5 7 9 17 19 21 25]\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nassert(lights_out_8(board)); % [2 6 8 12 14 18 20 24]\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nassert(lights_out_8(board)); % [1:2 4:7 9:10 16:17 19:22 24:25]\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_8(board)); % [3 7 9 11 13 15 17 19 23]\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1];\r\nassert(lights_out_8(board)); % [1 3 5 11 13 15 21 23 25]\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_8(board)); % [2:4 6:7 9:11 13 15:17 19:20 22:24]\r\n\r\n\r\n%% false cases start here\r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nassert(~lights_out_8(board)); % [1 2 3 4 5 7 9 13]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 0 0 0];\r\nassert(~lights_out_8(board)); % [1 2 3 4 6 7 8 11 12 16]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          1 0 0 0 1];\r\nassert(~lights_out_8(board)); % on your own\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 1 1 0 0];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0];\r\nassert(~lights_out_8(board));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":16,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":52,"created_at":"2018-10-30T14:01:47.000Z","updated_at":"2025-11-29T15:01:02.000Z","published_at":"2019-01-09T15:04:36.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44759\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, any number of moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44761\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? II\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44753,"title":"Lights Out 3 - 5x5, 6 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require six moves to solve. For example, if\r\n\r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [1 5 11 15 21 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves 5x5, 4 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves 5x5, 8 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require six moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 5 11 15 21 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\"\u003e5x5, 4 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\"\u003e5x5, 8 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_3(board) % 5x5 board, 6 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_3(board); % [1 5 11 15 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_3(board); % [4 9 10 16 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_3(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 0 1 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_3(board); % [4 8 11 13 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 1 0];\r\nmoves = lights_out_3(board); % [7 8 12 14 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 1 1 0  \r\n          0 1 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_3(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 1 0 1  \r\n          0 1 0 0 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 1 1  \r\n          1 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 1  \r\n          0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 0 0 0  \r\n          1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 0 0 1  \r\n          0 1 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":10,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":20,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T14:59:29.000Z","updated_at":"2025-11-29T15:04:22.000Z","published_at":"2018-11-05T13:04:28.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require six moves to solve. For example, if\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 = [1 0 1 0 1  \\n          1 0 1 0 1  \\n          0 0 0 0 0  \\n          1 0 1 0 1  \\n          1 0 1 0 1]]]\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\u003ethe answer is:\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[ moves = [1 5 11 15 21 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 8 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44751,"title":"Lights Out 1 - 5x5, 3 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\r\n\r\nThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\r\n\r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0]\r\n\r\nthe answer is:\r\n\r\n moves = [5 7 23]\r\n\r\nIn this first problem, all boards have solutions of only three moves.\r\n\r\nNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\r\n\r\nAs a potential starting point, you might check out one or more of the following resources by \u003chttp://www.mat.ucm.es/~vmunozve/lights-out.pdf Vicente Muñoz\u003e; \u003chttp://www.ijritcc.org/download/browse/Volume_5_Issues/August_17_Volume_5_Issue_8/1503304082_21-08-2017.pdf Chen, et al.\u003e; \u003chttp://vprusso.github.io/blog/2017/the-mathematics-of-lights-out/ Vincent Russo\u003e; \u003chttp://mathworld.wolfram.com/LightsOutPuzzle.html Margherita Barile (on wolfram.com)\u003e; \u003chttp://www.keithschwarz.com/interesting/code/?dir=lights-out Keith Schwarz\u003e; or \u003chttps://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026httpsredir=1\u0026article=1166\u0026context=theses Rebecca Meyer (a master's thesis)\u003e.\r\n\r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves 5x5, 4 moves\u003e","description_html":"\u003cdiv style = \"text-align: start; line-height: 20px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: normal; text-decoration: none; white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; vertical-align: baseline; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eLights Out\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e board = [0 1 0 0 0\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 1 1 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          0 1 0 1 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 0 0 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 1 0 0 0]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003ethe answer is:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e moves = [5 7 23]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eIn this first problem, all boards have solutions of only three moves.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eAs a potential starting point, you might check out one or more of the following resources by\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eVicente Muñoz\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eChen, et al.\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eVincent Russo\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eMargherita Barile (on wolfram.com)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eKeith Schwarz\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e; or\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026amp;httpsredir=1\u0026amp;article=1166\u0026amp;context=theses\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eRebecca Meyer (a master's thesis)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eNext:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e5x5, 4 moves\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function moves = lights_out_1(board) % 5x5 board, 3 moves\r\n moves = board;\r\nend","test_suite":"\r\n%% \r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0];\r\n %plot_board(board,'Input')\r\nmoves = lights_out_1(board); % should be [5 7 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2);   %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_1(board); %should be [1 13 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_1(board); %should be [2 3 4]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_1(board); %should be [7 12 17]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 1  \r\n          0 0 1 1 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_1(board); %you're on your own now\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 1 0 0];\r\n plot_board(board,'Input',[])\r\nmoves = lights_out_1(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2);\r\n    plot_board(board,['Step ',num2str(i)],moves(i))\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n\r\nfunction plot_board(board,txt_title,sq_tap)\r\n sq_on = (board==1); % determine which squares are on\r\n pts = repmat(1:5,[5,1]); x = pts(1:end); % x coordinates for each square\r\n pts = fliplr(pts)'; y = pts(1:end); % y coordinates for each square\r\n figure; hold on\r\n fill([0,0,5,5],[0,5,5,0],'k','FaceAlpha',0.3) % background\r\n plot(x(sq_on)-0.5,y(sq_on)-0.5,'ys','MarkerSize',50, ...\r\n     'MarkerFaceColor','y') % on squares\r\n plot(x(~sq_on)-0.5,y(~sq_on)-0.5,'s','MarkerSize',50, ...\r\n     'Color',[0.9 0.95 1.0],'MarkerFaceColor',[0.9 0.95 1.0]) % off squares\r\n if ~isempty(sq_tap)\r\n  plot(x(sq_tap)-0.5,y(sq_tap)-0.5,'g*','MarkerSize',40, ...\r\n      'LineWidth',5) % tapped square\r\n end\r\n axis square\r\n set(gca,'XTick',0:5); set(gca,'YTick',0:5)\r\n title(txt_title,'FontSize',24)\r\nend","published":true,"deleted":false,"likes_count":5,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2021-04-13T00:54:00.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-25T19:39:19.000Z","updated_at":"2026-01-06T08:23:35.000Z","published_at":"2018-10-29T14:07:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\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 = [0 1 0 0 0\\n          1 1 1 0 1\\n          0 1 0 1 1\\n          1 0 0 0 1\\n          1 1 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethe answer is:\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[ moves = [5 7 23]]]\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn this first problem, all boards have solutions of only three moves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAs a potential starting point, you might check out one or more of the following resources by\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eVicente Muñoz\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eChen, et al.\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eVincent Russo\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMargherita Barile (on wolfram.com)\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKeith Schwarz\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e; or\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=\\\"https://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026amp;httpsredir=1\u0026amp;article=1166\u0026amp;context=theses\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eRebecca Meyer (a master's thesis)\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\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNext:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":44752,"title":"Lights Out 2 - 5x5, 4 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require four moves to solve. For example, if\r\n\r\n board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [2 5 16 24]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves 5x5, 3 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves 5x5, 6 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require four moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 5 16 24]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003e5x5, 3 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\"\u003e5x5, 6 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_2(board) % 5x5 board, 4 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1];\r\nmoves = lights_out_2(board); % [2 5 16 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_2(board); % [1 2 3 4]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_2(board); % [7 9 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 1 1 0  \r\n          0 1 1 1 1  \r\n          0 1 1 1 1  \r\n          0 0 1 1 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_2(board); % [12 13 17 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_2(board); % [8 12 14 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 0 1  \r\n          0 0 1 1 1  \r\n          0 0 0 1 1  \r\n          0 1 0 0 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_2(board); % on your own now\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T14:43:57.000Z","updated_at":"2025-11-29T15:31:35.000Z","published_at":"2018-10-31T12:25:12.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require four moves to solve. For example, if\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 = [1 0 1 1 1  \\n          1 1 0 1 0  \\n          1 0 0 0 1  \\n          1 0 0 1 1  \\n          1 1 0 0 1]]]\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\u003ethe answer is:\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[ moves = [2 5 16 24]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44761,"title":"Lights Out 9 - 5x5, light-only solution? II","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44760 5x5, light-only solution? I\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44764 5x5, with wrapping, 6 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44760\"\u003e5x5, light-only solution? I\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44764\"\u003e5x5, with wrapping, 6 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function tf = lights_out_9(board) % 5x5 board, lights-only subset solution\r\n tf = 0;\r\nend","test_suite":"%% all true cases first\r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0];\r\nassert(lights_out_9(board)); % [5 7 23]\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 1];\r\nassert(lights_out_9(board)); % [1 13 25]\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [9 15 19]\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nassert(lights_out_9(board)); % [7 9 17 19]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nassert(lights_out_9(board)); % [1 5 7 9 17 19 21 25]\r\n\r\n%% \r\n board = [0 0 1 1 0  \r\n          0 1 1 1 1  \r\n          0 1 1 1 1  \r\n          0 0 1 1 0  \r\n          0 0 0 0 0];\r\nassert(lights_out_9(board)); % [12 13 17 18]\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [8 12 14 18]\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [3 9 15 19]\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1];\r\nassert(lights_out_9(board)); % [1 5 11 15 21 25]\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_9(board)); % [7 8 9 12 14 17 18 19]\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nassert(lights_out_9(board)); % [2 6 8 12 14 18 20 24]\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 0 0 0];\r\nassert(lights_out_9(board)); % on your own\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board));\r\n\r\n\r\n%% false cases start here\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nassert(~lights_out_9(board)); % [7 8 9 17 18 19]\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 1 0  \r\n          0 0 1 1 0];\r\nassert(~lights_out_9(board)); % [1 2 5 10 16 21 24 25]\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          1 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 0 1  \r\n          0 1 0 0 0];\r\nassert(~lights_out_9(board)); % [2 5:6 8:11 17:24]\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 0 0  \r\n          0 0 0 0 0];\r\nassert(~lights_out_9(board)); % on your own\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          1 0 1 0 0  \r\n          1 1 1 1 0  \r\n          0 1 1 0 1  \r\n          0 1 0 1 0];\r\nassert(~lights_out_9(board));\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 0];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          1 0 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 0 1  \r\n          0 1 1 0 1];\r\nassert(~lights_out_9(board));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":4,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":"2019-05-02T12:12:00.000Z","rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-30T14:14:44.000Z","updated_at":"2025-11-29T15:23:35.000Z","published_at":"2019-04-22T15:59:00.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44760\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? I\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44764\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, with wrapping, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44764,"title":"Lights Out 10 - 5x5, with wrapping, 6 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if \r\n\r\n board = [1 0 0 0 1\r\n          1 0 1 0 1\r\n          0 0 0 0 0\r\n          1 0 1 0 1\r\n          1 0 0 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [1 5 11 15 21 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44761 5x5, light-only solution? II\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44765 5x5, wrapping, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 0 0 1\r\n          1 0 1 0 1\r\n          0 0 0 0 0\r\n          1 0 1 0 1\r\n          1 0 0 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 5 11 15 21 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44761\"\u003e5x5, light-only solution? II\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44765\"\u003e5x5, wrapping, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_10(board) % 5x5 board, with wrapping, 6 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1];\r\nmoves = lights_out_10(board); % [1 5 11 15 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          1 0 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 1  \r\n          0 0 1 1 0];\r\nmoves = lights_out_10(board); % [4 9 10 16 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_10(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          1 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 0 0];\r\nmoves = lights_out_10(board); % [4 8 11 13 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 1 1];\r\nmoves = lights_out_10(board); % [7 8 12 14 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          0 1 1 1 1  \r\n          0 1 0 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_10(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 1 0 1  \r\n          1 1 0 0 0  \r\n          1 0 0 1 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          1 0 0 1 1  \r\n          0 0 0 0 1  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          1 0 1 1 0  \r\n          1 1 1 1 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 0 1  \r\n          1 0 0 0 1  \r\n          1 1 1 0 1];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 0 0 1 1  \r\n          0 1 1 0 0  \r\n          1 1 0 0 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":4,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":"2019-04-30T11:31:05.000Z","rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T16:24:02.000Z","updated_at":"2025-11-29T15:08:07.000Z","published_at":"2019-04-22T16:00:51.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if\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 = [1 0 0 0 1\\n          1 0 1 0 1\\n          0 0 0 0 0\\n          1 0 1 0 1\\n          1 0 0 0 1]]]\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\u003ethe answer is:\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[ moves = [1 5 11 15 21 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44761\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? II\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44765\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44766,"title":"Lights Out 12 - 5x5, three stages, \u003c7 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if \r\n\r\n board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 19]\r\n\r\nsince the first \"1\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \"1\" will then change those three values to zero, while the \"19\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44765 5x5, wrapping, any number of moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44767 5x5, 3 stages, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 19]\u003c/pre\u003e\u003cp\u003esince the first \"1\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \"1\" will then change those three values to zero, while the \"19\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44765\"\u003e5x5, wrapping, any number of moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44767\"\u003e5x5, 3 stages, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_12(board) % 5x5 board, three stages, \u003c7 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\r\nmoves = lights_out_12(board); % [1 1 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 2 0 2 2  \r\n          2 0 2 0 2  \r\n          0 2 2 2 0  \r\n          2 0 2 0 2  \r\n          2 2 0 2 2];\r\nmoves = lights_out_12(board); % [1 5 13 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 1 0 1  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 0 0];\r\nmoves = lights_out_12(board); % [5 5 7 7 23 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 2 0 0  \r\n          2 0 2 0 0  \r\n          2 0 2 0 0  \r\n          2 0 2 0 0  \r\n          2 1 2 0 0];\r\nmoves = lights_out_12(board); % [6:10]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          2 0 0 0 0  \r\n          1 1 0 0 0  \r\n          2 0 0 0 0  \r\n          1 1 0 0 0];\r\nmoves = lights_out_12(board); % [1 1 3 3 5 5]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 2 0  \r\n          2 2 0 2 2  \r\n          0 0 2 0 0  \r\n          2 2 0 2 2  \r\n          0 2 0 2 0];\r\nmoves = lights_out_12(board); % [7 9 13 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 2 0  \r\n          2 1 1 1 2  \r\n          2 0 1 0 2  \r\n          2 1 1 1 2  \r\n          0 2 0 2 0];\r\nmoves = lights_out_12(board); % [7:9 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 2 1 2 0  \r\n          2 0 2 0 2  \r\n          0 2 1 2 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board); % [8 13 13 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 2 0 2 1  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board); % [7 7 12 12 17 17]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 1 2  \r\n          0 0 2 2 1  \r\n          0 1 0 2 2  \r\n          0 1 1 2 2  \r\n          2 0 0 0 2];\r\nmoves = lights_out_12(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 1 0 0  \r\n          2 2 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 0 0 0  \r\n          2 1 1 0 0  \r\n          2 0 0 2 0  \r\n          0 1 1 2 0];\r\nmoves = lights_out_12(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T17:08:51.000Z","updated_at":"2025-11-04T21:21:50.000Z","published_at":"2019-05-02T12:25:05.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if\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 = [1 1 0 0 0  \\n          1 0 0 0 0  \\n          0 0 0 2 0  \\n          0 0 2 2 2  \\n          0 0 0 2 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\u003ethe answer is:\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[ moves = [1 1 19]]]\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\u003esince the first \\\"1\\\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \\\"1\\\" will then change those three values to zero, while the \\\"19\\\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44765\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, any number of moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44767\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44776,"title":"Lights Out 15 - 5x5, broken buttons I","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\r\n\r\nFor example, if:\r\n\r\n board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 10 18]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44769 5x5, four stages, x moves\u003e — \r\nNext: [Check back later for new problems in the series.]","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\u003c/p\u003e\u003cp\u003eFor example, if:\u003c/p\u003e\u003cpre\u003e board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 10 18]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44769\"\u003e5x5, four stages, x moves\u003c/a\u003e — \r\nNext: [Check back later for new problems in the series.]\u003c/p\u003e","function_template":"function moves = lights_out_15(board) % 5x5 board, any number of moves, broken buttons I\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_15(board); % [1 10 18]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_15(board); % [7 19]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % [1 5 13 21 25]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0];\r\nmoves = lights_out_15(board); % [6:10]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          1 0 1 1 1  \r\n          1 0 1 0 1  \r\n          1 0 0 1 1  \r\n          0 1 0 1 1];\r\nmoves = lights_out_15(board); % [2 5 7 11:13 19 21 24]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % [2:4 6 10:11 15:16 20 22:24]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % on your own\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 1 0 1 0  \r\n          0 1 0 1 0  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          0 1 0 0 0  \r\n          0 0 1 1 1  \r\n          0 0 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 0 0 0  \r\n          0 0 1 0 1  \r\n          0 1 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 1  \r\n          1 0 0 0 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 1  \r\n          0 1 1 1 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 0 0 1 0  \r\n          0 1 0 1 0  \r\n          0 1 0 0 1  \r\n          0 1 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 0 0 1  \r\n          0 0 0 0 0  \r\n          0 0 1 0 1  \r\n          0 0 1 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0  \r\n          1 0 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-11-06T13:17:12.000Z","updated_at":"2025-11-04T21:41:48.000Z","published_at":"2019-09-16T10:59:35.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\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\u003eFor example, if:\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 = [0 1 0 0 0  \\n          1 0 0 1 0  \\n          0 0 1 0 1  \\n          0 1 0 1 0  \\n          1 0 1 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\u003ethe answer is:\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[ moves = [1 10 18]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44769\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, four stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next: [Check back later for new problems in the series.]\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":44769,"title":"Lights Out 14 - 5x5, four stages, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction and \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 problem 12\u003e for an explanation of three stages.\r\n\r\nThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if \r\n\r\n board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 1 15 15 22]\r\n\r\nUp to three moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44767 5x5, 3 stages, x moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44776 5x5, broken buttons I\u003e.","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction and \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003eproblem 12\u003c/a\u003e for an explanation of three stages.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 1 15 15 22]\u003c/pre\u003e\u003cp\u003eUp to three moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44767\"\u003e5x5, 3 stages, x moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44776\"\u003e5x5, broken buttons I\u003c/a\u003e.\u003c/p\u003e","function_template":"function moves = lights_out_14(board) % 5x5 board, four stages, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0];\r\nmoves = lights_out_14(board); % [1 1 1 15 15 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 3 2 0 0  \r\n          3 1 2 0 0  \r\n          3 1 2 0 0  \r\n          3 1 2 0 0  \r\n          0 3 2 0 0];\r\nmoves = lights_out_14(board); % [1:10 6:10]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 3 0 3 1  \r\n          3 2 3 2 3  \r\n          0 3 3 3 0  \r\n          3 2 3 2 3  \r\n          1 3 0 3 1];\r\nmoves = lights_out_14(board); % [1 1 1 5 5 5 7 7 9 9 13 17 17 19 19 21 21 21 25 25 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 1 1 2  \r\n          1 2 3 2 1  \r\n          1 3 0 3 1  \r\n          1 2 3 2 1  \r\n          2 1 1 1 2];\r\nmoves = lights_out_14(board); % [6:10 16:20 2:5:22 4:5:24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 3 1 3  \r\n          2 2 2 3 2  \r\n          0 2 3 1 2  \r\n          0 0 0 1 0  \r\n          3 2 1 2 1];\r\nmoves = lights_out_14(board); % [2 4 4 4:7 6 10:13 12 17:21 18:20 19 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 1 0  \r\n          2 2 3 1 1  \r\n          0 3 0 3 0  \r\n          1 1 3 2 2  \r\n          0 1 0 2 0];\r\nmoves = lights_out_14(board); % [7 7 9 9 9 17 17 17 19 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 3 1 3 0  \r\n          0 2 2 2 0  \r\n          0 1 1 1 0  \r\n          0 2 2 2 0  \r\n          0 3 1 3 0];\r\nmoves = lights_out_14(board); % [11:15 12:14 13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [3 1 0 0 0  \r\n          1 2 3 0 0  \r\n          0 3 1 3 0  \r\n          0 0 3 2 1  \r\n          0 0 0 1 3];\r\nmoves = lights_out_14(board); % [1 7 7 13 13 13 19 19 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 1 2 1  \r\n          3 0 1 0 3  \r\n          3 2 0 2 2  \r\n          2 2 3 0 1  \r\n          1 3 0 2 3];\r\nmoves = lights_out_14(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          3 2 2 0 0  \r\n          1 1 3 2 0  \r\n          0 2 1 2 1  \r\n          0 0 1 3 1];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [3 2 3 3 0  \r\n          3 1 2 2 3  \r\n          3 0 1 0 2  \r\n          2 3 3 3 1  \r\n          3 1 2 2 3];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 3 0 1  \r\n          1 2 1 1 2  \r\n          1 0 2 0 0  \r\n          1 0 0 0 0  \r\n          0 3 2 2 0];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-31T17:50:31.000Z","updated_at":"2025-11-04T21:35:20.000Z","published_at":"2019-05-06T13:53:48.000Z","restored_at":"2022-02-16T22:15:39.000Z","restored_by":null,"spam":false,"simulink":false,"admin_reviewed":true,"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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction and\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eproblem 12\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an explanation of three stages.\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\u003eThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if\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 = [1 1 0 0 3  \\n          1 0 0 3 3  \\n          0 0 0 0 3  \\n          0 0 2 0 0  \\n          0 2 2 2 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\u003ethe answer is:\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[ moves = [1 1 1 15 15 22]]]\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\u003eUp to three moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44767\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44776\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, broken buttons I\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":44765,"title":"Lights Out 11 - 5x5, with wrapping, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if \r\n\r\n board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1]\r\n\r\nthe answer is:\r\n\r\n moves = [2 4 13 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44764 5x5, wrapping, 6 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 5x5, 3 stages, \u003c7 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 4 13 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44764\"\u003e5x5, wrapping, 6 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_11(board) % 5x5 board, with wrapping, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1];\r\nmoves = lights_out_11(board); % [2 4 13 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_11(board); % [1 5 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_11(board); % [2 6 8 12 14 18 20 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 0 0 1 1  \r\n          1 0 0 1 1];\r\nmoves = lights_out_11(board); % [3 7 17 21 23:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_11(board); % [7:9 12:14 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_11(board); % [11:15]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 1 1 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0];\r\nmoves = lights_out_11(board); % [9 11 14 21 23 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_11(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1];\r\nmoves = lights_out_11(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_11(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T16:37:46.000Z","updated_at":"2025-11-04T20:46:11.000Z","published_at":"2019-05-02T12:20:39.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if\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 = [1 0 0 0 1  \\n          1 1 1 0 1  \\n          0 1 1 1 0  \\n          1 1 1 0 0  \\n          0 0 0 1 1]]]\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\u003ethe answer is:\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[ moves = [2 4 13 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44764\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44767,"title":"Lights Out 13 - 5x5, three stages, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction and \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 problem 12\u003e for an explanation of three stages.\r\n\r\nThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if \r\n\r\n board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 10 15 21]\r\n\r\nUp to two moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 5x5, 3 stages, \u003c7 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44769 5x5, 4 stages, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction and \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003eproblem 12\u003c/a\u003e for an explanation of three stages.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 10 15 21]\u003c/pre\u003e\u003cp\u003eUp to two moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44769\"\u003e5x5, 4 stages, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_13(board) % 5x5 board, three stages, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0];\r\nmoves = lights_out_13(board); % [1 1 10 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 2 0 2 2  \r\n          0 0 2 0 0  \r\n          2 2 2 2 2  \r\n          0 0 2 0 0  \r\n          2 2 0 2 2];\r\nmoves = lights_out_13(board); % [6:2:20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 2 0  \r\n          1 0 2 2 2  \r\n          2 0 0 1 0  \r\n          1 1 2 2 2  \r\n          0 1 2 2 0];\r\nmoves = lights_out_13(board); % [1 1 4 5 10 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 0 1 2  \r\n          1 2 2 2 1  \r\n          0 2 1 2 0  \r\n          1 2 2 2 1  \r\n          2 1 0 1 2];\r\nmoves = lights_out_13(board); % [1 5 7 9 13 13 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 0 2 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          2 0 2 0 0];\r\nmoves = lights_out_13(board); % [6:10 7:9]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 0 2 0 2  \r\n          2 2 1 2 2  \r\n          2 2 1 2 2  \r\n          2 2 1 2 2  \r\n          2 0 2 0 2];\r\nmoves = lights_out_13(board); % [6:20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 2 1 0  \r\n          0 1 2 1 2  \r\n          2 2 2 2 2  \r\n          2 1 1 1 2  \r\n          2 2 0 2 0];\r\nmoves = lights_out_13(board); % [2 2 5 7:8 8 11 13 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 2 0  \r\n          0 2 1 2 2  \r\n          1 2 2 0 1  \r\n          0 1 2 1 1  \r\n          2 1 2 2 1];\r\nmoves = lights_out_13(board); % [3 3 7 9:11 11 16:18 17 21 21 24 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 2  \r\n          0 2 0 1 2  \r\n          1 2 0 2 0  \r\n          2 2 2 0 2  \r\n          1 2 1 0 2];\r\nmoves = lights_out_13(board); % [4 8 10 14 18 22 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 1 2 1 0  \r\n          0 2 0 2 0  \r\n          0 1 2 1 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_13(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 0 2 1  \r\n          2 2 2 2 2  \r\n          0 2 2 2 0  \r\n          2 2 2 2 2  \r\n          1 2 0 2 1];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 2 2 2  \r\n          1 1 1 0 0  \r\n          2 1 1 2 2  \r\n          1 1 1 0 0  \r\n          1 2 2 2 2];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 2 0 2  \r\n          1 1 0 0 2  \r\n          1 1 0 1 1  \r\n          0 1 2 2 2  \r\n          0 2 0 1 0];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-31T17:27:28.000Z","updated_at":"2025-11-04T21:29:01.000Z","published_at":"2019-05-06T13:52:06.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction and\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eproblem 12\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an explanation of three stages.\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\u003eThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if\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 = [1 1 0 2 2  \\n          1 0 0 0 2  \\n          0 0 0 0 0  \\n          0 2 2 0 0  \\n          2 1 1 2 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\u003ethe answer is:\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[ moves = [1 1 10 15 21]]]\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\u003eUp to two moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44769\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44757,"title":"Lights Out 6 - 5x5, 13 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require 13 moves to solve. For example, if\r\n\r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\n\r\nan answer is:\r\n\r\n moves = [1:5 8 13 18 21:25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44756 5x5, 10 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves 5x5, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require 13 moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\u003c/pre\u003e\u003cp\u003ean answer is:\u003c/p\u003e\u003cpre\u003e moves = [1:5 8 13 18 21:25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44756\"\u003e5x5, 10 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves\"\u003e5x5, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_6(board) % 5x5 board, 13 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_6(board); % [1:5 8 13 18 21:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board); % [1:13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_6(board); % [1:2:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 1 1 1 0  \r\n          1 1 0 0 0  \r\n          1 1 0 1 0  \r\n          0 1 0 0 1  \r\n          1 0 1 1 0];\r\nmoves = lights_out_6(board); % [1:3 6 8:11 16:19 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 0 1 1 1  \r\n          0 0 0 0 1  \r\n          0 0 1 0 1  \r\n          1 0 0 0 0];\r\nmoves = lights_out_6(board); % [1 4 6:9 12:16 19 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 1 0 0  \r\n          0 1 0 1 1];\r\nmoves = lights_out_6(board); % [1 3 9 11 14 16 19 20:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_6(board); % [1:2 4:6 10 13 16 20:22 24:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          1 1 1 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 1 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 1 0 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 1 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          1 1 0 1 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 0 0 0  \r\n          0 1 1 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 0 1];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_6(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==13)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":16,"test_suite_updated_at":"2018-11-15T13:42:34.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T19:15:14.000Z","updated_at":"2025-11-29T13:54:32.000Z","published_at":"2018-11-15T13:38:34.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require 13 moves to solve. For example, if\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 = [0 1 0 1 0  \\n          1 0 1 0 1  \\n          0 1 1 1 0  \\n          1 0 1 0 1  \\n          0 1 0 1 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\u003ean answer is:\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[ moves = [1:5 8 13 18 21:25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44756\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 10 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44759-lights-out-7-5x5-x-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44756,"title":"Lights Out 5 - 5x5, 10 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require ten moves to solve. For example, if\r\n\r\n board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1]\r\n\r\nan answer is:\r\n\r\n moves = [1 2 3 4 5 16 17 18 19 20]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves 5x5, 8 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves 5x5, 13 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require ten moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1]\u003c/pre\u003e\u003cp\u003ean answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 2 3 4 5 16 17 18 19 20]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\"\u003e5x5, 8 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves\"\u003e5x5, 13 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_5(board) % 5x5 board, 10 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 0 1];\r\nmoves = lights_out_5(board); % [1 2 3 4 5 16 17 18 19 20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 0 1  \r\n          0 0 1 0 1];\r\nmoves = lights_out_5(board); % [1 2 3 11 13 14 16 17 21 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_5(board); % [1 2 3 4 6 7 8 11 12 16]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          0 0 1 1 0  \r\n          1 1 0 1 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_5(board); % [3 6:7 11 13:15 19 22:23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 0 1 0  \r\n          1 0 1 1 0];\r\nmoves = lights_out_5(board); % [2 3 9 10 14 16 17 20 23 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 1 0 0 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 1 0 1];\r\nmoves = lights_out_5(board); % [2 4 7 9 11 12 17 19 20 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 0 1 1  \r\n          1 0 1 0 0  \r\n          1 0 1 0 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 1];\r\nmoves = lights_out_5(board); % [1 4 6 12 14 15 18 21 23 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          0 0 0 1 1  \r\n          1 1 0 0 0  \r\n          1 0 0 1 0  \r\n          1 1 1 1 0];\r\nmoves = lights_out_5(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 1  \r\n          1 1 0 0 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 1 1 1 0  \r\n          0 1 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 1 0  \r\n          0 1 1 1 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          0 0 0 1 1  \r\n          1 0 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 1 0 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [1 0 0 1 1  \r\n          0 0 1 1 0  \r\n          0 1 0 0 0  \r\n          0 1 1 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          0 0 0 1 1  \r\n          0 1 1 0 0  \r\n          1 1 1 1 0  \r\n          0 0 1 1 0];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_5(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==10)","published":true,"deleted":false,"likes_count":3,"comments_count":8,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2018-11-13T13:07:28.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T18:53:12.000Z","updated_at":"2025-11-29T13:41:10.000Z","published_at":"2018-11-12T15:53:32.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require ten moves to solve. For example, if\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 = [0 1 1 0 1  \\n          1 1 1 1 1  \\n          1 1 1 1 1  \\n          1 1 1 1 1  \\n          0 1 1 0 1]]]\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\u003ean answer is:\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[ moves = [1 2 3 4 5 16 17 18 19 20]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 8 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44757-lights-out-6-5x5-13-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 13 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44759,"title":"Lights Out 7 - 5x5, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44757 5x5, 13 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i 5x5, light-only solution? I\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44757\"\u003e5x5, 13 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i\"\u003e5x5, light-only solution? I\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_7(board) % 5x5 board, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 0 0 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_7(board); % [13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 0 1 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_7(board); % [7 13 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_7(board); % [7:9 12:14 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board); % [7 9 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_7(board); % [1 5 7 9 13 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0];\r\nmoves = lights_out_7(board); % [1:5 11:15 21:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          0 0 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_7(board); % [18:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_7(board); % [1:6 10:11 15:16 20:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_7(board); % [1:2 4:7 9:10 16:17 19:22 24:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 0 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_7(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          0 1 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 1 0  \r\n          0 1 1 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          1 1 1 1 1  \r\n          1 1 0 1 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          0 0 1 1 1];\r\nmoves = lights_out_7(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":19,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-30T12:52:15.000Z","updated_at":"2025-11-29T15:18:21.000Z","published_at":"2018-12-03T13:24:38.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve, ranging from 1 to 25 buttons (indices). The one function you write has to solve all of them. An answer is provided for some of the test cases, though there are often multiple possible answers per test case—any correct answer will work.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44757\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 13 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44760-lights-out-8-5x5-light-only-solution-i\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? I\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44755,"title":"Lights Out 4 - 5x5, 8 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require eight moves to solve. For example, if\r\n\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0]\r\n\r\nthe answer is:\r\n\r\n moves = [2 4 6 10 16 20 22 24]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves 5x5, 6 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves 5x5, 10 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require eight moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 4 6 10 16 20 22 24]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\"\u003e5x5, 6 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves\"\u003e5x5, 10 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_4(board) % 5x5 board, 8 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_4(board); % [2 4 6 10 16 20 22 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board); % [7 8 9 12 14 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 1 0  \r\n          0 0 1 1 0];\r\nmoves = lights_out_4(board); % [1 2 5 10 16 21 24 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_4(board); % [1 5 7 9 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board); % [4 5 8 12 14 19 22 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_4(board); % [1 2 3 4 5 7 9 13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_4(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 1 1  \r\n          1 1 1 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 0  \r\n          1 0 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 1 0 0 0  \r\n          0 1 1 0 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 1 0 1 0  \r\n          1 0 1 1 1  \r\n          1 1 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 1  \r\n          1 1 0 1 0  \r\n          0 1 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 1 1 1 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 1 0 0 1  \r\n          0 1 0 1 1  \r\n          1 0 1 1 0  \r\n          0 1 0 0 1];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          1 0 0 0 1  \r\n          0 0 1 1 1  \r\n          0 1 0 1 1  \r\n          1 0 0 1 0];\r\nmoves = lights_out_4(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==8)\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2018-11-09T14:19:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T18:36:00.000Z","updated_at":"2025-11-29T14:28:48.000Z","published_at":"2018-11-09T14:19:17.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require eight moves to solve. For example, if\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 = [0 1 0 1 0  \\n          1 0 0 0 1  \\n          0 0 0 0 0  \\n          1 0 0 0 1  \\n          0 1 0 1 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\u003ethe answer is:\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[ moves = [2 4 6 10 16 20 22 24]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44756-lights-out-5-5x5-10-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 10 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44760,"title":"Lights Out 8 - 5x5, light-only solution? I","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44759 5x5, any number of moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44761 5x5, light-only solution? II\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44759\"\u003e5x5, any number of moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44761\"\u003e5x5, light-only solution? II\u003c/a\u003e\u003c/p\u003e","function_template":"function tf = lights_out_8(board) % 5x5 board, lights-only solution\r\n tf = 0;\r\nend","test_suite":"%% all true cases first\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nassert(lights_out_8(board)); % [2 4 6 10 16 20 22 24]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nassert(lights_out_8(board)); % [1 5 7 9 17 19 21 25]\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nassert(lights_out_8(board)); % [2 6 8 12 14 18 20 24]\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nassert(lights_out_8(board)); % [1:2 4:7 9:10 16:17 19:22 24:25]\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_8(board)); % [3 7 9 11 13 15 17 19 23]\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1];\r\nassert(lights_out_8(board)); % [1 3 5 11 13 15 21 23 25]\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_8(board)); % [2:4 6:7 9:11 13 15:17 19:20 22:24]\r\n\r\n\r\n%% false cases start here\r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nassert(~lights_out_8(board)); % [1 2 3 4 5 7 9 13]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 0 0  \r\n          0 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 0 0 0];\r\nassert(~lights_out_8(board)); % [1 2 3 4 6 7 8 11 12 16]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          1 0 0 0 1];\r\nassert(~lights_out_8(board)); % on your own\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 1 1 0 0];\r\nassert(~lights_out_8(board));\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0];\r\nassert(~lights_out_8(board));\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":16,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":52,"created_at":"2018-10-30T14:01:47.000Z","updated_at":"2025-11-29T15:01:02.000Z","published_at":"2019-01-09T15:04:36.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state is a potential answer—i.e., if toggling only the starting lights are sufficient to solve the board.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44759\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, any number of moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44761\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? II\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44753,"title":"Lights Out 3 - 5x5, 6 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require six moves to solve. For example, if\r\n\r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [1 5 11 15 21 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves 5x5, 4 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves 5x5, 8 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require six moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 5 11 15 21 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\"\u003e5x5, 4 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\"\u003e5x5, 8 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_3(board) % 5x5 board, 6 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_3(board); % [1 5 11 15 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_3(board); % [4 9 10 16 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_3(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          0 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 0 1 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_3(board); % [4 8 11 13 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 1 0];\r\nmoves = lights_out_3(board); % [7 8 12 14 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 1 1 0  \r\n          0 1 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_3(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 1 0 1  \r\n          0 1 0 0 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 1 1  \r\n          1 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 1  \r\n          0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          1 0 1 1 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 0 0 0 0  \r\n          1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 0 0 1  \r\n          0 1 1 0 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 0];\r\nmoves = lights_out_3(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":10,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":20,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T14:59:29.000Z","updated_at":"2025-11-29T15:04:22.000Z","published_at":"2018-11-05T13:04:28.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require six moves to solve. For example, if\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 = [1 0 1 0 1  \\n          1 0 1 0 1  \\n          0 0 0 0 0  \\n          1 0 1 0 1  \\n          1 0 1 0 1]]]\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\u003ethe answer is:\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[ moves = [1 5 11 15 21 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44755-lights-out-4-5x5-8-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 8 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44751,"title":"Lights Out 1 - 5x5, 3 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\r\n\r\nThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\r\n\r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0]\r\n\r\nthe answer is:\r\n\r\n moves = [5 7 23]\r\n\r\nIn this first problem, all boards have solutions of only three moves.\r\n\r\nNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\r\n\r\nAs a potential starting point, you might check out one or more of the following resources by \u003chttp://www.mat.ucm.es/~vmunozve/lights-out.pdf Vicente Muñoz\u003e; \u003chttp://www.ijritcc.org/download/browse/Volume_5_Issues/August_17_Volume_5_Issue_8/1503304082_21-08-2017.pdf Chen, et al.\u003e; \u003chttp://vprusso.github.io/blog/2017/the-mathematics-of-lights-out/ Vincent Russo\u003e; \u003chttp://mathworld.wolfram.com/LightsOutPuzzle.html Margherita Barile (on wolfram.com)\u003e; \u003chttp://www.keithschwarz.com/interesting/code/?dir=lights-out Keith Schwarz\u003e; or \u003chttps://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026httpsredir=1\u0026article=1166\u0026context=theses Rebecca Meyer (a master's thesis)\u003e.\r\n\r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves 5x5, 4 moves\u003e","description_html":"\u003cdiv style = \"text-align: start; line-height: 20px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: normal; text-decoration: none; white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; vertical-align: baseline; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003ca target='_blank' href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eLights Out\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e board = [0 1 0 0 0\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 1 1 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          0 1 0 1 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 0 0 0 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e          1 1 0 0 0]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003ethe answer is:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-bottom: 10px; margin-left: 3px; margin-right: 3px; margin-top: 10px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-inline-end: 1px solid rgb(233, 233, 233); border-inline-start: 1px solid rgb(233, 233, 233); border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 18px; padding-left: 4px; white-space: nowrap; \"\u003e\u003cspan style=\"border-inline-end: 0px none rgb(0, 0, 0); border-inline-start: 0px none rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-right: 45px; min-height: 0px; padding-left: 0px; tab-size: 4; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-right: 0px; \"\u003e moves = [5 7 23]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 10px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eIn this first problem, all boards have solutions of only three moves.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eAs a potential starting point, you might check out one or more of the following resources by\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eVicente Muñoz\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eChen, et al.\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eVincent Russo\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eMargherita Barile (on wolfram.com)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e;\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves/edit#null\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eKeith Schwarz\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e; or\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026amp;httpsredir=1\u0026amp;article=1166\u0026amp;context=theses\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eRebecca Meyer (a master's thesis)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003eNext:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e5x5, 4 moves\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function moves = lights_out_1(board) % 5x5 board, 3 moves\r\n moves = board;\r\nend","test_suite":"\r\n%% \r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0];\r\n %plot_board(board,'Input')\r\nmoves = lights_out_1(board); % should be [5 7 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2);   %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_1(board); %should be [1 13 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_1(board); %should be [2 3 4]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 1 0 1  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_1(board); %should be [7 12 17]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 1  \r\n          0 0 1 1 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_1(board); %you're on your own now\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 1 0 0];\r\n plot_board(board,'Input',[])\r\nmoves = lights_out_1(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2);\r\n    plot_board(board,['Step ',num2str(i)],moves(i))\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==3)\r\n\r\n\r\nfunction plot_board(board,txt_title,sq_tap)\r\n sq_on = (board==1); % determine which squares are on\r\n pts = repmat(1:5,[5,1]); x = pts(1:end); % x coordinates for each square\r\n pts = fliplr(pts)'; y = pts(1:end); % y coordinates for each square\r\n figure; hold on\r\n fill([0,0,5,5],[0,5,5,0],'k','FaceAlpha',0.3) % background\r\n plot(x(sq_on)-0.5,y(sq_on)-0.5,'ys','MarkerSize',50, ...\r\n     'MarkerFaceColor','y') % on squares\r\n plot(x(~sq_on)-0.5,y(~sq_on)-0.5,'s','MarkerSize',50, ...\r\n     'Color',[0.9 0.95 1.0],'MarkerFaceColor',[0.9 0.95 1.0]) % off squares\r\n if ~isempty(sq_tap)\r\n  plot(x(sq_tap)-0.5,y(sq_tap)-0.5,'g*','MarkerSize',40, ...\r\n      'LineWidth',5) % tapped square\r\n end\r\n axis square\r\n set(gca,'XTick',0:5); set(gca,'YTick',0:5)\r\n title(txt_title,'FontSize',24)\r\nend","published":true,"deleted":false,"likes_count":5,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2021-04-13T00:54:00.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-25T19:39:19.000Z","updated_at":"2026-01-06T08:23:35.000Z","published_at":"2018-10-29T14:07:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. Pressing any button on the board toggles that button as well as adjacent buttons vertically and horizontally, where present (and no wrapping here), to the opposite state (i.e., on to off and off to on). For example, pressing a corner button toggles three button states, an edge button four button states, and an interior button five button states (as can be visualized by the example below).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis first problem uses a 5x5 board and requires that your function return a column-major vector of button presses (indices) that will solve the board by turning off all lights. Since pressing any button twice will return to the same board state, only one move is needed (and allowed) per button. For example, if\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 = [0 1 0 0 0\\n          1 1 1 0 1\\n          0 1 0 1 1\\n          1 0 0 0 1\\n          1 1 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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethe answer is:\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[ moves = [5 7 23]]]\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn this first problem, all boards have solutions of only three moves.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote: while brute-force solutions will solve some problems in this series, they will time out on later ones. Solving the first few problems by developing a robust solver function is encouraged.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAs a potential starting point, you might check out one or more of the following resources by\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=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eVicente Muñoz\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eChen, et al.\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eVincent Russo\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMargherita Barile (on wolfram.com)\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\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eKeith Schwarz\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e; or\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=\\\"https://dc.ewu.edu/cgi/viewcontent.cgi?referer=https://www.google.com/\u0026amp;httpsredir=1\u0026amp;article=1166\u0026amp;context=theses\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eRebecca Meyer (a master's thesis)\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\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNext:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44752-lights-out-2-5x5-4-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":44752,"title":"Lights Out 2 - 5x5, 4 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require four moves to solve. For example, if\r\n\r\n board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [2 5 16 24]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves 5x5, 3 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves 5x5, 6 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require four moves to solve. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 5 16 24]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003e5x5, 3 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\"\u003e5x5, 6 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_2(board) % 5x5 board, 4 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 1 1 1  \r\n          1 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 1 1  \r\n          1 1 0 0 1];\r\nmoves = lights_out_2(board); % [2 5 16 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 0 0 0  \r\n          1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          1 0 0 0 0];\r\nmoves = lights_out_2(board); % [1 2 3 4]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_2(board); % [7 9 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 1 1 0  \r\n          0 1 1 1 1  \r\n          0 1 1 1 1  \r\n          0 0 1 1 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_2(board); % [12 13 17 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_2(board); % [8 12 14 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 0 1  \r\n          0 0 1 1 1  \r\n          0 0 0 1 1  \r\n          0 1 0 0 1  \r\n          1 1 1 0 0];\r\nmoves = lights_out_2(board); % on your own now\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          0 1 0 1 0  \r\n          0 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 0 0  \r\n          0 0 0 1 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_2(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==4)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-29T14:43:57.000Z","updated_at":"2025-11-29T15:31:35.000Z","published_at":"2018-10-31T12:25:12.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require four moves to solve. For example, if\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 = [1 0 1 1 1  \\n          1 1 0 1 0  \\n          1 0 0 0 1  \\n          1 0 0 1 1  \\n          1 1 0 0 1]]]\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\u003ethe answer is:\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[ moves = [2 5 16 24]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44753-lights-out-3-5x5-6-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44761,"title":"Lights Out 9 - 5x5, light-only solution? II","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44760 5x5, light-only solution? I\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44764 5x5, with wrapping, 6 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44760\"\u003e5x5, light-only solution? I\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44764\"\u003e5x5, with wrapping, 6 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function tf = lights_out_9(board) % 5x5 board, lights-only subset solution\r\n tf = 0;\r\nend","test_suite":"%% all true cases first\r\n board = [0 1 0 0 0\r\n          1 1 1 0 1\r\n          0 1 0 1 1\r\n          1 0 0 0 1\r\n          1 1 0 0 0];\r\nassert(lights_out_9(board)); % [5 7 23]\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 1];\r\nassert(lights_out_9(board)); % [1 13 25]\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [9 15 19]\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 1 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 0 1 0];\r\nassert(lights_out_9(board)); % [7 9 17 19]\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 1 0  \r\n          1 0 0 0 1];\r\nassert(lights_out_9(board)); % [1 5 7 9 17 19 21 25]\r\n\r\n%% \r\n board = [0 0 1 1 0  \r\n          0 1 1 1 1  \r\n          0 1 1 1 1  \r\n          0 0 1 1 0  \r\n          0 0 0 0 0];\r\nassert(lights_out_9(board)); % [12 13 17 18]\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [8 12 14 18]\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board)); % [3 9 15 19]\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 1 0 1];\r\nassert(lights_out_9(board)); % [1 5 11 15 21 25]\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_9(board)); % [7 8 9 12 14 17 18 19]\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nassert(lights_out_9(board)); % [2 6 8 12 14 18 20 24]\r\n\r\n%% \r\n board = [0 1 1 1 1  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 1 0  \r\n          0 1 0 0 0];\r\nassert(lights_out_9(board)); % on your own\r\n\r\n%% \r\n board = [1 1 1 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          1 1 1 1 1];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0];\r\nassert(lights_out_9(board));\r\n\r\n\r\n%% false cases start here\r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nassert(~lights_out_9(board)); % [7 8 9 17 18 19]\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 1 0  \r\n          0 0 1 1 0];\r\nassert(~lights_out_9(board)); % [1 2 5 10 16 21 24 25]\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          1 1 0 1 0  \r\n          0 0 0 0 0  \r\n          0 1 0 0 1  \r\n          0 1 0 0 0];\r\nassert(~lights_out_9(board)); % [2 5:6 8:11 17:24]\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          0 1 1 0 0  \r\n          1 0 1 0 0  \r\n          0 1 1 0 0  \r\n          0 0 0 0 0];\r\nassert(~lights_out_9(board)); % on your own\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          1 0 1 0 0  \r\n          1 1 1 1 0  \r\n          0 1 1 0 1  \r\n          0 1 0 1 0];\r\nassert(~lights_out_9(board));\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 0 1 0 0  \r\n          0 1 1 1 0  \r\n          0 0 1 0 0  \r\n          0 0 0 0 0];\r\nassert(lights_out_9(board));\r\n\r\n%% \r\n board = [1 0 1 1 0  \r\n          1 0 0 1 1  \r\n          0 0 0 0 0  \r\n          1 1 0 0 1  \r\n          0 1 1 0 1];\r\nassert(~lights_out_9(board));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":4,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":"2019-05-02T12:12:00.000Z","rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-30T14:14:44.000Z","updated_at":"2025-11-29T15:23:35.000Z","published_at":"2019-04-22T15:59:00.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. The function you write has to determine if the initial state contains a potential answer—i.e., if toggling any given subset of the starting lights is sufficient to solve the board.\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44760\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? I\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44764\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, with wrapping, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44764,"title":"Lights Out 10 - 5x5, with wrapping, 6 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if \r\n\r\n board = [1 0 0 0 1\r\n          1 0 1 0 1\r\n          0 0 0 0 0\r\n          1 0 1 0 1\r\n          1 0 0 0 1]\r\n\r\nthe answer is:\r\n\r\n moves = [1 5 11 15 21 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44761 5x5, light-only solution? II\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44765 5x5, wrapping, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 0 0 1\r\n          1 0 1 0 1\r\n          0 0 0 0 0\r\n          1 0 1 0 1\r\n          1 0 0 0 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 5 11 15 21 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44761\"\u003e5x5, light-only solution? II\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44765\"\u003e5x5, wrapping, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_10(board) % 5x5 board, with wrapping, 6 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 0 0 1  \r\n          1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1  \r\n          1 0 0 0 1];\r\nmoves = lights_out_10(board); % [1 5 11 15 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          1 0 1 1 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 1  \r\n          0 0 1 1 0];\r\nmoves = lights_out_10(board); % [4 9 10 16 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_10(board); % [7 8 9 17 18 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 1 0 1  \r\n          1 1 1 0 0  \r\n          0 0 0 0 1  \r\n          1 0 1 0 1  \r\n          1 0 1 0 0];\r\nmoves = lights_out_10(board); % [4 8 11 13 17 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          1 0 1 0 0  \r\n          0 0 0 1 0  \r\n          0 1 0 1 1];\r\nmoves = lights_out_10(board); % [7 8 12 14 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          0 1 1 1 1  \r\n          0 1 0 0 0  \r\n          0 0 1 0 0];\r\nmoves = lights_out_10(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 1 0 1  \r\n          1 1 0 0 0  \r\n          1 0 0 1 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 0 0 0 0  \r\n          1 0 0 1 1  \r\n          0 0 0 0 1  \r\n          1 0 0 0 1  \r\n          1 0 1 0 1];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 0 0 1 0  \r\n          0 0 0 1 0  \r\n          0 0 0 0 1  \r\n          1 0 1 1 0  \r\n          1 1 1 1 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          0 0 0 0 1  \r\n          1 1 0 0 1  \r\n          1 0 0 0 1  \r\n          1 1 1 0 1];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n\r\n%% \r\n board = [0 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 0 0 1 1  \r\n          0 1 1 0 0  \r\n          1 1 0 0 0];\r\nmoves = lights_out_10(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\nassert(numel(moves)==6)\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":4,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":"2019-04-30T11:31:05.000Z","rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T16:24:02.000Z","updated_at":"2025-11-29T15:08:07.000Z","published_at":"2019-04-22T16:00:51.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require six moves to solve. However, now wrapping of the lights occurs. For example, if\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 = [1 0 0 0 1\\n          1 0 1 0 1\\n          0 0 0 0 0\\n          1 0 1 0 1\\n          1 0 0 0 1]]]\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\u003ethe answer is:\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[ moves = [1 5 11 15 21 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44761\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, light-only solution? II\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44765\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44766,"title":"Lights Out 12 - 5x5, three stages, \u003c7 moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if \r\n\r\n board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 19]\r\n\r\nsince the first \"1\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \"1\" will then change those three values to zero, while the \"19\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44765 5x5, wrapping, any number of moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44767 5x5, 3 stages, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 19]\u003c/pre\u003e\u003cp\u003esince the first \"1\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \"1\" will then change those three values to zero, while the \"19\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44765\"\u003e5x5, wrapping, any number of moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44767\"\u003e5x5, 3 stages, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_12(board) % 5x5 board, three stages, \u003c7 moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 2 0  \r\n          0 0 2 2 2  \r\n          0 0 0 2 0];\r\nmoves = lights_out_12(board); % [1 1 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 2 0 2 2  \r\n          2 0 2 0 2  \r\n          0 2 2 2 0  \r\n          2 0 2 0 2  \r\n          2 2 0 2 2];\r\nmoves = lights_out_12(board); % [1 5 13 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 1 1 0 1  \r\n          0 1 0 1 1  \r\n          1 0 0 0 1  \r\n          1 1 0 0 0];\r\nmoves = lights_out_12(board); % [5 5 7 7 23 23]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 2 0 0  \r\n          2 0 2 0 0  \r\n          2 0 2 0 0  \r\n          2 0 2 0 0  \r\n          2 1 2 0 0];\r\nmoves = lights_out_12(board); % [6:10]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          2 0 0 0 0  \r\n          1 1 0 0 0  \r\n          2 0 0 0 0  \r\n          1 1 0 0 0];\r\nmoves = lights_out_12(board); % [1 1 3 3 5 5]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 2 0  \r\n          2 2 0 2 2  \r\n          0 0 2 0 0  \r\n          2 2 0 2 2  \r\n          0 2 0 2 0];\r\nmoves = lights_out_12(board); % [7 9 13 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 2 0  \r\n          2 1 1 1 2  \r\n          2 0 1 0 2  \r\n          2 1 1 1 2  \r\n          0 2 0 2 0];\r\nmoves = lights_out_12(board); % [7:9 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 2 1 2 0  \r\n          2 0 2 0 2  \r\n          0 2 1 2 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board); % [8 13 13 18]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 2 0 2 1  \r\n          0 1 1 1 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board); % [7 7 12 12 17 17]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 1 2  \r\n          0 0 2 2 1  \r\n          0 1 0 2 2  \r\n          0 1 1 2 2  \r\n          2 0 0 0 2];\r\nmoves = lights_out_12(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 1 0 0  \r\n          2 2 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_12(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 0 0 0  \r\n          2 1 1 0 0  \r\n          2 0 0 2 0  \r\n          0 1 1 2 0];\r\nmoves = lights_out_12(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T17:08:51.000Z","updated_at":"2025-11-04T21:21:50.000Z","published_at":"2019-05-02T12:25:05.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem is back to no wrapping and contains boards that each require six or fewer moves to solve. However, now lights are activated through three stages, rather than two, cycling from on1 (1) to on2 (2) to off (0). For example, if\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 = [1 1 0 0 0  \\n          1 0 0 0 0  \\n          0 0 0 2 0  \\n          0 0 2 2 2  \\n          0 0 0 2 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\u003ethe answer is:\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[ moves = [1 1 19]]]\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\u003esince the first \\\"1\\\" will change the 1's in (1,1), (1,2), and (2,1) to 2's. The second \\\"1\\\" will then change those three values to zero, while the \\\"19\\\" will bump the five 2's to zero. Therefore, up to two moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44765\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, any number of moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44767\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44776,"title":"Lights Out 15 - 5x5, broken buttons I","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\r\n\r\nFor example, if:\r\n\r\n board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 10 18]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44769 5x5, four stages, x moves\u003e — \r\nNext: [Check back later for new problems in the series.]","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\u003c/p\u003e\u003cp\u003eFor example, if:\u003c/p\u003e\u003cpre\u003e board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 10 18]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44769\"\u003e5x5, four stages, x moves\u003c/a\u003e — \r\nNext: [Check back later for new problems in the series.]\u003c/p\u003e","function_template":"function moves = lights_out_15(board) % 5x5 board, any number of moves, broken buttons I\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [0 1 0 0 0  \r\n          1 0 0 1 0  \r\n          0 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 0];\r\nmoves = lights_out_15(board); % [1 10 18]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 1 0 0  \r\n          0 1 0 1 0  \r\n          0 0 1 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_15(board); % [7 19]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % [1 5 13 21 25]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0];\r\nmoves = lights_out_15(board); % [6:10]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 0 0  \r\n          1 0 1 1 1  \r\n          1 0 1 0 1  \r\n          1 0 0 1 1  \r\n          0 1 0 1 1];\r\nmoves = lights_out_15(board); % [2 5 7 11:13 19 21 24]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % [2:4 6 10:11 15:16 20 22:24]\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_15(board); % on your own\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 1 0 1  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          1 0 1 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 1 0 1 0  \r\n          0 1 0 1 0  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 1  \r\n          1 0 0 0 1  \r\n          1 0 0 0 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 1 0 0 0  \r\n          0 1 0 0 0  \r\n          0 0 1 1 1  \r\n          0 0 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 0 0 0  \r\n          0 0 1 0 1  \r\n          0 1 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          1 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 0 1  \r\n          0 0 0 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          0 0 0 0 0  \r\n          0 0 0 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 0 0 0 0  \r\n          1 0 0 1 1  \r\n          1 0 0 0 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          1 1 0 1 1  \r\n          0 1 1 0 1  \r\n          0 1 1 1 1  \r\n          1 1 0 1 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 1 0  \r\n          1 0 0 1 0  \r\n          0 1 0 1 0  \r\n          0 1 0 0 1  \r\n          0 1 0 0 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 0 0  \r\n          0 1 0 0 1  \r\n          0 0 0 0 0  \r\n          0 0 1 0 1  \r\n          0 0 1 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 0 0  \r\n          0 1 1 1 1  \r\n          0 0 1 0 0  \r\n          1 0 0 0 0  \r\n          0 1 0 0 0];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0  \r\n          1 1 1 1 1  \r\n          0 0 0 1 1];\r\nmoves = lights_out_15(board);\r\nb1 = diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-11-06T13:17:12.000Z","updated_at":"2025-11-04T21:41:48.000Z","published_at":"2019-09-16T10:59:35.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains 5x5 boards that require any number of moves to solve. However, the game has a glitch now—each time you press a button, it doesn't toggle itself, only those lights that are adjacent. That is to say, all buttons toggle two, three, or four lights (indices), rather than the normal three, four, or five, respectively.\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\u003eFor example, if:\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 = [0 1 0 0 0  \\n          1 0 0 1 0  \\n          0 0 1 0 1  \\n          0 1 0 1 0  \\n          1 0 1 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\u003ethe answer is:\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[ moves = [1 10 18]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44769\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, four stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next: [Check back later for new problems in the series.]\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":44769,"title":"Lights Out 14 - 5x5, four stages, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction and \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 problem 12\u003e for an explanation of three stages.\r\n\r\nThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if \r\n\r\n board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 1 15 15 22]\r\n\r\nUp to three moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44767 5x5, 3 stages, x moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44776 5x5, broken buttons I\u003e.","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction and \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003eproblem 12\u003c/a\u003e for an explanation of three stages.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 1 15 15 22]\u003c/pre\u003e\u003cp\u003eUp to three moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44767\"\u003e5x5, 3 stages, x moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44776\"\u003e5x5, broken buttons I\u003c/a\u003e.\u003c/p\u003e","function_template":"function moves = lights_out_14(board) % 5x5 board, four stages, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 0 3  \r\n          1 0 0 3 3  \r\n          0 0 0 0 3  \r\n          0 0 2 0 0  \r\n          0 2 2 2 0];\r\nmoves = lights_out_14(board); % [1 1 1 15 15 22]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 3 2 0 0  \r\n          3 1 2 0 0  \r\n          3 1 2 0 0  \r\n          3 1 2 0 0  \r\n          0 3 2 0 0];\r\nmoves = lights_out_14(board); % [1:10 6:10]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 3 0 3 1  \r\n          3 2 3 2 3  \r\n          0 3 3 3 0  \r\n          3 2 3 2 3  \r\n          1 3 0 3 1];\r\nmoves = lights_out_14(board); % [1 1 1 5 5 5 7 7 9 9 13 17 17 19 19 21 21 21 25 25 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 1 1 2  \r\n          1 2 3 2 1  \r\n          1 3 0 3 1  \r\n          1 2 3 2 1  \r\n          2 1 1 1 2];\r\nmoves = lights_out_14(board); % [6:10 16:20 2:5:22 4:5:24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 3 1 3  \r\n          2 2 2 3 2  \r\n          0 2 3 1 2  \r\n          0 0 0 1 0  \r\n          3 2 1 2 1];\r\nmoves = lights_out_14(board); % [2 4 4 4:7 6 10:13 12 17:21 18:20 19 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 2 0 1 0  \r\n          2 2 3 1 1  \r\n          0 3 0 3 0  \r\n          1 1 3 2 2  \r\n          0 1 0 2 0];\r\nmoves = lights_out_14(board); % [7 7 9 9 9 17 17 17 19 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 3 1 3 0  \r\n          0 2 2 2 0  \r\n          0 1 1 1 0  \r\n          0 2 2 2 0  \r\n          0 3 1 3 0];\r\nmoves = lights_out_14(board); % [11:15 12:14 13]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [3 1 0 0 0  \r\n          1 2 3 0 0  \r\n          0 3 1 3 0  \r\n          0 0 3 2 1  \r\n          0 0 0 1 3];\r\nmoves = lights_out_14(board); % [1 7 7 13 13 13 19 19 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 1 2 1  \r\n          3 0 1 0 3  \r\n          3 2 0 2 2  \r\n          2 2 3 0 1  \r\n          1 3 0 2 3];\r\nmoves = lights_out_14(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          3 2 2 0 0  \r\n          1 1 3 2 0  \r\n          0 2 1 2 1  \r\n          0 0 1 3 1];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [3 2 3 3 0  \r\n          3 1 2 2 3  \r\n          3 0 1 0 2  \r\n          2 3 3 3 1  \r\n          3 1 2 2 3];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 3 0 1  \r\n          1 2 1 1 2  \r\n          1 0 2 0 0  \r\n          1 0 0 0 0  \r\n          0 3 2 2 0];\r\nmoves = lights_out_14(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),4); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-31T17:50:31.000Z","updated_at":"2025-11-04T21:35:20.000Z","published_at":"2019-05-06T13:53:48.000Z","restored_at":"2022-02-16T22:15:39.000Z","restored_by":null,"spam":false,"simulink":false,"admin_reviewed":true,"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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction and\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eproblem 12\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an explanation of three stages.\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\u003eThis problem contains boards that each require any number of moves to solve. In addition, lights are now activated through four stages: on1 (1) to on2 (2) to on3 (3) to off (0). For example, if\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 = [1 1 0 0 3  \\n          1 0 0 3 3  \\n          0 0 0 0 3  \\n          0 0 2 0 0  \\n          0 2 2 2 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\u003ethe answer is:\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[ moves = [1 1 1 15 15 22]]]\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\u003eUp to three moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44767\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44776\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, broken buttons I\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":44765,"title":"Lights Out 11 - 5x5, with wrapping, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction.\r\n\r\nThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if \r\n\r\n board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1]\r\n\r\nthe answer is:\r\n\r\n moves = [2 4 13 25]\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44764 5x5, wrapping, 6 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 5x5, 3 stages, \u003c7 moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if\u003c/p\u003e\u003cpre\u003e board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [2 4 13 25]\u003c/pre\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44764\"\u003e5x5, wrapping, 6 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_11(board) % 5x5 board, with wrapping, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 0 0 0 1  \r\n          1 1 1 0 1  \r\n          0 1 1 1 0  \r\n          1 1 1 0 0  \r\n          0 0 0 1 1];\r\nmoves = lights_out_11(board); % [2 4 13 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 0 0 0 1  \r\n          0 0 0 0 0  \r\n          1 0 0 0 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_11(board); % [1 5 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0  \r\n          1 0 1 0 1  \r\n          0 1 0 1 0];\r\nmoves = lights_out_11(board); % [2 6 8 12 14 18 20 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 0 0  \r\n          0 1 0 1 1  \r\n          0 0 0 0 1  \r\n          0 0 0 1 1  \r\n          1 0 0 1 1];\r\nmoves = lights_out_11(board); % [3 7 17 21 23:25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          1 1 0 1 1  \r\n          1 0 1 0 1  \r\n          1 1 0 1 1  \r\n          0 1 1 1 0];\r\nmoves = lights_out_11(board); % [7:9 12:14 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0];\r\nmoves = lights_out_11(board); % [11:15]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 1 0 0  \r\n          0 0 1 0 0  \r\n          1 1 1 1 1  \r\n          1 0 0 1 0  \r\n          1 1 0 1 0];\r\nmoves = lights_out_11(board); % [9 11 14 21 23 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 1 1  \r\n          1 1 0 1 1  \r\n          0 0 1 0 0  \r\n          1 1 0 1 1  \r\n          1 1 0 1 1];\r\nmoves = lights_out_11(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1  \r\n          0 0 1 0 1];\r\nmoves = lights_out_11(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 0 0 0 1  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          0 1 1 1 0  \r\n          1 0 0 0 1];\r\nmoves = lights_out_11(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1);\r\nb1(1,5) = 1; b1(5,1) = 1; b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b2; b2,b1,b2,b3,b3; b3,b2,b1,b2,b3; b3,b3,b2,b1,b2; b2,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),2); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":12,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":624,"created_at":"2018-10-31T16:37:46.000Z","updated_at":"2025-11-04T20:46:11.000Z","published_at":"2019-05-02T12:20:39.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction.\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\u003eThis problem contains boards that each require any number of moves to solve. However, now wrapping of the lights occurs. For example, if\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 = [1 0 0 0 1  \\n          1 1 1 0 1  \\n          0 1 1 1 0  \\n          1 1 1 0 0  \\n          0 0 0 1 1]]]\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\u003ethe answer is:\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[ moves = [2 4 13 25]]]\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44764\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, wrapping, 6 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":44767,"title":"Lights Out 13 - 5x5, three stages, x moves","description":"\u003chttps://en.wikipedia.org/wiki/Lights_Out_(game) Lights Out\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves the first problem in the series\u003e for an introduction and \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 problem 12\u003e for an explanation of three stages.\r\n\r\nThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if \r\n\r\n board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0]\r\n\r\nthe answer is:\r\n\r\n moves = [1 1 10 15 21]\r\n\r\nUp to two moves are possible for each button (index).\r\n\r\nPrev.: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44766 5x5, 3 stages, \u003c7 moves\u003e — \r\nNext: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44769 5x5, 4 stages, x moves\u003e","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Lights_Out_(game)\"\u003eLights Out\u003c/a\u003e is a logic game wherein all lights need to be turned off to complete each board. See \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\"\u003ethe first problem in the series\u003c/a\u003e for an introduction and \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003eproblem 12\u003c/a\u003e for an explanation of three stages.\u003c/p\u003e\u003cp\u003eThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if\u003c/p\u003e\u003cpre\u003e board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0]\u003c/pre\u003e\u003cp\u003ethe answer is:\u003c/p\u003e\u003cpre\u003e moves = [1 1 10 15 21]\u003c/pre\u003e\u003cp\u003eUp to two moves are possible for each button (index).\u003c/p\u003e\u003cp\u003ePrev.: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44766\"\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/a\u003e — \r\nNext: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44769\"\u003e5x5, 4 stages, x moves\u003c/a\u003e\u003c/p\u003e","function_template":"function moves = lights_out_13(board) % 5x5 board, three stages, any number of moves\r\n moves = board;\r\nend","test_suite":"%% \r\n board = [1 1 0 2 2  \r\n          1 0 0 0 2  \r\n          0 0 0 0 0  \r\n          0 2 2 0 0  \r\n          2 1 1 2 0];\r\nmoves = lights_out_13(board); % [1 1 10 15 21]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 2 0 2 2  \r\n          0 0 2 0 0  \r\n          2 2 2 2 2  \r\n          0 0 2 0 0  \r\n          2 2 0 2 2];\r\nmoves = lights_out_13(board); % [6:2:20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 0 2 0  \r\n          1 0 2 2 2  \r\n          2 0 0 1 0  \r\n          1 1 2 2 2  \r\n          0 1 2 2 0];\r\nmoves = lights_out_13(board); % [1 1 4 5 10 17 19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 1 0 1 2  \r\n          1 2 2 2 1  \r\n          0 2 1 2 0  \r\n          1 2 2 2 1  \r\n          2 1 0 1 2];\r\nmoves = lights_out_13(board); % [1 5 7 9 13 13 17 19 21 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 0 2 0 0  \r\n          1 1 1 0 0  \r\n          1 0 1 0 0  \r\n          1 1 1 0 0  \r\n          2 0 2 0 0];\r\nmoves = lights_out_13(board); % [6:10 7:9]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [2 0 2 0 2  \r\n          2 2 1 2 2  \r\n          2 2 1 2 2  \r\n          2 2 1 2 2  \r\n          2 0 2 0 2];\r\nmoves = lights_out_13(board); % [6:20]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 1 2 1 0  \r\n          0 1 2 1 2  \r\n          2 2 2 2 2  \r\n          2 1 1 1 2  \r\n          2 2 0 2 0];\r\nmoves = lights_out_13(board); % [2 2 5 7:8 8 11 13 17:19]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 2 0  \r\n          0 2 1 2 2  \r\n          1 2 2 0 1  \r\n          0 1 2 1 1  \r\n          2 1 2 2 1];\r\nmoves = lights_out_13(board); % [3 3 7 9:11 11 16:18 17 21 21 24 25]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 2  \r\n          0 2 0 1 2  \r\n          1 2 0 2 0  \r\n          2 2 2 0 2  \r\n          1 2 1 0 2];\r\nmoves = lights_out_13(board); % [4 8 10 14 18 22 24]\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 0 0 0  \r\n          0 1 2 1 0  \r\n          0 2 0 2 0  \r\n          0 1 2 1 0  \r\n          0 0 0 0 0];\r\nmoves = lights_out_13(board); % on your own\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 0 2 1  \r\n          2 2 2 2 2  \r\n          0 2 2 2 0  \r\n          2 2 2 2 2  \r\n          1 2 0 2 1];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [1 2 2 2 2  \r\n          1 1 1 0 0  \r\n          2 1 1 2 2  \r\n          1 1 1 0 0  \r\n          1 2 2 2 2];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n\r\n%% \r\n board = [0 0 2 0 2  \r\n          1 1 0 0 2  \r\n          1 1 0 1 1  \r\n          0 1 2 2 2  \r\n          0 2 0 1 0];\r\nmoves = lights_out_13(board);\r\nb1 = diag(ones(1,5),0) + diag(ones(1,4),1) + diag(ones(1,4),-1); b2 = eye(5); b3 = zeros(5);\r\nb_map = [b1,b2,b3,b3,b3;b2,b1,b2,b3,b3;b3,b2,b1,b2,b3;b3,b3,b2,b1,b2;b3,b3,b3,b2,b1];\r\nfor i = 1:numel(moves)\r\n\tboard = mod(board + reshape(b_map(moves(i),:),[5,5]),3); %remove semicolon to display progress\r\nend\r\nassert(sum(abs(board(:)))==0)\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-10-31T17:27:28.000Z","updated_at":"2025-11-04T21:29:01.000Z","published_at":"2019-05-06T13:52:06.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:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Lights_Out_(game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLights Out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a logic game wherein all lights need to be turned off to complete each board. See\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44751-lights-out-1-5x5-3-moves\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ethe first problem in the series\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an introduction and\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eproblem 12\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for an explanation of three stages.\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\u003eThis problem contains boards that each require any number of moves to solve. Recall that lights are activated through three stages: on1 (1) to on2 (2) to off (0). For example, if\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 = [1 1 0 2 2  \\n          1 0 0 0 2  \\n          0 0 0 0 0  \\n          0 2 2 0 0  \\n          2 1 1 2 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\u003ethe answer is:\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[ moves = [1 1 10 15 21]]]\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\u003eUp to two moves are possible for each button (index).\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\u003ePrev.:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44766\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 3 stages, \u0026lt;7 moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e — Next:\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44769\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e5x5, 4 stages, x moves\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:\"lamp\"","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:\"lamp\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"lamp\"","","\"","lamp","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f74db21b860\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f74db21b7c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f74db219e20\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f74db21bf40\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f74db21ba40\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f74db21b9a0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f74db21b900\u003e":"tag:\"lamp\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f74db21b900\u003e":"tag:\"lamp\""},"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":"search","password":"J3bGPZzQ7asjJcCk","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:\"lamp\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"lamp\"","","\"","lamp","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f74db21b860\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f74db21b7c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f74db219e20\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f74db21bf40\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f74db21ba40\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f74db21b9a0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f74db21b900\u003e":"tag:\"lamp\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f74db21b900\u003e":"tag:\"lamp\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44757,"difficulty_rating":"easy-medium"},{"id":44756,"difficulty_rating":"medium"},{"id":44759,"difficulty_rating":"medium"},{"id":44755,"difficulty_rating":"medium"},{"id":44760,"difficulty_rating":"medium"},{"id":44753,"difficulty_rating":"medium"},{"id":44751,"difficulty_rating":"medium"},{"id":44752,"difficulty_rating":"medium"},{"id":44761,"difficulty_rating":"medium"},{"id":44764,"difficulty_rating":"medium"},{"id":44766,"difficulty_rating":"medium"},{"id":44776,"difficulty_rating":"medium"},{"id":44769,"difficulty_rating":"medium"},{"id":44765,"difficulty_rating":"medium-hard"},{"id":44767,"difficulty_rating":"medium-hard"}]}}