diff --git a/ProblemSelection.ts b/ProblemSelection.ts index 090a7e9..8535d52 100644 --- a/ProblemSelection.ts +++ b/ProblemSelection.ts @@ -51,12 +51,13 @@ import { Problem24 } from "./Problems/Problem24"; import { Problem25 } from "./Problems/Problem25"; import { Problem26 } from "./Problems/Problem26"; import { Problem27 } from "./Problems/Problem27"; +import { Problem28 } from "./Problems/Problem28"; export class ProblemSelection{ //Holds the valid problem numbers public static PROBLEM_NUMBERS: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - 21, 22, 23, 24, 25, 26, 27]; + 21, 22, 23, 24, 25, 26, 27, 28]; //Returns the problem corresponding to the given problem number public static getProblem(problemNumber: number): Problem{ @@ -89,6 +90,7 @@ export class ProblemSelection{ case 25: problem = new Problem25(); break; case 26: problem = new Problem26(); break; case 27: problem = new Problem27(); break; + case 28: problem = new Problem28(); break; } return problem; } diff --git a/Problems/Problem28.ts b/Problems/Problem28.ts new file mode 100644 index 0000000..5fb07e9 --- /dev/null +++ b/Problems/Problem28.ts @@ -0,0 +1,170 @@ +//ProjectEulerTS/Problems/Problem27.ts +//Matthew Ellison +// Created: 05-26-21 +//Modified: 05-26-21 +//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses +/* + Copyright (C) 2021 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 . +*/ + + +import { Unsolved } from "../Unsolved"; +import { Problem } from "./Problem"; + + +export class Problem28 extends Problem{ + //Variables + //Static variables + private static grid: number[][]; //Holds the grid that we will be filling and searching + //Instance variables + private sumOfDiagonals: number; //Holds the sum of the diagonals of the grid + + //Functions + //Constructor + public constructor(){ + super(`What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise diration`); + this.sumOfDiagonals = 0; + } + //Operational functions + //Sets up the grid + private setupGrid(): void{ + Problem28.grid = []; + //Fill the grid with 0's + for(let cnt = 0;cnt < 1001;++cnt){ + Problem28.grid.push([]); + for(let cnt2 = 0;cnt2 < 1001;++cnt2){ + Problem28.grid[cnt].push(0); + } + } + + let finalLocation: boolean = false; //A flag to indicate if the final location to be filled has been reached + //Set the number that is going to be put at each location + let currentNum: number = 1; + //Start with the middle location and set it correctly and advance the tracker to the next number + let xLocation: number = 500; + let yLocation: number = 500; + Problem28.grid[yLocation][xLocation] = currentNum++; + //Move right the first time + ++xLocation; + //Move in a circular pattern until you rach the final location + while(!finalLocation){ + //Move down until you reach a blank location on the left + while(Problem28.grid[yLocation][xLocation - 1] != 0){ + Problem28.grid[yLocation][xLocation] = currentNum++; + ++yLocation; + } + + //Move left until you reach a blank location above + while(Problem28.grid[yLocation - 1][xLocation] != 0){ + Problem28.grid[yLocation][xLocation] = currentNum++; + --xLocation; + } + + //Move up until you reach a blank location to the right + while(Problem28.grid[yLocation][xLocation + 1] != 0){ + Problem28.grid[yLocation][xLocation] = currentNum++; + --yLocation; + } + + //Move right until you reach a blank location below + while(Problem28.grid[yLocation + 1][xLocation] != 0){ + Problem28.grid[yLocation][xLocation] = currentNum++; + ++xLocation; + //Check if you are at the final location and break the loop if you are + if(xLocation == Problem28.grid.length){ + finalLocation = true; + break; + } + } + } + } + //Finds the sum of the diagonals in the grid + private findSum(): void{ + //Start at the top corners and work your way down moving toward the opposite side + let leftSide: number = 0; + let rightSide: number = Problem28.grid.length - 1; + let row: number = 0; + while(row < Problem28.grid.length){ + //This ensures the middle location is only counted once + if(leftSide == rightSide){ + this.sumOfDiagonals += Problem28.grid[row][leftSide]; + } + else{ + this.sumOfDiagonals += Problem28.grid[row][leftSide]; + this.sumOfDiagonals += Problem28.grid[row][rightSide]; + } + ++row; + ++leftSide; + --rightSide; + } + } + //Solve the problem + public solve(): void{ + //If the problem has already been solved do nothing and end the function + if(this.solved){ + return; + } + + //Start the timer + this.timer.start(); + + //Setup the grid + this.setupGrid(); + //FInd the sum of the diagonals in the grid + this.findSum(); + + //Stop the timer + this.timer.stop(); + + //Throw a flag to show the problem is solved + this.solved = true; + } + //Reset the problem so it can be run again + public reset(): void{ + super.reset(); + this.sumOfDiagonals = 0; + } + //Gets + //Returns the result of solving the problem + public getResult(): string{ + if(!this.solved){ + throw new Unsolved(); + } + return `The sum of the diagonals in the given grid is ${this.sumOfDiagonals}`; + } + //Returns the grid + public getGrid(): number[][]{ + //IF the problem hasn't been solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return Problem28.grid; + } + //Returns the sum of the diagonals + public getSum(): number{ + //If the problem hasn't been solved throw an exception + if(!this.solved){ + throw new Unsolved(); + } + return this.sumOfDiagonals; + } +} + +/* Results: +The sum of the diagonals in the given grid is 669171001 +It took an average of 16.924 milliseconds to run this problem through 100 iterations +*/