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/main.cpp

154 lines
5.8 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
void tailFollow(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
//std::cout<<head<<"//"<<tail<<std::endl;
for (int i=1;i<9;i++){
if (std::abs(head[i-1].x-head[i].x)>=2&&std::abs(head[i-1].y-head[i].y)>=1||
std::abs(head[i-1].x-head[i].x)>=1&&std::abs(head[i-1].y-head[i].y)>=2){
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
} else
if (std::abs(head[i-1].x-head[i].x)>=2){
//std::cout<<" "<<"Move X: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
} else
if (std::abs(head[i-1].y-head[i].y)>=2){
//std::cout<<" "<<"Move Y: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
}
2 years ago
}
if (std::abs(head[8].x-tail.x)>=2&&std::abs(head[8].y-tail.y)>=1||
std::abs(head[8].x-tail.x)>=1&&std::abs(head[8].y-tail.y)>=2){
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
tail.y+=(head[8].y-tail.y)/std::abs(head[8].y-tail.y);
} else
if (std::abs(head[8].x-tail.x)>=2){
//std::cout<<" "<<"Move X: "<<std::endl;
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
} else
if (std::abs(head[8].y-tail.y)>=2){
//std::cout<<" "<<"Move Y: "<<std::endl;
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
tail.y+=(head[8].y-tail.y)/std::abs(head[8].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;
}
2 years ago
void DrawMap(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
int maxX=tail.x;
int maxY=tail.y;
int minX=tail.x;
int minY=tail.y;
for (int i=0;i<9;i++){
maxX=std::max(maxX,head[i].x);
maxY=std::max(maxY,head[i].y);
minX=std::min(minX,head[i].x);
minY=std::min(minY,head[i].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());
2 years ago
}
}
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;
}
for (int i=0;i<9;i++){
if (head[i].x==x&&head[i].y==y){
std::cout<<i;
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;
}
}
2 years ago
int main()
{
std::ifstream file("input");
std::map<std::string,bool>posmap;
vi2d tail={0,0};
std::array<vi2d,9>head={vi2d{0,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[0].y++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'R':{
while (amt>0){
head[0].x++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'L':{
while (amt>0){
head[0].x--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'U':{
while (amt>0){
head[0].y--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
}
//DrawMap(tail,head,posmap);
}
}
DrawMap(tail,head,posmap);
std::cout<<"Key count:"<<posmap.size()<<std::endl;
2 years ago
return 0;
}