diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-15 21:50:27 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-15 21:50:27 +0800 |
commit | 92c436b35c581bd5deaba63671faaed88a9e1a6e (patch) | |
tree | d2ee05507d19e023540db788c098eb3e7a7affff /src | |
parent | 5221877e962e70625921c2084c8a42996050aa94 (diff) | |
download | advent-of-code-92c436b35c581bd5deaba63671faaed88a9e1a6e.tar.gz advent-of-code-92c436b35c581bd5deaba63671faaed88a9e1a6e.zip |
check loops
Diffstat (limited to 'src')
-rw-r--r-- | src/2018/day6/aoc.cpp | 30 | ||||
-rw-r--r-- | src/2018/day6/aoc.h | 98 |
2 files changed, 127 insertions, 1 deletions
diff --git a/src/2018/day6/aoc.cpp b/src/2018/day6/aoc.cpp index 585f144..5350b0e 100644 --- a/src/2018/day6/aoc.cpp +++ b/src/2018/day6/aoc.cpp @@ -1,5 +1,35 @@ #include "aoc.h" +#include <algorithm> namespace aoc2018 { +int day6(line_view file) { + std::vector<coordinate> cs; + int maxx{INT32_MIN}; + int maxy{INT32_MIN}; + + per_line(file, [&cs, &maxx, &maxy](line_view lv) { + coordinate c(lv); + cs.push_back(c); + maxx = std::max(maxx, c.x); + maxy = std::max(maxx, c.y); + return true; + }); + + std::sort(cs.begin(), cs.end()); + space_board b{maxx + 1, maxy + 1}; + + 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}; + c.traverse([&total, &b, &c](int lap, int dir, coordinate x) { return true; }, + [&total]() { return total[total.size() - 1] == 0; }); + } + return 0; } + +} // namespace aoc2018 diff --git a/src/2018/day6/aoc.h b/src/2018/day6/aoc.h index 4bfdbf3..5d27936 100644 --- a/src/2018/day6/aoc.h +++ b/src/2018/day6/aoc.h @@ -1,6 +1,102 @@ #pragma once #include "common.h" +#include <math.h> +#include <utility> +#include <vector> namespace aoc2018 { -} +struct coordinate { + int x = 0; + int y = 0; + + coordinate(int xx, int yy) : x(xx), y(yy) {} + coordinate(line_view lv) { + const char* p = lv.line; + int* d[] = {&x, &y}; + int i{0}; + while (p < lv.line + lv.length) { + if (*p >= '0' && *p <= '9') { + get_number(&p, d[i++]); + } + p++; + } + } + + void get_number(const char** pp, int* d) { + const char* p = *pp; + *d = 0; + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *pp = p; + } + + int distance(coordinate c = coordinate{0, 0}) const noexcept { return std::abs(x - c.x) + std::abs(y - c.y); } + coordinate right(int d) { return {x + d, y}; } + coordinate up(int d) { return {x, y - d}; } + coordinate left(int d) { return {x - d, y}; } + coordinate down(int d) { return {x, y + d}; } + typedef coordinate (coordinate::*move)(int); + + bool operator<(const coordinate& c) const noexcept { + int d1 = distance(); + int d2 = c.distance(); + return d1 < d2 ? true : (d1 > d2 ? false : (x < c.x ? true : (x > c.x ? false : y < c.y))); + } + + template <typename F, typename G, typename... Args> + void traverse(F&& f, G&& g, Args&&... args) const noexcept { + // right, up, left, down + auto next = [](int i, coordinate c) -> coordinate { + move fs[] = {&coordinate::right, &coordinate::up, &coordinate::left, &coordinate::down}; + return (c.*(fs[i]))(1); + }; + int steps[] = {1, 1, 2, 2}; + int 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)...); + } + steps[s] += 2; + i++; + stop = stop || ((i % 4 == 0) && g()); + } + } +}; + +struct space_board { + // -1 no one + // -2 same distance + struct point { + int id; + int distance; + }; + + int rows; + int cols; + std::vector<point> ps; + space_board(int x, int y) : rows(x), cols(y), ps(x * y, {-1, INT32_MAX}){}; + + void count(std::vector<int>& area) { + for (int y = 0; y < cols; y++) { + for (int x = 0; x < rows; x++) { + auto p = ps[y * rows + x]; + if (x == 0 || y == 0 || x == rows - 1 || y == cols - 1) { + area[p.id] = INT32_MAX; + } else { + area[p.id] += 1; + } + } + } + } +}; + +int day6(line_view); + +} // namespace aoc2018 |