Added solution to problem 15

This commit is contained in:
2020-08-25 09:32:24 -04:00
parent 6b8c6e0266
commit 5f486dec23
2 changed files with 103 additions and 1 deletions

View File

@@ -31,7 +31,7 @@ namespace ProjectEulerCS{
//Holds the valid problem numbers
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14};
10, 11, 12, 13, 14, 15};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; }
}
@@ -54,6 +54,7 @@ namespace ProjectEulerCS{
case 12: problem = new Problem12(); break;
case 13: problem = new Problem13(); break;
case 14: problem = new Problem14(); break;
case 15: problem = new Problem15(); break;
}
return problem;
}

View File

@@ -0,0 +1,101 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem15.cs
//Matthew Ellison
// Created: 08-25-20
//Modified: 08-25-20
//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?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/*
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
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/>.
*/
namespace ProjectEulerCS.Problems{
public class Problem15 : Problem{
//Variables
//Static variables
private const int WIDTH = 20; //The width of the box to traverse
private const int LENGTH = 20; //The height of the box to traverse
//Instance variables
private long numOfRoutes; //The number of routes from 0, 0 to 20, 20
public long NumberOfRoutes{
get{
if(!solved){
throw new Unsolved();
}
return numOfRoutes;
}
}
//Functions
//Constructor
public Problem15() : base("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?"){
numOfRoutes = 0;
}
//Operational functions
//Solve the problems
public override void Solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
return;
}
//Start the timer
_timer.Start();
//We write this as a recursive function
//When in a location it always moves right first, then down
Move(0, 0);
//Stop the timer
_timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
//Save the results
_result = "The number of routes is " + numOfRoutes;
}
//This function acts as a handler for moving the position on the grid and counting the distance
//It moves right first, then down
private void Move(int currentX, int currentY){
//Check if you are at the end and act accordingly
if((currentX == WIDTH) && (currentY == LENGTH)){
++numOfRoutes;
return;
}
//Move right if possible
if(currentX < WIDTH){
Move(currentX + 1, currentY);
}
//Move down if possible
if(currentY < LENGTH){
Move(currentX, currentY + 1);
}
}
//Reset the problem so it can be run again
public override void Reset(){
base.Reset();
numOfRoutes = 0;
}
}
}
/* Results
The number of routes is 137846528820
It took 17.328 minutes to solve this problem.
*/