Fixed issue with problem 15 not saving number of routes

This commit is contained in:
2020-06-08 01:59:11 -04:00
parent 534e1ed688
commit 758fa4714e

View File

@@ -29,13 +29,14 @@ public class Problem15 extends Problem{
private static final Integer WIDTH = 20;
//The height of the box to traverse
private static final Integer LENGTH = 20;
//The number of routes from 0, 0 to 20, 20
private Long numOfRoutes = 0L;
public Problem15(){
super("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?");
}
public void solve(){
//Setup the rest of the variables
Long numOfRoutes = 0L; //The number of routes from 0, 0, to 20, 20
Integer currentX = 0; //The current x location on the grid
Integer currentY = 0; //The current y location on the grid
@@ -44,7 +45,7 @@ public class Problem15 extends Problem{
//We write this as a recursive function
//When in a location it always moves right first, then down
move(currentX, currentY, numOfRoutes);
move(currentX, currentY);
//Stop the timer
timer.stop();
@@ -54,7 +55,7 @@ public class Problem15 extends Problem{
}
//This function acts as a handler for moving the position on the grid and counting the distance
//It moves right first, then down
private static void move(Integer currentX, Integer currentY, Long numOfRoutes){
private void move(Integer currentX, Integer currentY){
//Check if you are at the end and act accordingly
if((currentX == WIDTH) && (currentY == LENGTH)){
++numOfRoutes;
@@ -63,12 +64,12 @@ public class Problem15 extends Problem{
//Move right if possible
if(currentX < WIDTH){
move(currentX + 1, currentY, numOfRoutes);
move(currentX + 1, currentY);
}
//Move down if possible
if(currentY < LENGTH){
move(currentX, currentY + 1, numOfRoutes);
move(currentX, currentY + 1);
}
}
}