aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day18/aoc.h
blob: 1d87b3ccd6a47e2c65c0c31aca37ca39577c873c (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
#include "common.h"
#include <vector>

namespace aoc2022 {

struct cube {
  int dx;
  int dy;
  int dz;

  char* ds = nullptr;

  char& get(int x, int y, int z) {
    static char none{0};
    bool bx = x >= 0 && x < dx;
    bool by = y >= 0 && y < dy;
    bool bz = z >= 0 && z < dz;
    return (bx && by && bz) ? *(ds + z * (dx * dy) + y * dx + x) : none;
  }

  cube(int x, int y, int z) : dx(x), dy(y), dz(z) {
    ds = (char*)malloc(x * y * z);
    memset(ds, 0, x * y * z);
  }

  template <typename F, typename... Args>
  void traverse(F&& f, Args&&... args) {
    for (int z = 0; z < dz; z++) {
      for (int y = 0; y < dy; y++) {
        for (int x = 0; x < dx; x++) {
          f(x, y, z, std::forward<Args>(args)...);
        }
      }
    }
  }
};

struct droplet {
  int x = 0;
  int y = 0;
  int z = 0;

  void get_number(const char** pp, int* d) {
    const char* p = *pp;
    while (*p >= '0' && *p <= '9') {
      *d = *d * 10 + *p - '0';
      p++;
    }
    *pp = p;
  }

  void print() const noexcept { printf("%d,%d,%d\n", x, y, z); }

  friend bool operator<(droplet d1, droplet d2) {
    return d1.z < d2.z ? true : d1.z > d2.z ? false : d1.y < d2.y ? true : d1.y > d2.y ? false : d1.x < d2.x;
  }

  droplet(int xx, int yy, int zz) : x(xx), y(yy), z(zz) {}
  droplet(line_view lv) {
    const char* p = lv.line;
    int* ds[] = {&x, &y, &z};
    int i{0};
    while (p < lv.line + lv.length) {
      get_number(&p, ds[i++]);
      p++;
    }
  }
};

std::pair<int, int> day18(line_view);
} // namespace aoc2022