diff options
Diffstat (limited to 'src/2017/day20/aoc.h')
-rw-r--r-- | src/2017/day20/aoc.h | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/2017/day20/aoc.h b/src/2017/day20/aoc.h index 973f018..de904f8 100644 --- a/src/2017/day20/aoc.h +++ b/src/2017/day20/aoc.h @@ -3,5 +3,70 @@ #include <vector> namespace aoc2017 { + +struct particle { + int64_t p[3] = {0}; + int64_t v[3] = {0}; + int64_t a[3] = {0}; + + int id; + + int64_t mdistance() const noexcept { return std::abs(p[0]) + std::abs(p[1]) + std::abs(p[2]); } + friend bool operator<(particle p1, particle p2) { return p1.mdistance() < p2.mdistance(); } + friend bool operator==(particle p1, particle p2) { + return p1.p[0] == p2.p[0] && p1.p[1] == p2.p[1] && p1.p[2] == p2.p[2]; + } + + // Increase the X velocity by the X acceleration. + // Increase the Y velocity by the Y acceleration. + // Increase the Z velocity by the Z acceleration. + // Increase the X position by the X velocity. + // Increase the Y position by the Y velocity. + // Increase the Z position by the Z velocity. + void tick() { + for (int i = 0; i < 3; i++) { + v[i] += a[i]; + p[i] += v[i]; + } + } + + void get_number(const char** pp, int64_t* d) { + const char* p = *pp; + int sign = 1; + if (*p == '-') { + sign = -1; + p += 1; + } + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + + *d *= sign; + *pp = p; + } + + void print() const noexcept { + const int64_t* d[] = {p, v, a}; + printf("%d: ", id); + for (int i = 0; i < 3; i++) { + printf("%ld %ld %ld ", *d[i], *(d[i] + 1), *(d[i] + 2)); + } + printf("\n"); + } + + particle(int x, line_view lv) : id(x) { + int64_t* ds[9] = {p, p + 1, p + 2, v, v + 1, v + 2, a, a + 1, a + 2}; + int i{0}; + const char* p = lv.line; + while (p < lv.line + lv.length) { + if (*p == '-' || (*p >= '0' && *p <= '9')) { + get_number(&p, ds[i++]); + } + p++; + } + } +}; + std::pair<int64_t, int64_t> day20(line_view); -} +} // namespace aoc2017 |