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); 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) { auto Part1(Grid const& grid) {
std::size_t best = 0; std::size_t best = 0;
Coord base {}; Coord base {};
grid.each([&](Coord src, char s) { grid.each([&](Coord src, char s) {
if ('#' == s) { if ('#' == s) {
std::set<Rational<std::int64_t>> angles; std::size_t visible = 0;
grid.each([&](Coord dst, char d){ grid.each([&](Coord dst, char d){
if ('#' == d && src != dst) { if ('#' == d && src != dst) {
auto delta = dst - src; if (ClearView(grid, src, dst)) {
angles.insert(angle(dst - src)); visible++;
}
} }
}); });
auto n = angles.size();
if (n > best) { if (visible > best) {
best = n; best = visible;
base = src; base = src;
} }
} }