diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-16 15:10:12 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-16 15:10:12 +0800 |
commit | 04e50217ffb0288e501c1f150a13b6ca70f1367a (patch) | |
tree | 69bb16e6ff8d72c869a21f71f6be532dd92a74a6 /src/2018/day6/aoc.cpp | |
parent | 8833538921c9e167d1c127b8905740ebaa4323ee (diff) | |
download | advent-of-code-04e50217ffb0288e501c1f150a13b6ca70f1367a.tar.gz advent-of-code-04e50217ffb0288e501c1f150a13b6ca70f1367a.zip |
check loops
Diffstat (limited to 'src/2018/day6/aoc.cpp')
-rw-r--r-- | src/2018/day6/aoc.cpp | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/2018/day6/aoc.cpp b/src/2018/day6/aoc.cpp index 07f8ad9..745a185 100644 --- a/src/2018/day6/aoc.cpp +++ b/src/2018/day6/aoc.cpp @@ -12,47 +12,59 @@ int day6(line_view file) { coordinate c(lv); cs.push_back(c); maxx = std::max(maxx, c.x); - maxy = std::max(maxx, c.y); + maxy = std::max(maxy, c.y); return true; }); std::sort(cs.begin(), cs.end()); space_board b{maxx + 1, maxy + 1}; + printf("%d\n", b.size()); for (size_t i = 0; i < cs.size(); i++) { coordinate& c = cs[i]; + auto& p = b.ps[c.y * b.rows + c.x]; p.id = i; p.distance = 0; - std::vector<int> total = {0}; + std::vector<int> total(1, 0); + int win{1}; - 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 f = [&win, &total, &b, &c, &i](size_t lap, coordinate x) { + if (lap == total.size()) { + // printf("%zu, %d %d\n", lap - 1, total[lap - 1], win); + total.push_back(0); + } + if (x.x >= 0 && x.x < b.cols && x.y >= 0 && x.y < b.rows) { auto& p = b.ps[x.y * b.rows + x.x]; int d = x.distance(c); + if (d == p.distance) { + p.id = -2; // same distance + total[lap] += 1; + } 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; + win += 1; } } return true; }; - auto g = [&total]() { return total[total.size() - 1] == 0; }; + auto g = [&total]() { + int last = total.size() - 1; + return total[last] == 0; + }; c.traverse(f, g); + printf("%zu %d\n", i, win); } std::vector<int> xs(cs.size(), 0); b.count(xs); + std::for_each(xs.begin(), xs.end(), [](int d) { printf("%d ", d); }); + printf("\n"); + int max{INT32_MIN}; for (auto& x : xs) { if (x != INT32_MAX && x > max) { |