diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-16 11:40:13 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-16 11:40:13 +0800 |
commit | 8833538921c9e167d1c127b8905740ebaa4323ee (patch) | |
tree | 904536fe204e2ffc65861e122167004dfd75a2a8 /src/2018/day6/aoc.cpp | |
parent | 92c436b35c581bd5deaba63671faaed88a9e1a6e (diff) | |
download | advent-of-code-8833538921c9e167d1c127b8905740ebaa4323ee.tar.gz advent-of-code-8833538921c9e167d1c127b8905740ebaa4323ee.zip |
traverse lap
Diffstat (limited to 'src/2018/day6/aoc.cpp')
-rw-r--r-- | src/2018/day6/aoc.cpp | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/src/2018/day6/aoc.cpp b/src/2018/day6/aoc.cpp index 5350b0e..07f8ad9 100644 --- a/src/2018/day6/aoc.cpp +++ b/src/2018/day6/aoc.cpp @@ -26,10 +26,40 @@ int day6(line_view file) { p.distance = 0; std::vector<int> total = {0}; - c.traverse([&total, &b, &c](int lap, int dir, coordinate x) { return true; }, - [&total]() { return total[total.size() - 1] == 0; }); + + auto f = [&total, &b, &c, &i](size_t lap, coordinate x) { + if (x.x >= 0 && x.x <= b.cols && x.y >= 0 && x.y <= b.rows) { + if (lap == total.size()) { + total.push_back(0); + } + auto& p = b.ps[x.y * b.rows + x.x]; + int d = x.distance(c); + if (d < p.distance) { + p.id = i; + p.distance = d; + total[lap] += 1; + } + if (d == p.distance) { + p.id = -2; // same distance + total[lap] += 1; + } + } + return true; + }; + auto g = [&total]() { return total[total.size() - 1] == 0; }; + c.traverse(f, g); + } + + std::vector<int> xs(cs.size(), 0); + b.count(xs); + + int max{INT32_MIN}; + for (auto& x : xs) { + if (x != INT32_MAX && x > max) { + max = x; + } } - return 0; + return max; } } // namespace aoc2018 |