mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution to problem 15
This commit is contained in:
@@ -37,3 +37,4 @@ pub mod Problem11;
|
|||||||
pub mod Problem12;
|
pub mod Problem12;
|
||||||
pub mod Problem13;
|
pub mod Problem13;
|
||||||
pub mod Problem14;
|
pub mod Problem14;
|
||||||
|
pub mod Problem15;
|
||||||
|
|||||||
79
src/Problems/Problem15.rs
Normal file
79
src/Problems/Problem15.rs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
//ProjectEulerRust/src/Problems/Problems15.rs
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 06-16-20
|
||||||
|
//Modified: 06-16-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/RustClasses
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
extern crate myClasses;
|
||||||
|
use crate::Problems::Answer::Answer;
|
||||||
|
|
||||||
|
|
||||||
|
static WIDTH: i32 = 20;
|
||||||
|
static LENGTH: i32 = 20;
|
||||||
|
|
||||||
|
pub fn getDescription() -> String{
|
||||||
|
"Which starting number, under one million, produces the longest chain using the itterative sequence?".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve() -> Answer{
|
||||||
|
//Setup the rest of the variables
|
||||||
|
let currentX = 0; //The current x location on the grid
|
||||||
|
let currentY = 0; //The current y location on the grid
|
||||||
|
let mut numOfRoutes = 0i64;
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
|
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
//We write this as a recursive function
|
||||||
|
//When in a location it always move right first, then down
|
||||||
|
moveLoc(currentX, currentY, &mut numOfRoutes);
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
|
timer.stop();
|
||||||
|
|
||||||
|
//Return the results
|
||||||
|
return Answer::new(format!("The number of routes is {}", numOfRoutes), timer.getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
//This function acts as a handler for moving the position on the grid and counting the distance
|
||||||
|
//It moves right first, then down
|
||||||
|
fn moveLoc(currentX: i32, currentY: i32, numOfRoutes: &mut i64){
|
||||||
|
//Check if you are at the end and act accordingly
|
||||||
|
if((currentX == WIDTH) && (currentY == LENGTH)){
|
||||||
|
*numOfRoutes += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Move right if possible
|
||||||
|
if(currentX < WIDTH){
|
||||||
|
moveLoc(currentX + 1, currentY, numOfRoutes);
|
||||||
|
}
|
||||||
|
//Move down if possible{
|
||||||
|
if(currentY < LENGTH){
|
||||||
|
moveLoc(currentX, currentY + 1, numOfRoutes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Results:
|
||||||
|
The number of routes is 137846528820
|
||||||
|
It took 11.804 minutes to solve this problem
|
||||||
|
*/
|
||||||
10
src/main.rs
10
src/main.rs
@@ -33,7 +33,8 @@ mod Problems;
|
|||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
|
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
|
||||||
|
|
||||||
static problemNumbers: [u32; 15] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
|
static problemNumbers: [u32; 16] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
10, 11, 12, 13, 14, 15];
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
let mut selection = Selections::EMPTY;
|
let mut selection = Selections::EMPTY;
|
||||||
@@ -186,6 +187,10 @@ fn solveProblem(problemNumber: u32){
|
|||||||
println!("{}", Problems::Problem14::getDescription());
|
println!("{}", Problems::Problem14::getDescription());
|
||||||
println!("{}", Problems::Problem14::solve());
|
println!("{}", Problems::Problem14::solve());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 15){
|
||||||
|
println!("{}", Problems::Problem15::getDescription());
|
||||||
|
println!("{}", Problems::Problem15::solve());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn descriptionMenu(){
|
fn descriptionMenu(){
|
||||||
//Give some extra space to print the description
|
//Give some extra space to print the description
|
||||||
@@ -253,6 +258,9 @@ fn printDescription(problemNumber: u32){
|
|||||||
else if(problemNumber == 14){
|
else if(problemNumber == 14){
|
||||||
println!("{}", Problems::Problem14::getDescription());
|
println!("{}", Problems::Problem14::getDescription());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 15){
|
||||||
|
println!("{}", Problems::Problem15::getDescription());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn getProblemNumber() -> u32{
|
fn getProblemNumber() -> u32{
|
||||||
println!("Enter a problem number: ");
|
println!("Enter a problem number: ");
|
||||||
|
|||||||
Reference in New Issue
Block a user