aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-16 11:40:13 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-16 11:40:13 +0800
commit8833538921c9e167d1c127b8905740ebaa4323ee (patch)
tree904536fe204e2ffc65861e122167004dfd75a2a8 /src
parent92c436b35c581bd5deaba63671faaed88a9e1a6e (diff)
downloadadvent-of-code-8833538921c9e167d1c127b8905740ebaa4323ee.tar.gz
advent-of-code-8833538921c9e167d1c127b8905740ebaa4323ee.zip
traverse lap
Diffstat (limited to 'src')
-rw-r--r--src/2018/day6/aoc.cpp36
-rw-r--r--src/2018/day6/aoc.h9
2 files changed, 38 insertions, 7 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
diff --git a/src/2018/day6/aoc.h b/src/2018/day6/aoc.h
index 5d27936..9bb86d7 100644
--- a/src/2018/day6/aoc.h
+++ b/src/2018/day6/aoc.h
@@ -54,14 +54,14 @@ struct coordinate {
return (c.*(fs[i]))(1);
};
int steps[] = {1, 1, 2, 2};
- int i{0};
+ size_t i{0};
coordinate c = *this;
bool stop{false};
while (!stop) {
int s = i % 4;
for (int x = 0; x < steps[s] && !stop; x++) {
c = next(s, c);
- stop = !f(i / 4, s, c, std::forward<Args>(args)...);
+ stop = !f(i / 4, c, std::forward<Args>(args)...);
}
steps[s] += 2;
i++;
@@ -81,7 +81,7 @@ struct space_board {
int rows;
int cols;
std::vector<point> ps;
- space_board(int x, int y) : rows(x), cols(y), ps(x * y, {-1, INT32_MAX}){};
+ space_board(int x, int y) : rows(y), cols(x), ps(x * y, {-1, INT32_MAX}){};
void count(std::vector<int>& area) {
for (int y = 0; y < cols; y++) {
@@ -89,7 +89,8 @@ struct space_board {
auto p = ps[y * rows + x];
if (x == 0 || y == 0 || x == rows - 1 || y == cols - 1) {
area[p.id] = INT32_MAX;
- } else {
+ }
+ if (area[p.id] < INT32_MAX) {
area[p.id] += 1;
}
}