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

93 lines
3.6 KiB

#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
struct Sensor{
vi2d pos;
int radius;
};
int main()
{
std::ifstream file("input");
std::vector<Sensor>sensors;
while (file.good()){
std::string line;
std::getline(file,line);
std::cout<<line<<std::endl;
int prevMarker=0;
int marker=line.find(' ',prevMarker);
vi2d sensorPos={-999,-999};
vi2d beaconPos={-999,-999};
if (line.length()>0){
while (prevMarker!=std::string::npos){
std::string word=line.substr(prevMarker,marker-prevMarker);
prevMarker=marker;
marker=line.find(' ',prevMarker+1);
if (word[1]=='x'){
if (sensorPos.x==-999){
sensorPos.x=std::atoi(word.substr(word.find('=')+1,word.find(',')-word.find('=')-1).c_str());
std::cout<<"Sensor X: "<<sensorPos.x<<std::endl;
} else {
beaconPos.x=std::atoi(word.substr(word.find('=')+1,word.find(',')-word.find('=')-1).c_str());
std::cout<<"Beacon X: "<<beaconPos.x<<std::endl;
}
} else
if (word[1]=='y'){
if (sensorPos.y==-999){
sensorPos.y=std::atoi(word.substr(word.find('=')+1,word.find(',')-word.find('=')-1).c_str());
std::cout<<"Sensor Y: "<<sensorPos.y<<std::endl;
} else {
beaconPos.y=std::atoi(word.substr(word.find('=')+1,word.find(',')-word.find('=')-1).c_str());
std::cout<<"Beacon Y: "<<beaconPos.y<<std::endl;
}
}
}
sensors.push_back({sensorPos,std::abs(beaconPos.x-sensorPos.x)+std::abs(beaconPos.y-sensorPos.y)});
std::cout<<"Sensor -> Beacon Radius: "<<sensors[sensors.size()-1].radius<<std::endl;
std::cout<<std::endl;
}
}
int targetY=2000000;
int sum=0;
std::vector<std::pair<int,int>>ranges;
for (Sensor s:sensors){
int distance=std::abs(s.pos.y-targetY);
if (distance<s.radius){
//In range.
std::cout<<"Sensor "<<s.pos<<" in range of Y "<<targetY<<" Radius: "<<s.radius<<std::endl;
int rangeSize=s.radius*2-distance*2+1;
std::cout<<"Leaves a range of "<<rangeSize<<std::endl;
int minX=(s.pos.x-rangeSize/2);
int maxX=(s.pos.x+rangeSize/2);
std::cout<<"Range is from "<<minX<<" ~ "<<maxX<<std::endl;
for (int i=0;i<ranges.size();i++){
if (ranges[i].first<=minX&&ranges[i].second>=maxX){
maxX=minX-1;
break;
}
if (ranges[i].first>minX&&ranges[i].second<maxX){
sum-=ranges[i].second-ranges[i].first+1;
std::cout<<" --> Removed Range "<<ranges[i].first<<" ~ "<<ranges[i].second<<std::endl;
ranges.erase(ranges.begin()+i--);
}
if (ranges[i].first<=maxX&&ranges[i].second>=maxX){
maxX=ranges[i].first-1;
}
if (ranges[i].first<=minX&&ranges[i].second>=minX){
minX=ranges[i].second+1;
}
}
if (maxX>=minX){
std::cout<<" --> Range is truncated to "<<minX<<" ~ "<<maxX<<std::endl;
sum+=maxX-minX+1;
ranges.push_back({minX,maxX});
}
}
}
std::cout<<"Sum: "<<sum-1<<std::endl;
return 0;
}