You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AoC2022_Day12/main.cpp

115 lines
4.3 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
std::vector<int> validPathStepCounts;
vi2d playerPos={0,0};
vi2d endingPos={0,0};
std::vector<std::vector<char>>grid;
int lowest=2468;
void step(char currentTile,vi2d pos,int xoffset,int yoffset,int stepCount,std::vector<std::vector<char>>visitedgrid,std::string currentPath){
pos.x+=xoffset;
pos.y+=yoffset;
std::cout<<currentPath<<std::endl;
if (currentTile=='S'){
currentTile='a';
2 years ago
}
if (stepCount+abs(pos.x-endingPos.x)+abs(pos.y-endingPos.y)>lowest){
//std::cout<<"Took too long..."<<std::endl;
return;
}
if (pos.y<0||pos.x<0||pos.x>=visitedgrid[0].size()||pos.y>=visitedgrid.size()){
//std::cout<<"Dead end reached in "<<stepCount<<" steps ("<<currentPath<<")"<<std::endl;
return;
}
if (visitedgrid[pos.y][pos.x]!='0'&&(visitedgrid[pos.y][pos.x]!='E'&&visitedgrid[pos.y][pos.x]-currentTile<=1||visitedgrid[pos.y][pos.x]=='E'&&currentTile>='y')){
currentPath+=visitedgrid[pos.y][pos.x];
if (visitedgrid[pos.y][pos.x]=='E'){
validPathStepCounts.push_back(++stepCount);
if (stepCount<lowest){
lowest=stepCount;
std::cout<<"Valid path found in "<<stepCount<<" steps! ("<<currentPath<<")"<<std::endl;
}
return;
2 years ago
}
++stepCount;
char newTile=visitedgrid[pos.y][pos.x];
visitedgrid[pos.y][pos.x]='0';
if (pos.y+1<visitedgrid.size()&&visitedgrid[pos.y+1][pos.x]!='0'&&yoffset!=-1){
//visitedgrid[pos.y+1][pos.x]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,0,1,stepCount,newvisitedgrid,currentPath);
}
if (pos.y-1>=0&&visitedgrid[pos.y-1][pos.x]!='0'&&yoffset!=1){
//visitedgrid[pos.y-1][pos.x]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,0,-1,stepCount,newvisitedgrid,currentPath);
}
if (pos.x-1>=0&&visitedgrid[pos.y][pos.x-1]!='0'&&xoffset!=1){
//visitedgrid[pos.y][pos.x-1]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,-1,0,stepCount,newvisitedgrid,currentPath);
}
if (pos.x+1<visitedgrid[0].size()&&visitedgrid[pos.y][pos.x+1]!='0'&&xoffset!=-1){
//visitedgrid[pos.y][pos.x+1]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,1,0,stepCount,newvisitedgrid,currentPath);
}
}
}
2 years ago
int main()
{
std::ifstream file("testinput2");
while (file.good()){
std::string line;
std::getline(file,line);
std::vector<char>row;
if (line.length()>0){
for (int x=0;x<line.length();x++){
row.push_back(line[x]);
if (line[x]=='S'){
playerPos={x,(int)grid.size()};
}
if (line[x]=='E'){
endingPos={x,(int)grid.size()};
}
}
grid.push_back(row);
}
}
std::cout<<"================"<<std::endl;
for (int y=0;y<grid.size();y++){
for (int x=0;x<grid[y].size();x++){
std::cout<<grid[y][x];
}
std::cout<<std::endl;
}
std::cout<<"Player Pos: "<<playerPos<<std::endl;
char newTile=grid[playerPos.y][playerPos.x];
grid[playerPos.y][playerPos.x]='0';
std::vector<std::vector<char>>visitedgrid=grid;
std::vector<std::vector<char>>visitedgrid2=grid;
std::vector<std::vector<char>>visitedgrid3=grid;
std::vector<std::vector<char>>visitedgrid4=grid;
std::cout<<"Part 1"<<std::endl;
step(newTile,playerPos,1,0,0,visitedgrid,"S");
std::cout<<"Part 2"<<std::endl;
step(newTile,playerPos,-1,0,0,visitedgrid2,"S");
std::cout<<"Part 3"<<std::endl;
step(newTile,playerPos,0,1,0,visitedgrid3,"S");
std::cout<<"Part 4"<<std::endl;
step(newTile,playerPos,0,-1,0,visitedgrid4,"S");
std::sort(validPathStepCounts.begin(),validPathStepCounts.end());
for (int i=0;i<validPathStepCounts.size();i++){
std::cout<<validPathStepCounts[i]<<std::endl;
}
std::cout<<"Best: "<<validPathStepCounts[0]<<std::endl;
2 years ago
return 0;
}