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_Day9/part1

135 lines
4.9 KiB

#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
void tailFollow(vi2d&tail,vi2d&head,std::map<std::string,bool>&posmap){
//std::cout<<head<<"//"<<tail<<std::endl;
if (std::abs(head.x-tail.x)>=2&&std::abs(head.y-tail.y)>=1||
std::abs(head.x-tail.x)>=1&&std::abs(head.y-tail.y)>=2){
tail.x+=(head.x-tail.x)/std::abs(head.x-tail.x);
tail.y+=(head.y-tail.y)/std::abs(head.y-tail.y);
} else
if (std::abs(head.x-tail.x)>=2){
//std::cout<<" "<<"Move X: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head<<std::endl;
tail.x+=(head.x-tail.x)/std::abs(head.x-tail.x);
} else
if (std::abs(head.y-tail.y)>=2){
//std::cout<<" "<<"Move Y: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head<<std::endl;
tail.y+=(head.y-tail.y)/std::abs(head.y-tail.y);
}
//std::cout<<"Added "<<std::to_string(tail.x)+"_"+std::to_string(tail.y)<<std::endl;
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
}
void DrawMap(vi2d&tail,vi2d&head,std::map<std::string,bool>&posmap){
int maxX=tail.x;
int maxY=tail.y;
int minX=tail.x;
int minY=tail.y;
maxX=std::max(maxX,head.x);
maxY=std::max(maxY,head.y);
minX=std::min(minX,head.x);
minY=std::min(minY,head.y);
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())>maxX){
maxX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())>maxY){
maxY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())<minX){
minX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())<minY){
minY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
}
}
for (int y=minY;y<=maxX;y++){
for (int x=minX;x<=maxX;x++){
if (tail.x==x&&tail.y==y){
std::cout<<"T";
continue;
}
if (head.x==x&&head.y==y){
std::cout<<"H";
goto next;
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())==x&&
std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())==y){
std::cout<<"#";
goto next;
}
}
std::cout<<".";
next:;
}
std::cout<<std::endl;
}
}
int main()
{
std::ifstream file("SPOILER_day9_input.txt");
std::map<std::string,bool>posmap;
vi2d tail={0,0};
vi2d head={0,0};
int iterationCount=0;
while (file.good()){
std::string line;
std::getline(file,line);
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
if (line.length()>0){
char dir=line[0];
int amt=std::atoi(line.substr(1,std::string::npos).c_str());
//std::cout<<dir<<" "<<amt<<std::endl;
switch (dir){
case 'D':{
while (amt>0){
head.y++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'R':{
while (amt>0){
head.x++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'L':{
while (amt>0){
head.x--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'U':{
while (amt>0){
head.y--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
}
std::cout<<"Iteration "<<iterationCount++<<": T-"<<tail<<" H-"<<head<<" Tiles Explored: "<<posmap.size()<<std::endl;
//DrawMap(tail,head,posmap);
}
}
DrawMap(tail,head,posmap);
std::cout<<"Key count:"<<posmap.size()<<std::endl;
return 0;
}