faster 10

This commit is contained in:
Eric Mertens 2022-11-12 08:03:14 -08:00
parent 2955e6f3d9
commit c1e78f3360

View File

@ -52,21 +52,34 @@ auto angle(Coord c) -> Rational<std::int64_t> {
mk(4, c.y, c.x);
}
auto ClearView(Grid const& grid, Coord src, Coord dst) -> bool {
auto vec = dst - src;
auto steps = std::gcd(vec.x, vec.y);
Coord delta {vec.x / steps, vec.y / steps};
for (auto cursor = src+delta; cursor != dst; cursor+=delta) {
if (grid[cursor] == '#') {
return false;
}
}
return true;
}
auto Part1(Grid const& grid) {
std::size_t best = 0;
Coord base {};
grid.each([&](Coord src, char s) {
if ('#' == s) {
std::set<Rational<std::int64_t>> angles;
std::size_t visible = 0;
grid.each([&](Coord dst, char d){
if ('#' == d && src != dst) {
auto delta = dst - src;
angles.insert(angle(dst - src));
if (ClearView(grid, src, dst)) {
visible++;
}
}
});
auto n = angles.size();
if (n > best) {
best = n;
if (visible > best) {
best = visible;
base = src;
}
}