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

132 lines
5.8 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
std::map<std::string,bool>blocks;
std::map<std::string,bool>trappedBlocks;
int minX=0,maxX=0,minY=0,maxY=0,minZ=0,maxZ=0;
bool escape(int x,int y,int z,std::map<std::string,bool>visited){
visited[std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z)]=true;
if (x<=minX||x>=maxX||y<=minY||y>=maxY||z<=minZ||z>=maxZ){
return true;
} else {
if (blocks.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z))!=blocks.end()){
return false;
}
if (visited.find(std::to_string(x+1)+"_"+std::to_string(y)+"_"+std::to_string(z))==visited.end()&&escape(x+1,y,z,visited)||
visited.find(std::to_string(x-1)+"_"+std::to_string(y)+"_"+std::to_string(z))==visited.end()&&escape(x-1,y,z,visited)||
visited.find(std::to_string(x)+"_"+std::to_string(y+1)+"_"+std::to_string(z))==visited.end()&&escape(x,y+1,z,visited)||
visited.find(std::to_string(x)+"_"+std::to_string(y-1)+"_"+std::to_string(z))==visited.end()&&escape(x,y-1,z,visited)||
visited.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z+1))==visited.end()&&escape(x,y,z+1,visited)||
visited.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z-1))==visited.end()&&escape(x,y,z-1,visited)){
return true;
} else {
return false;
}
}
}
2 years ago
int main()
2 years ago
{
std::ifstream file("input");
int surfaceArea=0;
while (file.good()){
std::string line;
std::getline(file,line);
if (line.length()>0){
//std::cout<<line<<std::endl;
int marker=0;
int x=std::atoi(line.substr(marker,line.find(',',marker)-marker).c_str());
marker=line.find(',',marker)+1;
int y=std::atoi(line.substr(marker,line.find(',',marker)-marker).c_str());
marker=line.find(',',marker)+1;
int z=std::atoi(line.substr(marker,line.find(',',marker)-marker).c_str());
std::cout<<x<<","<<y<<","<<z<<std::endl;
minX=std::min(x,minX);
maxX=std::max(x,maxX);
minY=std::min(y,minY);
maxY=std::max(y,maxY);
minZ=std::min(z,minZ);
maxZ=std::max(z,maxZ);
if (blocks.find(std::to_string(x)+"_"+std::to_string(y-1)+"_"+std::to_string(z))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
if (blocks.find(std::to_string(x)+"_"+std::to_string(y+1)+"_"+std::to_string(z))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
if (blocks.find(std::to_string(x-1)+"_"+std::to_string(y)+"_"+std::to_string(z))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
if (blocks.find(std::to_string(x+1)+"_"+std::to_string(y)+"_"+std::to_string(z))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
if (blocks.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z-1))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
if (blocks.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z+1))==blocks.end()){
surfaceArea++;
} else {
surfaceArea--;
}
blocks[std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z)]=true;
std::cout<<surfaceArea<<std::endl;
}
2 years ago
}
int trappedSurfaceArea=0;
for (int x=-minX;x<=maxX;x++){
for (int y=-minY;y<=maxY;y++){
for (int z=-minZ;z<=maxZ;z++){
if (!escape(x,y,z,{})){
trappedBlocks[std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z)]=true;
std::cout<<"Block "<<std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z)<<" is trapped"<<std::endl;
if (trappedBlocks.find(std::to_string(x)+"_"+std::to_string(y-1)+"_"+std::to_string(z))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
if (trappedBlocks.find(std::to_string(x)+"_"+std::to_string(y+1)+"_"+std::to_string(z))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
if (trappedBlocks.find(std::to_string(x-1)+"_"+std::to_string(y)+"_"+std::to_string(z))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
if (trappedBlocks.find(std::to_string(x+1)+"_"+std::to_string(y)+"_"+std::to_string(z))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
if (trappedBlocks.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z-1))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
if (trappedBlocks.find(std::to_string(x)+"_"+std::to_string(y)+"_"+std::to_string(z+1))==trappedBlocks.end()){
trappedSurfaceArea++;
} else {
trappedSurfaceArea--;
}
}
}
2 years ago
}
}
std::cout<<surfaceArea-trappedSurfaceArea<<std::endl;
2 years ago
return 0;
}