mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Inital commit with existing files
This commit is contained in:
160
Source/Problem28.cpp
Normal file
160
Source/Problem28.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
//ProjectEuler/C++/Source/Problem28.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-21-19
|
||||
//Modified: 09-21-19
|
||||
//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/myClasses
|
||||
/*
|
||||
Copyright (C) 2019 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <cinttypes>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include "../Headers/Problem28.hpp"
|
||||
|
||||
|
||||
//Sets up the grid
|
||||
void Problem28::setupGrid(){
|
||||
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.at(yLocation).at(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.at(yLocation).at(xLocation - 1) != 0){
|
||||
grid.at(yLocation).at(xLocation) = currentNum++;
|
||||
++yLocation;
|
||||
}
|
||||
|
||||
//Move left until you reach a blank location above
|
||||
while(grid.at(yLocation - 1).at(xLocation) != 0){
|
||||
grid.at(yLocation).at(xLocation) = currentNum++;
|
||||
--xLocation;
|
||||
}
|
||||
|
||||
//Move up until you reach a blank location to the right
|
||||
while(grid.at(yLocation).at(xLocation + 1) != 0){
|
||||
grid.at(yLocation).at(xLocation) = currentNum++;
|
||||
--yLocation;
|
||||
}
|
||||
|
||||
//Move right until you reach a blank location below
|
||||
while(grid.at(yLocation + 1).at(xLocation) != 0){
|
||||
grid.at(yLocation).at(xLocation) = currentNum++;
|
||||
++xLocation;
|
||||
//Check if you are at the final location and break the loop if you are
|
||||
if(xLocation == (int)grid.size()){
|
||||
finalLocation = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finds the sum of the diagonals in the grid
|
||||
void Problem28::findSum(){
|
||||
//Start at the top corners and work your way down moving toward the opposite side
|
||||
unsigned int leftSide = 0;
|
||||
unsigned int rightSide = grid.size() - 1;
|
||||
unsigned int row = 0;
|
||||
while(row < grid.size()){
|
||||
//This ensures the middle location is only counted once
|
||||
if(leftSide == rightSide){
|
||||
sumOfDiagonals += grid.at(row).at(leftSide);
|
||||
}
|
||||
else{
|
||||
sumOfDiagonals += grid.at(row).at(leftSide);
|
||||
sumOfDiagonals += grid.at(row).at(rightSide);
|
||||
}
|
||||
++row;
|
||||
++leftSide;
|
||||
--rightSide;
|
||||
}
|
||||
}
|
||||
|
||||
Problem28::Problem28(){
|
||||
//Set the size of the grid to 1001 x 1001
|
||||
for(int cnt = 0;cnt < 1001;++cnt){
|
||||
grid.emplace_back();
|
||||
for(int location = 0;location < 1001;++location){
|
||||
grid.at(cnt).push_back(0);
|
||||
}
|
||||
}
|
||||
sumOfDiagonals = 0;
|
||||
}
|
||||
|
||||
void Problem28::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;
|
||||
}
|
||||
|
||||
std::string Problem28::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
|
||||
std::stringstream results;
|
||||
results << "The sum of the diagonals in the given grid is " << sumOfDiagonals;
|
||||
|
||||
return results.str();
|
||||
}
|
||||
|
||||
//Returns the grid
|
||||
std::vector<std::vector<int>> Problem28::getGrid() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
//Returns the sum of the diagonals
|
||||
uint64_t Problem28::getSum() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return sumOfDiagonals;
|
||||
}
|
||||
Reference in New Issue
Block a user