diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java index f3b3c40..fa22a08 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java @@ -48,6 +48,8 @@ import java.util.Arrays; public class Problem18 extends Problem{ //The number of rows in the array private static final int NUM_ROWS = 15; + //The list to hold the numbers in + private ArrayList> list; //Used to keep track of where the best location came from private static class location{ public int xLocation; @@ -82,12 +84,8 @@ public class Problem18 extends Problem{ private void removeHelper(ArrayList possiblePoints, final location minLoc){ possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation))); } - public void solve(){ - //Start the timer - timer.start(); - - //Setup the list you are trying to find a path through - ArrayList> list = new ArrayList>(); + private void setupList(){ + list = new ArrayList>(); list.add(new ArrayList(Arrays.asList(75))); list.add(new ArrayList(Arrays.asList(95, 64))); list.add(new ArrayList(Arrays.asList(17, 47, 82))); @@ -103,6 +101,13 @@ public class Problem18 extends Problem{ list.add(new ArrayList(Arrays.asList(91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48))); list.add(new ArrayList(Arrays.asList(63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31))); list.add(new ArrayList(Arrays.asList(04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23))); + } + public void solve(){ + //Start the timer + timer.start(); + + //Setup the list you are trying to find a path through + setupList(); //Invert the list invert(list); diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java index 783b7d0..bf810b1 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java @@ -1,7 +1,7 @@ -//ProjectEuler/Java/Problem67.java +//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java //Matthew Ellison // Created: 03-26-19 -//Modified: 03-28-19 +//Modified: 06-18-20 //Find the maximum total from top to bottom /* 59 @@ -108,7 +108,7 @@ //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 /* - Copyright (C) 2019 Matthew Ellison + Copyright (C) 2020 Matthew Ellison 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 @@ -132,19 +132,19 @@ import java.util.Arrays; public class Problem67 extends Problem{ //The number of rows in the array - private static final Integer NUM_ROWS = 100; + private static final int NUM_ROWS = 100; //Setup the list you are trying to find a path through private ArrayList> list = new ArrayList>(); //Used to keep track of where the best location came from private class location{ - public Integer xLocation; - public Integer yLocation; - public Integer total; + public int xLocation; + public int yLocation; + public int total; //Originally used in debugging @SuppressWarnings("unused") - public Boolean fromRight; - location(Integer x, Integer y, Integer t, Boolean r){ + public boolean fromRight; + location(int x, int y, int t, boolean r){ xLocation = x; yLocation = y; total = t; @@ -154,7 +154,23 @@ public class Problem67 extends Problem{ public Problem67(){ super("Find the maximum total from top to bottom"); - + } + //This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest + private void invert(ArrayList> list){ + //Loop through every row in the list + for(int rowCnt = 0;rowCnt < list.size();++rowCnt){ + //Loop through every column in the list + for(int colCnt = 0;colCnt < list.get(rowCnt).size();++colCnt){ + //The current element gets the value of 100 - value + list.get(rowCnt).set(colCnt, 100 - list.get(rowCnt).get(colCnt)); + } + } + } + //This function helps by removing the element that is the same as the minimum location + private void removeHelper(ArrayList possiblePoints, final location minLoc){ + possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation))); + } + private void setupList(){ list.ensureCapacity(NUM_ROWS); list.add(new ArrayList(Arrays.asList(59))); list.add(new ArrayList(Arrays.asList(73, 41))); @@ -257,22 +273,13 @@ public class Problem67 extends Problem{ list.add(new ArrayList(Arrays.asList(30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68))); list.add(new ArrayList(Arrays.asList(23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35))); } - //This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest - private void invert(ArrayList> list){ - //Loop through every row in the list - for(int rowCnt = 0;rowCnt < list.size();++rowCnt){ - //Loop through every column in the list - for(int colCnt = 0;colCnt < list.get(rowCnt).size();++colCnt){ - //The current element gets the value of 100 - value - list.get(rowCnt).set(colCnt, 100 - list.get(rowCnt).get(colCnt)); - } - } - } - //This function helps by removing the element that is the same as the minimum location - private void removeHelper(ArrayList possiblePoints, final location minLoc){ - possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation))); - } public void solve(){ + //Start the timer + timer.start(); + + //Setup the list you are trying to find a path through + setupList(); + //Invert the list invert(list); @@ -286,7 +293,7 @@ public class Problem67 extends Problem{ //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(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 while(!foundBottom){ @@ -306,8 +313,8 @@ public class Problem67 extends Problem{ //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 - Integer xLoc = minLoc.xLocation; - Integer yLoc = minLoc.yLocation + 1; //Add one because you will always be moving to the next row + int xLoc = minLoc.xLocation; + int yLoc = minLoc.yLocation + 1; //Add one because you will always be moving to the next row if(yLoc >= NUM_ROWS){ foundBottom = true; } @@ -326,13 +333,13 @@ public class Problem67 extends Problem{ //Print the results //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 = "The value of the longest path is " + actualTotal.toString(); + result = "The value of the longest path is " + actualTotal; } } /* Results: The value of the longest path is 7273 -It took 0.000 nanoseconds to solve this problem. +It took 175.694 milliseconds to solve this problem. */