This is stuck

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 17807eb031
commit 36236a54be
  1. BIN
      C++ProjectTemplate
  2. 41
      input
  3. 175
      main.cpp
  4. 5
      testinput
  5. 36
      testinput2

Binary file not shown.

41
input

@ -0,0 +1,41 @@
abacccaaaacccccccccccaaaaaacccccaaaaaaccccaaacccccccccccccccccccccccccccccccccccccccccccaaaaa
abaaccaaaacccccccccccaaaaaaccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa
abaaccaaaacccccccccccaaaaacccccaaaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa
abccccccccccccccccccccaaaaacccaaaaaaaaaaaaaaaacccccccccccccccccccccccccccaaaccccccccccccaaaaa
abccccccccccccccccccccaacaacccaaaaaaaaccaaaaaccccccccccccccccccccccccccccaaaccccccccccccaccaa
abcccccccccccccaacccaaaccccccaaaaaaaaaccaaaaaccccccccccccccccccccccccccccccacccccccccccccccca
abcccccccccccaaaaaaccaaaccacccccaaaaaaacccccccccccccccccccccccccciiiicccccccddddddccccccccccc
abcccccccccccaaaaaaccaaaaaaaccccaaaaaacccccaacccccccaaaccccccccciiiiiiiicccdddddddddacaaccccc
abccccccccccccaaaaaaaaaaaaacccccaaaaaaacaaaacccccccaaaacccccccchhiiiiiiiiicddddddddddaaaccccc
abcccccccccccaaaaaaaaaaaaaacccccccaaacccaaaaaacccccaaaaccccccchhhipppppiiiijjjjjjjddddaaccccc
abcccccccccccaaaaaaaaaaaaaaccccccccccccccaaaaaccccccaaaccccccchhhpppppppiijjjjjjjjjddeeaccccc
abcccccccccccccccccaaaaaaaacccccccccccccaaaaaccccccccccccccccchhppppppppppjjqqqjjjjjeeeaacccc
abccccccccccccccccccaaaaaaaacccccccccccccccaacccccccccccccccchhhpppuuuupppqqqqqqqjjjeeeaacccc
abcccccccccccccccccccaacccacccccccccccccccccccccccccccccccccchhhopuuuuuuppqqqqqqqjjjeeecccccc
abacccccccccccccaaacaaaccccccccccccccccccccccccccccaaccccccchhhhoouuuuuuuqvvvvvqqqjkeeecccccc
abaccccccccccccaaaaaacccccaaccccccccccccccccccccccaaaccccccchhhooouuuxxxuvvvvvvqqqkkeeecccccc
abaccccccccccccaaaaaacccaaaaaaccccccccccccccccccaaaaaaaaccchhhhooouuxxxxuvyyyvvqqqkkeeecccccc
abcccccccccccccaaaaacccaaaaaaaccccccccccccccccccaaaaaaaaccjjhooooouuxxxxyyyyyvvqqqkkeeecccccc
abccccccccccccccaaaaaacaaaaaaaccccccccaaaccccccccaaaaaaccjjjooootuuuxxxxyyyyyvvqqkkkeeecccccc
abccccccccccccccaaaaaaaaaaaaacccccccccaaaacccccccaaaaaacjjjooootttuxxxxxyyyyvvrrrkkkeeecccccc
SbccccccccccccccccccaaaaaaaaacccccccccaaaacccccccaaaaaacjjjoootttxxxEzzzzyyvvvrrrkkkfffcccccc
abcccccccccccaaacccccaaaaaaacaaaccccccaaaccccccccaaccaacjjjoootttxxxxxyyyyyyvvvrrkkkfffcccccc
abcccccccccaaaaaacccaaaaaacccaaacacccaacccccccccccccccccjjjoootttxxxxyxyyyyyywvvrrkkkfffccccc
abcccccccccaaaaaacccaaaaaaaaaaaaaaaccaaacaaacccccaacccccjjjnnnttttxxxxyyyyyyywwwrrkkkfffccccc
abcaacacccccaaaaacccaaacaaaaaaaaaaaccaaaaaaacccccaacaaacjjjnnnntttttxxyywwwwwwwwrrrlkfffccccc
abcaaaaccccaaaaacccccccccaacaaaaaaccccaaaaaacccccaaaaacccjjjnnnnnttttwwywwwwwwwrrrrllfffccccc
abaaaaaccccaaaaaccccccaaaaaccaaaaacaaaaaaaaccccaaaaaaccccjjjjinnnntttwwwwwsssrrrrrllllffccccc
abaaaaaaccccccccccccccaaaaacaaaaaacaaaaaaaaacccaaaaaaacccciiiiinnnntswwwwssssrrrrrlllfffccccc
abacaaaaccccccccccccccaaaaaacaaccccaaaaaaaaaaccccaaaaaaccccciiiinnnssswwsssssllllllllfffccccc
abccaaccccccccccccccccaaaaaaccccccccccaaacaaaccccaaccaacccccciiiinnsssssssmmllllllllfffaacccc
abccccccccccccccccccccaaaaaaccccccccccaaaccccccccaaccccccccccciiinnmsssssmmmmlllllgggffaacccc
abcccccccccccccccaccccccaaacccccccccccaaccccccccccccccccccccccciiimmmsssmmmmmgggggggggaaacccc
abcccccccccaaaaaaaaccccccccccccccccccccccccccccaaaaaccccccccccciiimmmmmmmmmgggggggggaaacccccc
abccccccccccaaaaaaccccccccccccccccccaacccccccccaaaaacccccccccccciiimmmmmmmhhggggcaaaaaaaccccc
abccccccccccaaaaaacccccccccccccccccaacccccccccaaaaaacccccccccccciihhmmmmhhhhgccccccccaacccccc
abccccaacaaaaaaaaaaccccccccccccccccaaaccccccccaaaaaaccccccccccccchhhhhhhhhhhaaccccccccccccccc
abccccaaaaaaaaaaaaaaccccccccccaaccaaaaccccccccaaaaaacccaaacccccccchhhhhhhhaaaaccccccccccccccc
abcccaaaaaaaaaaaaaaaccccccccaaaaaacaaaacacaccccaaaccccaaaacccccccccchhhhccccaaccccccccccaaaca
abcccaaaaaacacaaacccccccccccaaaaaaaaaaaaaaacccccccccccaaaacccccccccccaaaccccccccccccccccaaaaa
abcccccaaaacccaaaccccccccccaaaaaaaaaaaaaaaaccccccccccccaaacccccccccccaaacccccccccccccccccaaaa
abcccccaacccccaacccccccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaa

@ -4,86 +4,111 @@
using namespace olc; using namespace olc;
class Example : public olc::PixelGameEngine std::vector<int> validPathStepCounts;
{ vi2d playerPos={0,0};
public: vi2d endingPos={0,0};
Example() std::vector<std::vector<char>>grid;
{ int lowest=2468;
sAppName = "Example";
} void step(char currentTile,vi2d pos,int xoffset,int yoffset,int stepCount,std::vector<std::vector<char>>visitedgrid,std::string currentPath){
pos.x+=xoffset;
public: pos.y+=yoffset;
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){ std::cout<<currentPath<<std::endl;
if (currentTile=='S'){
contact_normal = { 0, 0 }; currentTile='a';
contact_point = { 0, 0 };
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
if (t_hit_far < 0) return false;
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
} }
vf2d originPoint={16,16}; if (stepCount+abs(pos.x-endingPos.x)+abs(pos.y-endingPos.y)>lowest){
bool OnUserCreate() override //std::cout<<"Took too long..."<<std::endl;
{ return;
// Called once at the start, so create things here }
return true; 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;
bool OnUserUpdate(float fElapsedTime) override }
{ 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')){
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime}; currentPath+=visitedgrid[pos.y][pos.x];
vf2d contact_point; if (visitedgrid[pos.y][pos.x]=='E'){
vf2d contact_normal; validPathStepCounts.push_back(++stepCount);
float t_hit_near; if (stepCount<lowest){
lowest=stepCount;
Clear(Pixel(64,64,255)); std::cout<<"Valid path found in "<<stepCount<<" steps! ("<<currentPath<<")"<<std::endl;
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) { }
originPoint+=velocity; return;
DrawCircle(originPoint,5);
} else {
DrawCircle(originPoint,5,RED);
} }
DrawLine(originPoint,GetMousePos()); ++stepCount;
char newTile=visitedgrid[pos.y][pos.x];
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE); visitedgrid[pos.y][pos.x]='0';
return true; 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);
}
}
}
int main() int main()
{ {
Example demo; std::ifstream file("testinput2");
if (demo.Construct(128, 120, 8, 8)) while (file.good()){
demo.Start(); 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;
return 0; return 0;
} }

@ -0,0 +1,5 @@
Sabqponm
abcryxxl
accszExk
acctuvwj
abdefghi

@ -0,0 +1,36 @@
ccccccccccccccccccccccccccacccccccccccccccca
cccccccccccccccciiiicccccccddddddccccccccccc
cccaaaccccccccciiiiiiiicccdddddddddacaaccccc
ccaaaacccccccchhiiiiiiiiicddddddddddaaaccccc
ccaaaaccccccchhhipppppiiiijjjjjjjddddaaccccc
cccaaaccccccchhhpppppppiijjjjjjjjjddeeaccccc
ccccccccccccchhppppppppppjjqqqjjjjjeeeaacccc
cccccccccccchhhpppuuuupppqqqqqqqjjjeeeaacccc
cccccccccccchhhopuuuuuuppqqqqqqqjjjeeecccccc
ccaaccccccchhhhoouuuuuuuqvvvvvqqqjkeeecccccc
caaaccccccchhhooouuuxxxuvvvvvvqqqkkeeecccccc
aaaaaaaccchhhhooouuxxxxuvyyyvvqqqkkeeecccccc
aaaaaaaccjjhooooouuxxxxyyyyyvvqqqkkeeecccccc
aaaaaaccjjjooootuuuxxxxyyyyyvvqqkkkeeecccccc
aaaaaacjjjooootttuxxxxxyyyyvvrrrkkkeeecccccc
aaaaaacjjjoootttxxxEzzzzyyvvvrrrkkkfffcccccc
aaccaacjjjoootttxxxxxyyyyyyvvvrrkkkfffcccccc
cccccccjjjoootttxxxxyxyyyyyywvvrrkkkfffccccc
aacccccjjjnnnttttxxxxyyyyyyywwwrrkkkfffccccc
Sabaaacjjjnnnntttttxxyywwwwwwwwrrrlkfffccccc
aaaaacccjjjnnnnnttttwwywwwwwwwrrrrllfffccccc
aaaaccccjjjjinnnntttwwwwwsssrrrrrllllffccccc
aaaaacccciiiiinnnntswwwwssssrrrrrlllfffccccc
aaaaaaccccciiiinnnssswwsssssllllllllfffccccc
aaccaacccccciiiinnsssssssmmllllllllfffaacccc
aaccccccccccciiinnmsssssmmmmlllllgggffaacccc
cccccccccccccciiimmmsssmmmmmgggggggggaaacccc
aaaccccccccccciiimmmmmmmmmgggggggggaaacccccc
aaacccccccccccciiimmmmmmmhhggggcaaaaaaaccccc
aaacccccccccccciihhmmmmhhhhgccccccccaacccccc
aaaccccccccccccchhhhhhhhhhhaaccccccccccccccc
aaacccaaacccccccchhhhhhhhaaaaccccccccccccccc
accccaaaacccccccccchhhhccccaaccccccccccaaaca
cccccaaaacccccccccccaaaccccccccccccccccaaaaa
ccccccaaacccccccccccaaacccccccccccccccccaaaa
cccccccccccccccccccccccccccccccccccccccaaaaa
Loading…
Cancel
Save