Finished the 11th problem

This commit is contained in:
2018-09-26 20:14:39 -04:00
parent 98c47adca6
commit 2ddd0053cb

View File

@@ -45,9 +45,108 @@ grid = [08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08;
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16;
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54;
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48];
currentLocation = [1, 1];
currentLocation = [1, 1]; %The location you are checking now
currentProduct = [0 0 0 0]; %The product you are ucrrently looking at
greatestProduct = [0 0 0 0]; %The greatest product of values you have found
finished = false;
%Loop until you reach the last element
startTime = clock();
while(~finished)
left = false;
right = false;
down = false;
%Check which directions you can go
%When moving you will be moving 4 locations
if((currentLocation(2) - 3) >= 1)
left = true;
end
if((currentLocation(2) + 3) <= size(grid)(2))
right = true;
end
if((currentLocation(1) + 3) <= size(grid)(1))
down = true;
end
%Check the possible directions and check against greatest
%Left
if(left)
currentProduct = grid(currentLocation(1),currentLocation(2):-1:(currentLocation(2) - 3));
%If the current numbers' product is greater than the greatest product so far, replace it
if(prod(currentProduct) > prod(greatestProduct))
greatestProduct = currentProduct;
end
end
%Right
if(right)
currentProduct = grid(currentLocation(1), currentLocation(2):(currentLocation(2) + 3));
%If the current numbers' product is greater than the greatest product so far, replace it
if(prod(currentProduct) > prod(greatestProduct))
greatestProduct = currentProduct;
end
end
%Down
if(down)
currentProduct = grid(currentLocation(1):(currentLocation(1) + 3), currentLocation(2));
%If the current numbers' product is greater than the greatest product so far, replace it
if(prod(currentProduct) > prod(greatestProduct))
greatestProduct = currentProduct;
end
end
%LeftDown
if(left && down)
currentProduct(1) = grid(currentLocation(1), currentLocation(2));
currentProduct(2) = grid(currentLocation(1) + 1,currentLocation(2) - 1);
currentProduct(3) = grid(currentLocation(1) + 2,currentLocation(2) - 2);
currentProduct(4) = grid(currentLocation(1) + 3,currentLocation(2) - 3);
%If the current numbers' product is greater than the greatest product so far, replace it
if(prod(currentProduct) > prod(greatestProduct))
greatestProduct = currentProduct;
end
end
%RightDown
if(right && down)
currentProduct(1) = grid(currentLocation(1), currentLocation(2));
currentProduct(2) = grid(currentLocation(1) + 1,currentLocation(2) + 1);
currentProduct(3) = grid(currentLocation(1) + 2,currentLocation(2) + 2);
currentProduct(4) = grid(currentLocation(1) + 3,currentLocation(2) + 3);
%If the current numbers' product is greater than the greatest product so far, replace it
if(prod(currentProduct) > prod(greatestProduct))
greatestProduct = currentProduct;
end
end
%Move to the next column
++currentLocation(2);
%If you have moved too far in the columns move back to the beginning and to the next row
if(currentLocation(2) > size(grid)(2))
currentLocation(2) = 1;
++currentLocation(1);
end
%If the row is currently greater than what is available you have traversed the list
if(currentLocation(1) > size(grid)(1))
finished = true;
end
end
greatestProduct = reshape(greatestProduct, 1, 4); %For some reason it is coming out a 4X1 instead of 1X4
endTime = clock();
%Print the result
totalTime = etime(endTime, startTime)
greatestProduct
prod(greatestProduct)
%Cleanup the variables
clear down;
clear right;
clear left;
clear finished;
clear startTime;
clear endTime;
clear totalTime;
clear grid;
clear greatestProduct;
clear currentLocation;
clear currentProduct;