aboutsummaryrefslogtreecommitdiff
path: root/src/2018/day3/aoc.h
blob: 42b3c9026e18520dcaf90e0b6673c16b5342759b (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
73
74
75
76
#pragma once
#include "common.h"
#include <vector>

namespace aoc2018 {

struct claim {
  int id;
  int x;
  int y;
  int width;
  int height;
};

struct fabric {
  // 0 ... 999
  uint16_t claims[1000 * 1000] = {0};

  void apply(const claim& c) {
    for (int x = c.x; x < c.x + c.width; x++) {
      for (int y = c.y; y < c.y + c.height; y++) {
        claims[y * 1000 + x] += 1;
      }
    }
  }

  int count(int x) {
    int total{0};
    for (auto c : claims) {
      if (c > x) {
        total += 1;
      }
    }
    return total;
  }

  bool overlap(const claim& c) {
    for (int x = c.x; x < c.x + c.width; x++) {
      for (int y = c.y; y < c.y + c.height; y++) {
        if (claims[y * 1000 + x] > 1) {
          return true;
        }
      }
    }
    return false;
  }

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

  void parse(line_view lv, std::vector<claim>& cs) {
    claim c;
    int* is[] = {&c.id, &c.x, &c.y, &c.width, &c.height};
    int i{0};
    const char* p = lv.line;
    while (p < lv.line + lv.length) {
      if (*p >= '0' && *p <= '9') {
        get_number(&p, is, i++);
      }
      p++;
    }
    apply(c);
    cs.push_back(c);
  }
};

std::pair<int, int> day3(line_view, int);

} // namespace aoc2018