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


namespace aoc2022 {

struct apair {
  int first[2] = {0};
  int second[2] = {0};

  void get_number(int* d, int i, const char* p) {
    *(d + i) = *(d + i) * 10 + *p - '0';
  }

  void load(line_view lv) {
    int* d = first;
    int i{0};
    const char* p0 = lv.line;
    while (p0 < lv.line + lv.length) {
      if (*p0 >= '0' && *p0 <= '9') {
        get_number(d, i, p0);
      }
      else {
        if (*p0 == ',') {
          d = second;
          i = 0;
        }
        if (*p0 == '-') {
          i += 1;
        }
      }
      p0++;
    }
  }

  bool covered() const noexcept {
    bool b1 = first[0] <= second[0] && first[1]  >= second[1];
    bool b2 = second[0] <= first[0] && second[1]  >= first[1];
    return b1 || b2;
  }

  bool overlap() const noexcept {
    bool b1 = second[0] >= first[0] && second[0] <= first[1];
    bool b2 = second[1] >= first[0] && second[1] <= first[1];
    bool b3 = first[0] >= second[0] && first[0] <= second[1];
    bool b4 = first[1] >= second[0] && first[1] <= second[1];
    return covered() || b1 || b2 || b3 || b4;
  }
};

std::pair<int, int> day4(line_view file);

}