aboutsummaryrefslogtreecommitdiffstats
path: root/day15/part1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'day15/part1.cpp')
-rw-r--r--day15/part1.cpp50
1 files changed, 50 insertions, 0 deletions
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;
+}
+