From b6e660ac61a927e517b355cd8d20ddcabc3c9b75 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <clyne@bitgloo.com>
Date: Thu, 15 Dec 2022 08:15:23 -0500
Subject: day15: part 1 c++, partial part 2

---
 day15/part1.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 day15/part2.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 day15/part1.cpp
 create mode 100644 day15/part2.cpp

(limited to 'day15')

diff --git a/day15/part1.cpp b/day15/part1.cpp
new file mode 100644
index 0000000..fb53753
--- /dev/null
+++ b/day15/part1.cpp
@@ -0,0 +1,50 @@
+#include <cstdio>
+#include <iostream>
+#include <set>
+#include <string>
+
+constexpr long Y = 2000000;
+
+int main()
+{
+    std::set<int> xs;
+    std::set<int> bs;
+    long px, py, bx, by;
+
+    do {
+        std::string line;
+        std::getline(std::cin, line);
+        if (std::cin.eof())
+            break;
+
+        auto a = sscanf(line.c_str(),
+            "Sensor at x=%ld, y=%ld: closest beacon is at x=%ld, y=%ld",
+            &px, &py, &bx, &by);
+
+        std::cout << px << ' ' << py << ' ' << bx << ' ' << by;
+
+        if (by == Y)
+            bs.insert(bx);
+
+        long ic = std::abs(px - bx) + std::abs(py - by);
+
+        if (Y < py - ic || Y > py + ic)
+            continue;
+
+        long spr = (ic - std::abs(py - Y));
+        for (long j = 0; j <= spr; ++j) {
+            xs.insert(px + j);
+            xs.insert(px - j);
+        }
+
+        std::cout << " -> " << xs.size() << std::endl;
+    } while (1);
+
+    for (auto& b : bs)
+        xs.erase(b);
+
+    std::cout << "Part 1: " << xs.size() << std::endl;
+
+    return 0;
+}
+
diff --git a/day15/part2.cpp b/day15/part2.cpp
new file mode 100644
index 0000000..6a80e54
--- /dev/null
+++ b/day15/part2.cpp
@@ -0,0 +1,60 @@
+#include <cstdio>
+#include <iostream>
+#include <string>
+#include <vector>
+
+struct Sensor
+{
+    long x, y, power;
+
+    Sensor(long xx, long yy, long pp):
+        x(xx), y(yy), power(pp) {}
+};
+
+int main()
+{
+    std::vector<Sensor> sensors;
+    long px, py, bx, by;
+
+    do {
+        std::string line;
+        std::getline(std::cin, line);
+        if (std::cin.eof())
+            break;
+
+        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;
+                }
+            }
+
+            if (!h) {
+                std::cout << std::endl << x << ", " << y << std::endl;
+                return 0;
+            }
+        }
+    }
+
+    return 0;
+}
+
-- 
cgit v1.2.3