diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java index c274762..1d9b8e7 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java @@ -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 trail = new ArrayList(); + 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 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