Changed Problem 67 to work with 18

This commit is contained in:
2020-07-25 16:02:49 -04:00
parent 3ed2de1dff
commit 44be9aa5b0
5 changed files with 147 additions and 334 deletions

View File

@@ -49,26 +49,29 @@
//Setup the list you are trying to find a path through
std::vector<int> Problem18::list[NUM_ROWS] =
{{75},
{95, 64},
{17, 47, 82},
{18, 35, 87, 10},
{20, 04, 82, 47, 65},
{19, 01, 23, 75, 03, 34},
{88, 02, 77, 73, 07, 63, 67},
{99, 65, 04, 28, 06, 16, 70, 92},
{41, 41, 26, 56, 83, 40, 80, 70, 33},
{41, 48, 72, 33, 47, 32, 37, 16, 94, 29},
{53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14},
{70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57},
{91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48},
{63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31},
{04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23}};
std::vector<std::vector<int>> Problem18::list;
void Problem18::setupList(){
list.push_back({75});
list.push_back({95, 64});
list.push_back({17, 47, 82});
list.push_back({18, 35, 87, 10});
list.push_back({20, 04, 82, 47, 65});
list.push_back({19, 01, 23, 75, 03, 34});
list.push_back({88, 02, 77, 73, 07, 63, 67});
list.push_back({99, 65, 04, 28, 06, 16, 70, 92});
list.push_back({41, 41, 26, 56, 83, 40, 80, 70, 33});
list.push_back({41, 48, 72, 33, 47, 32, 37, 16, 94, 29});
list.push_back({53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14});
list.push_back({70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57});
list.push_back({91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48});
list.push_back({63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31});
list.push_back({04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23});
}
//This list turns every number in the vector into 100 - num
void Problem18::invert(){
for(unsigned int rowCnt = 0;rowCnt < NUM_ROWS;++rowCnt){
for(unsigned int rowCnt = 0;rowCnt < list.size();++rowCnt){
for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){
list[rowCnt][colCnt] = 100 - list[rowCnt][colCnt];
}
@@ -77,6 +80,7 @@ void Problem18::invert(){
//Constructor
Problem18::Problem18() : Problem("Find the maximum total from the top to the bottom of the pyramid."), actualTotal(0){
setupList();
}
//Solve the problem
@@ -119,7 +123,7 @@ void Problem18::solve(){
//If you are at the bottom raise the flag to end the program
int xLoc = minLoc.xLocation;
int yLoc = minLoc.yLocation + 1; //Add one because you will always be moving to the next row
if(yLoc >= NUM_ROWS){
if(yLoc >= (int)list.size()){
foundBottom = true;
}
else{
@@ -129,7 +133,7 @@ void Problem18::solve(){
}
}
actualTotal = ((100 * NUM_ROWS) - foundPoints.back().total); //Change the minimum to the maximum
actualTotal = ((100 * list.size()) - foundPoints.back().total); //Change the minimum to the maximum
invert(); //Invert the list again so that it is back to it's original form
//Stop the timer
@@ -159,7 +163,7 @@ std::string Problem18::getPyramid(){
std::stringstream results;
//Print the triangle list of numbers
for(unsigned int rowCnt = 0;rowCnt < NUM_ROWS;++rowCnt){
for(unsigned int rowCnt = 0;rowCnt < list.size();++rowCnt){
for(unsigned int colCnt = 0;colCnt < list[rowCnt].size();++colCnt){
results << std::setw(2) << list[rowCnt][colCnt] << ' ';
}
@@ -219,7 +223,10 @@ std::string Problem18::getTrail(){
}
for(location loc : trail){
results << list[loc.yLocation][loc.xLocation] << "->";
results << list[loc.yLocation][loc.xLocation];
if(loc.yLocation < (int)(list.size() - 1)){
results << "->";
}
}
return results.str();
}