From c1e78f33601a8cfb206eb131880b3726ffde8f62 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sat, 12 Nov 2022 08:03:14 -0800 Subject: [PATCH] faster 10 --- 2019/10.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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; } }