aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day20/aoc.h
blob: de904f86e676702c263e313bd2c176d12708167a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "common.h"
#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