From 758fa4714ec11387a37d14af139e97014b03e83a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 8 Jun 2020 01:59:11 -0400 Subject: [PATCH] Fixed issue with problem 15 not saving number of routes --- .../mattrixwv/ProjectEuler/Problems/Problem15.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java index 644a355..8898e78 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java @@ -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); } } }