aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-15 19:50:01 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-15 19:50:01 -0500
commitc6c170bde521817331bcb7a17a24eb32d586aeef (patch)
treec5d27312665620e8c4ea3d3c5a89503073a6e544
parent9044e949dc17fd23e2535a00246f25ea71817ade (diff)
day15: part 1 speedup
-rw-r--r--day15/part1.cpp33
1 files changed, 12 insertions, 21 deletions
diff --git a/day15/part1.cpp b/day15/part1.cpp
index fb53753..6c512f7 100644
--- a/day15/part1.cpp
+++ b/day15/part1.cpp
@@ -1,3 +1,4 @@
+#include <bitset>
#include <cstdio>
#include <iostream>
#include <set>
@@ -7,8 +8,7 @@ constexpr long Y = 2000000;
int main()
{
- std::set<int> xs;
- std::set<int> bs;
+ std::bitset<8000000> xs;
long px, py, bx, by;
do {
@@ -17,33 +17,24 @@ int main()
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);
+ 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;
+ long dist = std::abs(px - bx) + std::abs(py - by);
- if (by == Y)
- bs.insert(bx);
-
- long ic = std::abs(px - bx) + std::abs(py - by);
-
- if (Y < py - ic || Y > py + ic)
+ if (Y < py - dist || Y > py + dist)
continue;
- long spr = (ic - std::abs(py - Y));
+ px += 1000000;
+ long spr = dist - std::abs(py - Y);
for (long j = 0; j <= spr; ++j) {
- xs.insert(px + j);
- xs.insert(px - j);
+ xs.set(px + j);
+ xs.set(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;
+ std::cout << "Part 1: " << xs.count() - 1 << std::endl;
return 0;
}