Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 485489de10
commit 0b6e4fd76a
  1. BIN
      C++ProjectTemplate
  2. 158
      main.cpp
  3. 93
      part1

Binary file not shown.

@ -1,3 +1,4 @@
#include <cmath>
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "olcutils.h" #include "olcutils.h"
@ -50,44 +51,143 @@ int main()
std::cout<<std::endl; std::cout<<std::endl;
} }
} }
int targetY=2000000; const int MAX_VAL=4000000;
int sum=0; std::cout<<"Computing coords..."<<std::endl;
std::vector<std::pair<int,int>>ranges;
for (Sensor s:sensors){ for (Sensor s:sensors){
int distance=std::abs(s.pos.y-targetY); int y=0;
if (distance<s.radius){ for (int x=-s.radius;x<=s.radius;x++){
//In range. if (x!=-s.radius){
std::cout<<"Sensor "<<s.pos<<" in range of Y "<<targetY<<" Radius: "<<s.radius<<std::endl; if (x<=0){
int rangeSize=s.radius*2-distance*2+1; y-=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){ if (x>0){
sum-=ranges[i].second-ranges[i].first+1; y+=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){ if (std::abs(x)+std::abs(y)==s.radius){
minX=ranges[i].second+1; vi2d targetPos={x,y};
if (x<0){
targetPos.x--;
} }
if (x>0){
targetPos.x++;
} }
if (maxX>=minX){ if (x==0){
std::cout<<" --> Range is truncated to "<<minX<<" ~ "<<maxX<<std::endl; if (y>0){
sum+=maxX-minX+1; targetPos.y++;
ranges.push_back({minX,maxX});
} }
if (y<0){
targetPos.y--;
} }
} }
std::cout<<"Sum: "<<sum-1<<std::endl; 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; return 0;
} }

93
part1

@ -0,0 +1,93 @@
#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;
}
Loading…
Cancel
Save