diff --git a/2019/10.cpp b/2019/10.cpp index ff3bcfc..5aec55a 100644 --- a/2019/10.cpp +++ b/2019/10.cpp @@ -52,21 +52,34 @@ auto angle(Coord c) -> Rational { 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> 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; } }