Oh god what was this...
This commit is contained in:
parent
0a95f517ce
commit
91adcabc8b
3 changed files with 135 additions and 56 deletions
BIN
day02/day2Part1
BIN
day02/day2Part1
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
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;
|
||||
|
|
|
@ -1,46 +1,37 @@
|
|||
// The A* implementation used in this code is
|
||||
// a modified form of Dijkstra's Algorithm, as
|
||||
// see on the YouTube channel "Friendly Developer"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <list>
|
||||
|
||||
#define INF 10
|
||||
#define INFINITY 1000000;
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
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
|
||||
{
|
||||
int arrayXsize, arrayYsize;
|
||||
bool finalCode = false;
|
||||
if (!finalCode)
|
||||
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<sNode*> vecNeighbors; // Neighbors
|
||||
sNode* parent; // Node we came through to get here
|
||||
};
|
||||
|
||||
void mapImport()
|
||||
{
|
||||
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<pair<int,int>> pathThruMap;
|
||||
int lineLength = 0;
|
||||
int pathCost = 0;
|
||||
// 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;
|
||||
|
@ -49,22 +40,108 @@ int main(void)
|
|||
|
||||
while(getline(readFile, rawInput))
|
||||
{
|
||||
for(int xIndex = 0;
|
||||
for(xIndex = 0;
|
||||
xIndex < rawInput.size();
|
||||
xIndex++)
|
||||
{
|
||||
tempInt = rawInput[xIndex]-'0';
|
||||
map[yIndex][xIndex] = tempInt;
|
||||
costMap[yIndex][xIndex] = tempInt;
|
||||
}
|
||||
yIndex++;
|
||||
}
|
||||
|
||||
// Pairs are basically coordinates
|
||||
pair<int, int> vectorAddPair;
|
||||
// Pairs are initialized as (note namespace);
|
||||
vectorAddPair = make_pair(0,0);
|
||||
// add pair to vector
|
||||
pathThruMap.push_back(vectorAddPair);
|
||||
// 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<sNode*> 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)
|
||||
{
|
||||
AStarSort functionCall;
|
||||
functionCall.mapImport();
|
||||
functionCall.solveAStar();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue