aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day22/aoc.h
blob: ac5d83fb24a692f05a514a5fcba3d105d47a7118 (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
#pragma once
#include "common.h"
#include <vector>

namespace aoc2016 {

struct grid_node {
  int x = 0;
  int y = 0;
  int s[4] = {0}; // size, used, avail, use%

  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;
  }

  grid_node() {}
  grid_node(line_view lv) {
    int* ds[] = {&x, &y, s, s + 1, s + 2, s + 3};
    const char* p = lv.line;
    int i{0};
    while (p < lv.line + lv.length) {
      if (*p >= '0' && *p <= '9') {
        get_number(&p, ds[i]);
        i++;
      }
      p++;
    }
  }

  void print() const noexcept { printf("(%d,%d) %d %d %d %d\n", x, y, s[0], s[1], s[2], s[3]); }
  friend bool operator<(const grid_node& n1, const grid_node& n2) {
    return n1.s[2] > n2.s[2] ? true : n1.s[2] < n2.s[2] ? false : n1.s[0] > n2.s[0];
  }
};

struct storage_grid {
  std::vector<grid_node> ns;
  int width;
  int height;

  storage_grid(int w, int h) : width(w), height(h) { ns.resize(width * height); }
  grid_node& get(int x, int y) { return ns[y * width + x]; }

  void load(const std::vector<grid_node>& vs) {
    for (auto& n : vs) {
      auto& node = get(n.x, n.y);
      memcpy(node.s, n.s, sizeof(int) * 4);
    }
  }

  void print() {
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        auto& n = get(x, y);
        printf("(%02d/%02d)\t", n.s[1], n.s[0]);
      }
      printf("\n");
    }
  }
};

std::pair<int64_t, int64_t> day22(line_view);
} // namespace aoc2016