mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
163 lines
6.5 KiB
Java
163 lines
6.5 KiB
Java
//ProjectEuler/Java/Problem18.java
|
|
//Matthew Ellison
|
|
// Created: 03-11-19
|
|
//Modified: 03-28-18
|
|
//Find the maximum total from top to bottom
|
|
/*
|
|
75
|
|
95 64
|
|
17 47 82
|
|
18 35 87 10
|
|
20 04 82 47 65
|
|
19 01 23 75 03 34
|
|
88 02 77 73 07 63 67
|
|
99 65 04 28 06 16 70 92
|
|
41 41 26 56 83 40 80 70 33
|
|
41 48 72 33 47 32 37 16 94 29
|
|
53 71 44 65 25 43 91 52 97 51 14
|
|
70 11 33 28 77 73 17 78 39 68 17 57
|
|
91 71 52 38 17 14 91 43 58 50 27 29 48
|
|
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
|
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
|
*/
|
|
//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
|
|
|
|
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
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
import mattrixwv.Stopwatch;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
public class Problem18{
|
|
//The number of rows in the array
|
|
private static final Integer NUM_ROWS = 15;
|
|
//Used to keep track of where the best location came from
|
|
private static class location{
|
|
public Integer xLocation;
|
|
public Integer yLocation;
|
|
public Integer total;
|
|
public Boolean fromRight;
|
|
location(Integer x, Integer y, Integer t, Boolean r){
|
|
xLocation = x;
|
|
yLocation = y;
|
|
total = t;
|
|
fromRight = r;
|
|
}
|
|
}
|
|
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
|
private static void invert(ArrayList<ArrayList<Integer>> 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 static void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
|
|
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
|
|
}
|
|
public static void main(String[] argv){
|
|
//Setup the timer
|
|
Stopwatch timer = new Stopwatch();
|
|
timer.start();
|
|
|
|
//Setup the list you are trying to find a path through
|
|
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
|
|
list.add(new ArrayList<Integer>(Arrays.asList(75)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(95, 64)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(17, 47, 82)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(18, 35, 87, 10)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(20, 04, 82, 47, 65)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(19, 01, 23, 75, 03, 34)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(88, 02, 77, 73, 07, 63, 67)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(99, 65, 04, 28, 06, 16, 70, 92)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(41, 41, 26, 56, 83, 40, 80, 70, 33)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(41, 48, 72, 33, 47, 32, 37, 16, 94, 29)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31)));
|
|
list.add(new ArrayList<Integer>(Arrays.asList(04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23)));
|
|
|
|
//Invert the list
|
|
invert(list);
|
|
|
|
ArrayList<location> foundPoints = new ArrayList<location>(); //For the points that I have already found the shortest distance to
|
|
foundPoints.add(new location(0, 0, list.get(0).get(0), false)); //Add the first row as a found point because you have to go through the tip
|
|
ArrayList<location> possiblePoints = new ArrayList<location>(); //For the locations you are checking this round
|
|
//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
|
|
|
|
//Loop until you find the bottom of the list
|
|
while(!foundBottom){
|
|
//Check which possible point gives us the lowest number. If more than one has the same number simply keep the first one
|
|
location minLoc = possiblePoints.get(0);
|
|
for(location loc : possiblePoints){
|
|
if(loc.total < minLoc.total){
|
|
minLoc = loc;
|
|
}
|
|
}
|
|
|
|
//Remove it from the list of possible points
|
|
removeHelper(possiblePoints, minLoc);
|
|
|
|
//Add that point to the list of found points
|
|
foundPoints.add(minLoc);
|
|
|
|
//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
|
|
if(yLoc >= NUM_ROWS){
|
|
foundBottom = true;
|
|
}
|
|
else{
|
|
possiblePoints.add(new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), true));
|
|
++xLoc; //Advance the x location to simulate going right
|
|
//Check if x is out of bounds
|
|
if(xLoc < list.get(yLoc).size()){
|
|
possiblePoints.add( new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), false));
|
|
}
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//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);
|
|
|
|
System.out.println("The value of the longest path is " + actualTotal.toString());
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The value of the longest path is 1074
|
|
It took 5.146 milliseconds to run this algorithm
|
|
*/
|