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

193 lines
7.1 KiB

#include <cmath>
#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;
}
}
const int MAX_VAL=4000000;
std::cout<<"Computing coords..."<<std::endl;
for (Sensor s:sensors){
int y=0;
for (int x=-s.radius;x<=s.radius;x++){
if (x!=-s.radius){
if (x<=0){
y-=1;
}
if (x>0){
y+=1;
}
}
if (std::abs(x)+std::abs(y)==s.radius){
vi2d targetPos={x,y};
if (x<0){
targetPos.x--;
}
if (x>0){
targetPos.x++;
}
if (x==0){
if (y>0){
targetPos.y++;
}
if (y<0){
targetPos.y--;
}
}
if (s.pos.x+targetPos.x>=0&&s.pos.x+targetPos.x<=MAX_VAL&&
s.pos.y+targetPos.y>=0&&s.pos.y+targetPos.y<=MAX_VAL){
vi2d target=s.pos+targetPos;
for (Sensor ss:sensors){
if (std::abs(target.x-ss.pos.x)+std::abs(target.y-ss.pos.y)<=ss.radius){
goto next;
}
}
std::cout<<"Target location found w/no signal! "<<target<<std::endl;
std::cout<<" Tuning frequency:"<<(long)target.x*4000000l+(long)target.y<<std::endl;
}
targetPos={x,y};
if (y<0){
targetPos.y--;
}
if (y>0){
targetPos.y++;
}
if (y==0){
if (x>0){
targetPos.x++;
}
if (x<0){
targetPos.x--;
}
}
if (s.pos.x+targetPos.x>=0&&s.pos.x+targetPos.x<=MAX_VAL&&
s.pos.y+targetPos.y>=0&&s.pos.y+targetPos.y<=MAX_VAL){
vi2d target=s.pos+targetPos;
for (Sensor ss:sensors){
if (std::abs(target.x-ss.pos.x)+std::abs(target.y-ss.pos.y)<=ss.radius){
goto next;
}
}
std::cout<<"Target location found w/no signal! "<<target<<std::endl;
std::cout<<" Tuning frequency:"<<(long)target.x*4000000l+(long)target.y<<std::endl;
}
next:;
}
}
y=0;
for (int x=-s.radius;x<=s.radius;x++){
if (x!=-s.radius){
if (x<=0){
y+=1;
}
if (x>0){
y-=1;
}
}
if (std::abs(x)+std::abs(y)==s.radius){
vi2d targetPos={x,y};
if (x<0){
targetPos.x--;
}
if (x>0){
targetPos.x++;
}
if (x==0){
if (y>0){
targetPos.y++;
}
if (y<0){
targetPos.y--;
}
}
if (s.pos.x+targetPos.x>=0&&s.pos.x+targetPos.x<=MAX_VAL&&
s.pos.y+targetPos.y>=0&&s.pos.y+targetPos.y<=MAX_VAL){
vi2d target=s.pos+targetPos;
for (Sensor ss:sensors){
if (std::abs(target.x-ss.pos.x)+std::abs(target.y-ss.pos.y)<=ss.radius){
goto next2;
}
}
std::cout<<"Target location found w/no signal! "<<target<<std::endl;
std::cout<<" Tuning frequency:"<<(long)target.x*4000000l+(long)target.y<<std::endl;
}
targetPos={x,y};
if (y<0){
targetPos.y--;
}
if (y>0){
targetPos.y++;
}
if (y==0){
if (x>0){
targetPos.x++;
}
if (x<0){
targetPos.x--;
}
}
if (s.pos.x+targetPos.x>=0&&s.pos.x+targetPos.x<=MAX_VAL&&
s.pos.y+targetPos.y>=0&&s.pos.y+targetPos.y<=MAX_VAL){
vi2d target=s.pos+targetPos;
for (Sensor ss:sensors){
if (std::abs(target.x-ss.pos.x)+std::abs(target.y-ss.pos.y)<=ss.radius){
goto next2;
}
}
std::cout<<"Target location found w/no signal! "<<target<<std::endl;
std::cout<<" Tuning frequency:"<<(long)target.x*4000000l+(long)target.y<<std::endl;
}
next2:;
}
}
std::cout<<" Sensor "<<s.pos<<" complete..."<<std::endl;
}
return 0;
}