diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-12-15 18:09:56 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-12-15 18:09:56 +0800 |
commit | d96f65b6b51b78ddd9f51896ea99c8010ed77812 (patch) | |
tree | cd245be03f67a4c6b139745ab7e02a6006100634 /src/2022/day15/aoc.h | |
parent | 363e289eb2b54757e52dc93efa3090c763a8aa39 (diff) | |
download | advent-of-code-d96f65b6b51b78ddd9f51896ea99c8010ed77812.tar.gz advent-of-code-d96f65b6b51b78ddd9f51896ea99c8010ed77812.zip |
2022 day15 part1
Diffstat (limited to 'src/2022/day15/aoc.h')
-rw-r--r-- | src/2022/day15/aoc.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/2022/day15/aoc.h b/src/2022/day15/aoc.h index eac13eb..ffb4183 100644 --- a/src/2022/day15/aoc.h +++ b/src/2022/day15/aoc.h @@ -3,6 +3,80 @@ namespace aoc2022 { +struct sensor { + struct pos { + int x = 0; + int y = 0; + + pos() {} + pos(int i, int j): x(i), y(j) {} + bool operator==(pos px) const noexcept { + return x == px.x && y == px.y; + } + + bool operator<(pos px) const noexcept { + return x < px.x ? true : x > px.x ? false : y < px.y; + } + }; + + struct triangle { + pos as[3]; + }; + + static int minx; + static int maxx; + + pos ps[2] = {{0,0},{0,0}}; + + static int mdistance(pos p1, pos p2) { + return std::abs(p1.x - p2.x) + std::abs(p1.y - p2.y); + } + + void get_number(const char** pp, int* d) { + const char *p = *pp; + int sign = 1; + if (*p == '-') { + sign = -1; + p++; + } + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *d *= sign; + *pp = p; + } + + void print() { + printf("S(%d, %d) B(%d, %d)\n", ps[0].x, ps[0].y, ps[1].x, ps[1].y); + } + + bool inscope(pos p) { + auto d1 = mdistance(ps[0], ps[1]); + auto d2 = mdistance(ps[0], p); + return d2 <= d1 && !(p == ps[1]); + } + + sensor(line_view lv) { + const char* p = lv.line; + int *is[] = {&ps[0].x, &ps[0].y, &ps[1].x, &ps[1].y}; + int n{0}; + while (p < lv.line + lv.length) { + if (*p == '=') { + const char* p0 = p+1; + get_number(&p0, is[n]); + p = p0; + n++; + } + p++; + } + auto x1 = ps[0].x - mdistance(ps[0], ps[1]); + auto x2 = ps[0].x + mdistance(ps[0], ps[1]); + if (minx > x1) minx = x1; + if (maxx < x2) maxx = x2; + } +}; + std::pair<int, int> day15(line_view); } |