aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day8/aoc.cpp
blob: b4a9004dc2181fb1795c7ee4dd12cd0aea0c65bb (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
77
78
79
80
81
82
83
84
85
86
87
88
#include "aoc.h"
#include <unordered_map>

namespace aoc2017 {

int largest(const std::unordered_map<line_view, int>& rs) {
  int max{INT32_MIN};
  for (auto& kv : rs) {
    if (kv.second > max) {
      max = kv.second;
    }
  }
  return max;
}

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

line_view get_label(const char** pp) {
  const char* p = *pp;
  const char* p1 = p;
  while (*p != ' ') {
    p++;
  }
  *pp = p;
  return {p1, p};
}

static int inc(int x, int i) { return x + i; }
static int dec(int x, int i) { return x - i; }
static bool condition_met(line_view cond, int x, int i) {
  if (*cond.line == '=') {
    return x == i;
  }
  if (*cond.line == '!') {
    return x != i;
  }
  bool bs[] = {x > i, x < i, x >= i, x <= i};
  return bs[size_t(*cond.line == '<') + (cond.length & 2)];
}

typedef int (*op)(int, int);
std::pair<int, int> day8(line_view file) {
  std::unordered_map<line_view, int> registers;
  int max{INT32_MIN};
  per_line(file, [&registers, &max](line_view lv) {
    const char* p = lv.line;
    line_view v1 = get_label(&p);
    op f = p[1] == 'd' ? dec : inc;

    p += 5;
    int d1{0};
    get_number(&p, &d1);

    p += 4;
    line_view v2 = get_label(&p);

    p += 1;
    line_view condition = get_label(&p);

    p += 1;
    int d2{0};
    get_number(&p, &d2);
    if (condition_met(condition, registers[v2], d2)) {
      registers[v1] = f(registers[v1], d1);
      if (registers[v1] > max) {
        max = registers[v1];
      }
    }
    return true;
  });
  return {largest(registers), max};
}

} // namespace aoc2017