diff --git a/day02/day2Part1 b/day02/day2Part1 index 5b82bf9..6f5e7b7 100755 Binary files a/day02/day2Part1 and b/day02/day2Part1 differ diff --git a/day02/day2Part1.cpp b/day02/day2Part1.cpp index 63082dc..5c81596 100644 --- a/day02/day2Part1.cpp +++ b/day02/day2Part1.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace std; int main (void) @@ -28,6 +29,7 @@ int main (void) cout << "Error!!!"; } } + cout << std::filesystem::current_path() << endl; finalAnswer = totalVertical * totalForward; cout << finalAnswer; return 0; diff --git a/day15/day15Part1.cpp b/day15/day15Part1.cpp index 916875f..99e6a20 100644 --- a/day15/day15Part1.cpp +++ b/day15/day15Part1.cpp @@ -1,70 +1,147 @@ -// The A* implementation used in this code is -// a modified form of Dijkstra's Algorithm, as -// see on the YouTube channel "Friendly Developer" #include #include #include #include #include #include +#include -#define INF 10 +#define INFINITY 1000000; using namespace std; +const int arrayXsize = 10; // 10 for testing, 100 for final +const int arrayYsize = 10; // 10 for testing, 100 for final +int costMap[arrayYsize][arrayXsize]; + +class AStarSort { + public: + struct sNode + { + bool bVisited = false; // Has this node been visited? + int iDistRemain; // Distance remaining to goal + int iDistTaken; // Distance from start + int iNodeWeight; // Node wieght + int x; // X-coordinate of node + int y; // Y-coordinate of node + vector vecNeighbors; // Neighbors + sNode* parent; // Node we came through to get here + }; + + void mapImport() + { + // The Following code reads in the textfile of choice, and + // makes a 2D array of points with the cost values of each point + int xIndex = 0; + int yIndex = 0; + int tempInt = 0; + string rawInput; + ifstream readFile ("data/testInput.txt", ios::in); + + while(getline(readFile, rawInput)) + { + for(xIndex = 0; + xIndex < rawInput.size(); + xIndex++) + { + tempInt = rawInput[xIndex]-'0'; + costMap[yIndex][xIndex] = tempInt; + } + yIndex++; + } + + // End of file read + + return; + }; + + public: + void solveAStar() + { + // this creates all the nodes needed, and initializes them with + // a location, an unvisited state, no parent, and all neighbors + auto nodes = new sNode[arrayYsize * arrayXsize]; + for (int y = 0; y < arrayYsize; y++) + { + for (int x = 0; x < arrayXsize; x++) + { + nodes[x * arrayXsize + y].x=x; + nodes[x * arrayXsize + y].y=y; + nodes[x * arrayXsize + y].bVisited = false; + nodes[x * arrayXsize + y].iDistRemain = INFINITY; + nodes[x * arrayXsize + y].iDistTaken = INFINITY; + nodes[x * arrayXsize + y].iNodeWeight = costMap[y][x]; + nodes[x * arrayXsize + y].parent = nullptr; + if(y>0) + nodes[x * arrayXsize + y].vecNeighbors.push_back + ( + &nodes[(x + 0) * arrayXsize + (y - 1)] + ); + if(y< arrayYsize - 1) + nodes[x * arrayXsize + y].vecNeighbors.push_back + ( + &nodes[(x + 0) * arrayXsize + (y + 1)] + ); + if(x>0) + nodes[x * arrayXsize + y].vecNeighbors.push_back + ( + &nodes[(x - 1) * arrayXsize + (y - 0)] + ); + if(x< arrayYsize - 1) + nodes[x * arrayXsize + y].vecNeighbors.push_back + ( + &nodes[(x + 1) * arrayXsize + (y + 0)] + ); + } + } + + // This creates the starting and ending nodes, and initializes them + sNode *nodeStart = &nodes[0]; + sNode *nodeEnd = &nodes[ (arrayXsize * arrayYsize) - 1 ]; + + auto manhattanDist = [](sNode* a) + { + return ((arrayXsize - 1) - a->x) + ((arrayYsize - 1) - a->y); + }; + sNode *nodeCurrent = nodeStart; + nodeStart->iDistTaken = 0; + nodeStart->iDistRemain = manhattanDist(nodeStart); + list listNotTestedNodes; + listNotTestedNodes.push_back(nodeStart); + + while(!listNotTestedNodes.empty()) + { + // Sort by order of remaining distance to the goal + listNotTestedNodes.sort([](const sNode* lhs, const sNode* rhs) + { return lhs ->iDistRemain < rhs->iDistRemain; }); + + // Clear out visited nodes + while(!listNotTestedNodes.empty() && listNotTestedNodes.front()->bVisited) + listNotTestedNodes.pop_front(); + + // If list is empty, leave loop + if(listNotTestedNodes.empty()) break; + + // Visit the node at the front of the list + nodeCurrent = listNotTestedNodes.front(); + nodeCurrent->bVisited = true; + + //check each node's neighbors + for (auto nodeNeighbor : nodeCurrent->vecNeighbors) + { + if (!nodeNeighbor->bVisited) + listNotTestedNodes.push_back(nodeNeighbor); + } + } + + return; + }; +}; + int main(void) { - int arrayXsize, arrayYsize; - bool finalCode = false; - if (!finalCode) - { - arrayXsize = 10; - arrayYsize = 10; - } - if (finalCode) - { - arrayXsize = 100; - arrayYsize = 100; - } - int map[arrayYsize][arrayXsize]; - int distMap[arrayYsize][arrayXsize] = {INF}; - int remainingMap[arrayYsize][arrayXsize]; - for (int i = 0; i < arrayYsize; i++) - { - for (int j = 0; j < arrayXsize; j++) - { - //shrink xSize and ySize properly, using manhattan dist. - remainingMap[i][j] = ((arrayXsize - 1) - j) + - ((arrayYsize - 1) - i); - } - } - vector> pathThruMap; - int lineLength = 0; - int pathCost = 0; - int xIndex = 0; - int yIndex = 0; - int tempInt = 0; - string rawInput; - ifstream readFile ("data/testInput.txt", ios::in); - - while(getline(readFile, rawInput)) - { - for(int xIndex = 0; - xIndex < rawInput.size(); - xIndex++) - { - tempInt = rawInput[xIndex]-'0'; - map[yIndex][xIndex] = tempInt; - } - yIndex++; - } - - // Pairs are basically coordinates - pair vectorAddPair; - // Pairs are initialized as (note namespace); - vectorAddPair = make_pair(0,0); - // add pair to vector - pathThruMap.push_back(vectorAddPair); - + AStarSort functionCall; + functionCall.mapImport(); + functionCall.solveAStar(); return 0; }