#include "common.h" #include 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 line { pos p0; pos p1; line() {} line(pos x, pos y) : p0(x), p1(y) {} friend bool operator<(const line& l1, const line& l2) { return l1.p0.x < l2.p0.x; } }; 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); } std::pair> lines() const noexcept { auto d = mdistance(ps[0], ps[1]); std::vector ls; for (int y = ps[0].y - d; y <= ps[0].y + d; y++) { auto dx = d - std::abs(ps[0].y - y); pos p0 = {ps[0].x - dx, y}; pos p1 = {ps[0].x + dx, y}; ls.emplace_back(p0, p1); } return {ps[0].y - d, ls}; } 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); } 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++; } } bool operator<(const sensor& s) const noexcept { return ps[0].x < s.ps[0].x; } }; std::pair day15(line_view); } // namespace aoc2022