aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day22/aoc.h
blob: d368f224434c84aadb22d413c07b44e3b8f4f73c (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
#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(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];
  }
};

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