aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day6/aoc.h
blob: 328f38292f563374c15fd12ae2d07f0f69048a76 (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
#pragma once
#include "common.h"

namespace aoc2017 {

struct memory_bank {
  int banks[16];
  int index = 0;

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

  memory_bank(line_view lv) {
    const char* p = lv.line;
    int i{0};
    while (p < lv.line + lv.length) {
      if (*p >= '0' && *p <= '9') {
        get_number(&p, &banks[i++]);
      }
      p++;
    }
  }

  bool operator<(const memory_bank& m) const noexcept {
    for (size_t i = 0; i < ARRAY_SIZE(banks); i++) {
      if (banks[i] < m.banks[i]) {
        return true;
      }
      if (banks[i] > m.banks[i]) {
        return false;
      }
    }
    return false;
  }

  int highest(int* d) const noexcept {
    int max{INT32_MIN};
    int index{0};
    for (size_t i = 0; i < ARRAY_SIZE(banks); i++) {
      if (banks[i] > max) {
        max = banks[i];
        index = i;
      }
    }
    *d = max;
    return index;
  }

  void distribute(size_t i, int x) {
    banks[i] = 0;
    while (x > 0) {
      i = (i == ARRAY_SIZE(banks) - 1) ? 0 : i + 1;
      banks[i] += 1;
      x -= 1;
    }
    index += 1;
  }
};

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

} // namespace aoc2017