Added functionality to find the trail the algorithm took

This commit is contained in:
2020-07-24 13:21:52 -04:00
parent b9eac741f2
commit bf1860856e

View File

@@ -43,6 +43,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import mattrixwv.ProjectEuler.Unsolved;
@@ -55,7 +56,6 @@ public class Problem18 extends Problem{
public int yLocation;
public int total;
//Used originally for debugging so I could trace the path backwards
@SuppressWarnings("unused")
public boolean fromRight;
location(int x, int y, int t, boolean r){
xLocation = x;
@@ -213,8 +213,58 @@ public class Problem18 extends Problem{
}
StringBuilder results = new StringBuilder();
//TODO: Implement this
results.append("This has not yet been implemented");
//Save the trail the algorithm took
ArrayList<location> trail = new ArrayList<location>();
trail.add(foundPoints.get(foundPoints.size() - 1));
boolean top = false;
while(!top){
boolean found = false;
int loc = foundPoints.size() - 1;
location toAdd = null;
while(!found){
if(loc < 0){
results.append("Error: Location < 0\n");
System.exit(1);
}
Iterator<location> it = foundPoints.iterator();
location tempLoc = null;
for(int cnt = 0;cnt < loc;++cnt){
tempLoc = it.next();
}
if(trail.get(0).fromRight){
if((tempLoc.xLocation == trail.get(0).xLocation) && (tempLoc.yLocation == (trail.get(0).yLocation - 1))){
found = true;
toAdd = tempLoc;
}
else{
--loc;
}
}
else{
if((tempLoc.xLocation == (trail.get(0).xLocation - 1)) && (tempLoc.yLocation == (trail.get(0).yLocation - 1))){
found = true;
toAdd = tempLoc;
}
else{
--loc;
}
}
}
trail.add(0, toAdd);
if(trail.get(0).yLocation == 0){
top = true;
}
}
for(location loc : trail){
results.append(list.get(loc.yLocation).get(loc.xLocation));
if(loc.yLocation < (list.size() - 1)){
results.append("->");
}
}
return results.toString();
}
//Returns the total that was asked for