day15: part 1 speedup

master
Clyne 2 years ago
parent 9044e949dc
commit c6c170bde5

@ -1,3 +1,4 @@
#include <bitset>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <set> #include <set>
@ -7,8 +8,7 @@ constexpr long Y = 2000000;
int main() int main()
{ {
std::set<int> xs; std::bitset<8000000> xs;
std::set<int> bs;
long px, py, bx, by; long px, py, bx, by;
do { do {
@ -17,33 +17,24 @@ int main()
if (std::cin.eof()) if (std::cin.eof())
break; break;
auto a = sscanf(line.c_str(), sscanf(line.c_str(),
"Sensor at x=%ld, y=%ld: closest beacon is at x=%ld, y=%ld", "Sensor at x=%ld, y=%ld: closest beacon is at x=%ld, y=%ld",
&px, &py, &bx, &by); &px, &py, &bx, &by);
std::cout << px << ' ' << py << ' ' << bx << ' ' << by; long dist = std::abs(px - bx) + std::abs(py - by);
if (by == Y) if (Y < py - dist || Y > py + dist)
bs.insert(bx);
long ic = std::abs(px - bx) + std::abs(py - by);
if (Y < py - ic || Y > py + ic)
continue; continue;
long spr = (ic - std::abs(py - Y)); px += 1000000;
long spr = dist - std::abs(py - Y);
for (long j = 0; j <= spr; ++j) { for (long j = 0; j <= spr; ++j) {
xs.insert(px + j); xs.set(px + j);
xs.insert(px - j); xs.set(px - j);
} }
std::cout << " -> " << xs.size() << std::endl;
} while (1); } while (1);
for (auto& b : bs) std::cout << "Part 1: " << xs.count() - 1 << std::endl;
xs.erase(b);
std::cout << "Part 1: " << xs.size() << std::endl;
return 0; return 0;
} }

Loading…
Cancel
Save