Commit ede95118 authored by AYUSH RAJ's avatar AYUSH RAJ

Lab01

parents
function [b] = adj(k,l) %this function returns 1 if k th and l th cells are adjacent and zero otherwise
row_k = floor(k/3); %computing the corresponding row and column corresponding to k th and l th cell
row_l = floor(l/3);
col_k = mod(k,3);
col_l = mod(l,3);
if((row_k-row_l)^2+(col_k-col_l)^2 <= 1)
b = 1;
else
b = 0;
endif
endfunction
function [B] = final_state_3(initial,action) % initial is the matrix to be updated after the operating action
A = genmatrix(); % generate a matrix (9*9) where (i,j)th entry is 1 if i th and j th cells are adjacent and 0 otherwise
action = action';
action = reshape(action,9,1);
action = (A*action);
action = reshape(action,3,3);
action = action';
B = mod((initial + action),2); %computing the effect of action by using the logic given at http://www.ueda.info.waseda.ac.jp/~n-kato/lightsout/
return;
endfunction
\ No newline at end of file
function [A] = genmatrix() %this function generates a matrix (9*9) where (i,j)th entry is 1 if i th and j th cells are adjacent and 0 otherwise
A = zeros(9);
for i=0:8
for j=0:8
if ( adj(i,j) == 1)
A(i+1,j+1)= 1;
endif
end
end
%display(A)
endfunction
%A = floor(2*rand(3,3)); % make an abritrary input
%disp('The matrix to be solved is:')
%disp(A); % print it
function [answer] = lookup_solve_3(A) % takes A as an arguement
lookup_matrix = [3 7 4 6 5 1 2]; % decimal form of the given table
answer = zeros(3); % the required final action matrix
dec_equiv = 1; % to enter the below loop for first time
while dec_equiv > 0 % checks decimal equivalent of the last row
for variable = [2 3]
action = zeros(3); % action matrix : 0 means no flipping of switch
action(variable,:) = A(variable-1,:); % ith row of action matrix is updated with the (i-1)th row of input A
A = final_state_3(A,action);
answer = answer+action; % each action matrix is added to this final answer matrix
end
dec_equiv = 4*A(3,1)+2*A(3,2)+A(3,3); % calculates decimal equivalent of the last binary row.
if(dec_equiv>0)
num = lookup_matrix(dec_equiv); % num is the corresponding decimal from the lookup_matrix
C = zeros(3,1);
for var = [3 2 1] % breaking num into binary form and storing it in the row vector C
C(var) = mod(num,2);
num = floor(num/2);
end
%change A/
action1 = zeros(3);
action1(1,:) = C; % action to be taken in the first row
A = final_state_3(A,action1); % state of A after action1
answer = answer+action1; % updating the final answer matrix
answer = mod(answer,2);
endif
endwhile
answer = mod(answer,2);
%disp('The answer is :')
%disp(answer);
return
endfunction
Group No - 29
Group Name - puzzle
(Ayush Raj , 150050042):100%
(Umesh Kumar , 150050052):100%
(Ankan Sardar , 150050064):100%
Honor Code:
" We pledge on our honor that we have not given or received any unauthorised assistance for this assignment or any previous task!"
citations:
https://en.wikibooks.org/wiki/Octave_Programming_Tutorial
http://www.ueda.info.waseda.ac.jp/~n-kato/lightsout/
http://www.gnu.org/software/octave/octave.pdf
http://www.dm.unibo.it/~lenci/teaching/14/maa/octavetut.pdf
Reflection Essay:
We got to know an algorithmic solution to "lights Out" puzzle. We also learnt the implementations of various operations on matrices in octave environment and how to write functions in octave.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment