From b47a33a6fb52d7614841d6a53759940b30f1f19b Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 3 Oct 2020 10:55:23 -0400 Subject: [PATCH] Added solution to problem 28 --- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem28.cs | 171 +++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 ProjectEulerCS/Problems/Problem28.cs diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 3982167..573ff23 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -32,7 +32,7 @@ namespace ProjectEulerCS{ private static readonly List _PROBLEM_NUMBERS = new List() { 0, 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, 67}; + 20, 21, 22, 23, 24, 25, 26, 27, 28, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -68,6 +68,7 @@ namespace ProjectEulerCS{ case 25: problem = new Problem25(); break; case 26: problem = new Problem26(); break; case 27: problem = new Problem27(); break; + case 28: problem = new Problem28(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem28.cs b/ProjectEulerCS/Problems/Problem28.cs new file mode 100644 index 0000000..708a70d --- /dev/null +++ b/ProjectEulerCS/Problems/Problem28.cs @@ -0,0 +1,171 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem28.cs +//Matthew Ellison +// Created: 09-21-20 +//Modified: 09-21-20 +//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/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 . +*/ + + +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem28 : Problem{ + //Variables + //Static variables + private static List> grid; //Holds the grid that we will be filling and searching + //Instance variables + private int sumOfDiagonals; //Holds the sum of the diagonals of the grid + public List> Grid{ + get{ + if(!solved){ + throw new Unsolved(); + } + return grid; + } + } + public int Sum{ + get{ + if(!solved){ + throw new Unsolved(); + } + return sumOfDiagonals; + } + } + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The sum of the diagonals in the given grid is {sumOfDiagonals}"; + } + } + + //Functions + //Constructor + public Problem28() : base("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"){ + sumOfDiagonals = 0; + } + //Operational functions + //Sets up the grid + private void SetupGrid(){ + grid = new List>(); + //Fill the grid with 0's + for(int cnt = 0;cnt < 1001;++cnt){ + //Add a blank List + grid.Add(new List()); + for(int cnt2 = 0;cnt2 < 1001;++cnt2){ + grid[cnt].Add(0); + } + } + + bool finalLocation = 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 + int currentNum = 1; + //Start with the middle location and set it correctly and advance the tracker to the next number + int xLocation = 500; + int yLocation = 500; + grid[yLocation][xLocation] = currentNum++; + //Move right the first time + ++xLocation; + //Move in a circular pattern until you reach the final location + while(!finalLocation){ + //Move down until you reach a blank location on the left + while(grid[yLocation][xLocation - 1] != 0){ + grid[yLocation][xLocation] = currentNum++; + ++yLocation; + } + + //Move left until you reach a blank location above + while(grid[yLocation - 1][xLocation] != 0){ + grid[yLocation][xLocation] = currentNum++; + --xLocation; + } + + //Move up until you reach a blank location to the right + while(grid[yLocation][xLocation + 1] != 0){ + grid[yLocation][xLocation] = currentNum++; + --yLocation; + } + + //Move right until you reach a blank location below + while(grid[yLocation + 1][xLocation] != 0){ + grid[yLocation][xLocation] = currentNum++; + ++xLocation; + //Check if you are at the final location and break the loop if you are + if(xLocation == grid.Count){ + finalLocation = true; + break; + } + } + } + } + //Finds the sum of the diagonals in the grid + private void FindSum(){ + //Start at the top corners and work your way down moving toward the opposite side + int leftSide = 0; + int rightSide = grid.Count - 1; + int row = 0; + while(row < grid.Count){ + //This ensures the middle location is only counted once + if(leftSide == rightSide){ + sumOfDiagonals += grid[row][leftSide]; + } + else{ + sumOfDiagonals += grid[row][leftSide]; + sumOfDiagonals += grid[row][rightSide]; + } + ++row; + ++leftSide; + --rightSide; + } + } + //Solve the problem + 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(); + + //Setup the grid + SetupGrid(); + //Find the sum of the diagonals in the grid + FindSum(); + + //Stop the timer + timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the problem so it can be run again + public override void Reset(){ + base.Reset(); + sumOfDiagonals = 0; + } + } +} + +/* Results: +The sum of the diagonals in the given grid is 669171001 +It took an average of 7.735 milliseconds to run this problem through 100 iterations +*/