Updated for performance

This commit is contained in:
2020-06-17 10:45:47 -04:00
parent c3d0d7ded5
commit 43c0df0d22

View File

@@ -1,7 +1,7 @@
//ProjectEuler/Java/Problem18.java //ProjectEuler/Java/Problem18.java
//Matthew Ellison //Matthew Ellison
// Created: 03-11-19 // Created: 03-11-19
//Modified: 03-28-18 //Modified: 06-17-20
//Find the maximum total from top to bottom //Find the maximum total from top to bottom
/* /*
75 75
@@ -23,7 +23,7 @@
//This is done using a breadth first search //This is done using a breadth first search
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/* /*
Copyright (C) 2019 Matthew Ellison Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -47,16 +47,16 @@ import java.util.Arrays;
public class Problem18 extends Problem{ public class Problem18 extends Problem{
//The number of rows in the array //The number of rows in the array
private static final Integer NUM_ROWS = 15; private static final int NUM_ROWS = 15;
//Used to keep track of where the best location came from //Used to keep track of where the best location came from
private static class location{ private static class location{
public Integer xLocation; public int xLocation;
public Integer yLocation; public int yLocation;
public Integer total; public int total;
//Used originally for debugging so I could trace the path backwards //Used originally for debugging so I could trace the path backwards
@SuppressWarnings("unused") @SuppressWarnings("unused")
public Boolean fromRight; public boolean fromRight;
location(Integer x, Integer y, Integer t, Boolean r){ location(int x, int y, int t, boolean r){
xLocation = x; xLocation = x;
yLocation = y; yLocation = y;
total = t; total = t;
@@ -113,7 +113,7 @@ public class Problem18 extends Problem{
//Add the second row as possible points //Add the second row as possible points
possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true)); possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true));
possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false)); possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false));
Boolean foundBottom = false; //Used when you find a point at the bottom of the list boolean foundBottom = false; //Used when you find a point at the bottom of the list
//Loop until you find the bottom of the list //Loop until you find the bottom of the list
while(!foundBottom){ while(!foundBottom){
@@ -133,8 +133,8 @@ public class Problem18 extends Problem{
//Add to the list of possible points from the point we just found and //Add to the list of possible points from the point we just found and
//If you are at the bottom raise the flag to end the program //If you are at the bottom raise the flag to end the program
Integer xLoc = minLoc.xLocation; int xLoc = minLoc.xLocation;
Integer yLoc = minLoc.yLocation + 1; //Add one because you will always be moving to the next row int yLoc = minLoc.yLocation + 1; //Add one because you will always be moving to the next row
if(yLoc >= NUM_ROWS){ if(yLoc >= NUM_ROWS){
foundBottom = true; foundBottom = true;
} }
@@ -153,13 +153,13 @@ public class Problem18 extends Problem{
//Save the results //Save the results
//Get the correct total which will be the inversion of the current one //Get the correct total which will be the inversion of the current one
Integer actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total); int actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
result = String.format("The value of the longest path is " + actualTotal.toString()); result = String.format("The value of the longest path is " + actualTotal);
} }
} }
/* Results: /* Results:
The value of the longest path is 1074 The value of the longest path is 1074
It took 1.585 milliseconds to solve this problem. It took 278.899 microseconds to solve this problem.
*/ */