aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-15 08:42:50 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-15 08:42:50 -0500
commite54a0068c9fe99832a85cd459cd31f2b8967f45a (patch)
treed0259d6ba1b72bc85cbb41a004557a4171061a34
parentb6e660ac61a927e517b355cd8d20ddcabc3c9b75 (diff)
day15: finish part 2 c++
-rw-r--r--day15/part2.cpp65
1 files changed, 37 insertions, 28 deletions
diff --git a/day15/part2.cpp b/day15/part2.cpp
index 6a80e54..708f389 100644
--- a/day15/part2.cpp
+++ b/day15/part2.cpp
@@ -7,49 +7,58 @@ struct Sensor
{
long x, y, power;
- Sensor(long xx, long yy, long pp):
+ Sensor(long xx, long yy, long pp = 0):
x(xx), y(yy), power(pp) {}
};
-int main()
+std::vector<Sensor> sensors;
+
+bool isDistressBeacon(const Sensor& c)
{
- std::vector<Sensor> sensors;
- long px, py, bx, by;
+ if (c.x < 0 || c.x > 4000000 || c.y < 0 || c.y > 4000000)
+ return false;
+
+ bool found = true;
+ uint64_t freq = 0;
- do {
- std::string line;
- std::getline(std::cin, line);
- if (std::cin.eof())
+ for (auto& s : sensors) {
+ auto dist = std::abs(c.x - s.x) + std::abs(c.y - s.y);
+ if (dist <= s.power) {
+ found = false;
break;
+ }
+ }
+
+ return found;
+}
+int main()
+{
+ long px, py, bx, by;
+
+ std::string line;
+ std::getline(std::cin, line);
+ for (; !std::cin.eof(); std::getline(std::cin, line)) {
auto a = sscanf(line.c_str(),
"Sensor at x=%ld, y=%ld: closest beacon is at x=%ld, y=%ld",
&px, &py, &bx, &by);
long power = std::abs(px - bx) + std::abs(py - by);
sensors.emplace_back(px, py, power);
- } while (1);
-
- std::cout << "# of sensors: " << sensors.size() << std::endl;
-
- for (long y = 0; y <= 4000000; ++y) {
- std::cout << "\r \r" << y;
- fflush(stdout);
- for (long x = 0; x <= 4000000; ++x) {
- bool h = false;
-
- for (auto& s : sensors) {
- auto dist = std::abs(x - s.x) + std::abs(y - s.y);
- if (dist <= s.power) {
- long spr = (dist - std::abs(s.y - y));
- x += spr - std::abs(s.x - x);
- h = true;
- break;
- }
+ }
+
+ for (auto& s : sensors) {
+ for (long q = 0; q <= (s.power + 1) * 2; ++q) {
+ auto x = s.x + q - s.power - 1;
+ auto y = s.y + q;
+ if (isDistressBeacon(Sensor(x, y))) {
+ std::cout << x * 4000000 + y << std::endl;
+ return 0;
}
- if (!h) {
- std::cout << std::endl << x << ", " << y << std::endl;
+ y -= q * 2;
+ if (isDistressBeacon(Sensor(x, y))) {
+ std::cout << x * 4000000 + y << std::endl;
return 0;
}
}