Updated for performance

This commit is contained in:
2020-06-16 02:40:33 -04:00
parent 53a77b3816
commit d6682ab2cf

View File

@@ -71,7 +71,7 @@ public class Problem11 extends Problem{
{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, {04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16}, {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}, {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}};; {01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
public Problem11(){ public Problem11(){
super("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?"); super("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?");
@@ -92,12 +92,12 @@ public class Problem11 extends Problem{
timer.start(); timer.start();
//Loop through every row and column //Loop through every row and column
for(Integer row = 0;row < 20;++row){ for(int row = 0;row < grid.length;++row){
for(Integer col = 0;col < grid[row].length;++col){ for(int col = 0;col < grid[row].length;++col){
//Directional booleans to show whether you can move a certain direction //Directional booleans to show whether you can move a certain direction
Boolean left = false; boolean left = false;
Boolean right = false; boolean right = false;
Boolean down = false; boolean down = false;
//Check which direction you will be able to move //Check which direction you will be able to move
if((col - 3) >= 1){ if((col - 3) >= 1){
@@ -189,5 +189,5 @@ public class Problem11 extends Problem{
/* Results: /* Results:
The greatest product of 4 numbers in a line is 70600674 The greatest product of 4 numbers in a line is 70600674
The numbers are [89, 94, 97, 87] The numbers are [89, 94, 97, 87]
It took 1.310 milliseconds to solve this problem. It took 268.600 microseconds to solve this problem.
*/ */