Part 1 complete!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 44f4e7abfd
commit 485489de10
  1. BIN
      C++ProjectTemplate
  2. 33
      input
  3. 152
      main.cpp
  4. 14
      testinput

Binary file not shown.

33
input

@ -0,0 +1,33 @@
Sensor at x=1555825, y=18926: closest beacon is at x=1498426, y=-85030
Sensor at x=697941, y=3552290: closest beacon is at x=595451, y=3788543
Sensor at x=3997971, y=2461001: closest beacon is at x=3951198, y=2418718
Sensor at x=3818312, y=282332: closest beacon is at x=4823751, y=1061753
Sensor at x=2874142, y=3053631: closest beacon is at x=3074353, y=3516891
Sensor at x=1704479, y=2132468: closest beacon is at x=1749091, y=2000000
Sensor at x=3904910, y=2080560: closest beacon is at x=3951198, y=2418718
Sensor at x=657061, y=3898803: closest beacon is at x=595451, y=3788543
Sensor at x=3084398, y=3751092: closest beacon is at x=3074353, y=3516891
Sensor at x=2582061, y=972407: closest beacon is at x=1749091, y=2000000
Sensor at x=2886721, y=3971936: closest beacon is at x=3074353, y=3516891
Sensor at x=303399, y=548513: closest beacon is at x=-1010425, y=986825
Sensor at x=3426950, y=2290126: closest beacon is at x=3951198, y=2418718
Sensor at x=3132858, y=3592272: closest beacon is at x=3074353, y=3516891
Sensor at x=3773724, y=3751243: closest beacon is at x=3568452, y=3274758
Sensor at x=3987212, y=2416515: closest beacon is at x=3951198, y=2418718
Sensor at x=61559, y=3806326: closest beacon is at x=595451, y=3788543
Sensor at x=2693503, y=2291389: closest beacon is at x=2269881, y=2661753
Sensor at x=3953437, y=2669220: closest beacon is at x=3951198, y=2418718
Sensor at x=763035, y=3997568: closest beacon is at x=595451, y=3788543
Sensor at x=3999814, y=2370103: closest beacon is at x=3951198, y=2418718
Sensor at x=1290383, y=1696257: closest beacon is at x=1749091, y=2000000
Sensor at x=3505508, y=2805537: closest beacon is at x=3568452, y=3274758
Sensor at x=3276207, y=3323122: closest beacon is at x=3568452, y=3274758
Sensor at x=2244609, y=3517499: closest beacon is at x=3074353, y=3516891
Sensor at x=1370860, y=3436382: closest beacon is at x=595451, y=3788543
Sensor at x=3831063, y=3042662: closest beacon is at x=3568452, y=3274758
Sensor at x=551202, y=3971545: closest beacon is at x=595451, y=3788543
Sensor at x=336629, y=2519780: closest beacon is at x=595451, y=3788543
Sensor at x=2033602, y=2882628: closest beacon is at x=2269881, y=2661753
Sensor at x=3939808, y=2478271: closest beacon is at x=3951198, y=2418718
Sensor at x=1958708, y=2370822: closest beacon is at x=1749091, y=2000000
Sensor at x=3039958, y=3574483: closest beacon is at x=3074353, y=3516891

@ -4,86 +4,90 @@
using namespace olc;
class Example : public olc::PixelGameEngine
{
public:
Example()
{
sAppName = "Example";
}
public:
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
contact_normal = { 0, 0 };
contact_point = { 0, 0 };
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
if (t_hit_far < 0) return false;
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
struct Sensor{
vi2d pos;
int radius;
};
}
vf2d originPoint={16,16};
bool OnUserCreate() override
int main()
{
// Called once at the start, so create things here
return true;
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;
}
bool OnUserUpdate(float fElapsedTime) override
{
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
vf2d contact_point;
vf2d contact_normal;
float t_hit_near;
Clear(Pixel(64,64,255));
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
originPoint+=velocity;
DrawCircle(originPoint,5);
} 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 {
DrawCircle(originPoint,5,RED);
beaconPos.y=std::atoi(word.substr(word.find('=')+1,word.find(',')-word.find('=')-1).c_str());
std::cout<<"Beacon Y: "<<beaconPos.y<<std::endl;
}
DrawLine(originPoint,GetMousePos());
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
return true;
}
};
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
}
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;
}

@ -0,0 +1,14 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3
Loading…
Cancel
Save