diff --git a/C++ProjectTemplate b/C++ProjectTemplate index c1aa29d..780dadb 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/input b/input new file mode 100644 index 0000000..fe5a85e --- /dev/null +++ b/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 diff --git a/main.cpp b/main.cpp index 8ab7e53..35f0099 100644 --- a/main.cpp +++ b/main.cpp @@ -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 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; - - - } - vf2d originPoint={16,16}; - bool OnUserCreate() override - { - // Called once at the start, so create things here - return true; - } - - 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{originPoint+velocity,5},olc::utils::geom2d::rect{{32,32},{64,32}})) { - originPoint+=velocity; - DrawCircle(originPoint,5); - } else { - DrawCircle(originPoint,5,RED); - } - DrawLine(originPoint,GetMousePos()); - - DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE); - return true; - } +struct Sensor{ + vi2d pos; + int radius; }; - int main() { - Example demo; - if (demo.Construct(128, 120, 8, 8)) - demo.Start(); + std::ifstream file("input"); + std::vectorsensors; + while (file.good()){ + std::string line; + std::getline(file,line); + std::cout<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: "< Beacon Radius: "<>ranges; + for (Sensor s:sensors){ + int distance=std::abs(s.pos.y-targetY); + if (distance=maxX){ + maxX=minX-1; + break; + } + if (ranges[i].first>minX&&ranges[i].second Removed Range "<=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 "<