From b6e660ac61a927e517b355cd8d20ddcabc3c9b75 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 15 Dec 2022 08:15:23 -0500 Subject: day15: part 1 c++, partial part 2 --- day15/part1.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 day15/part1.cpp (limited to 'day15/part1.cpp') 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 +#include +#include +#include + +constexpr long Y = 2000000; + +int main() +{ + std::set xs; + std::set 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; +} + -- cgit v1.2.3