aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day9/aoc.cpp
blob: 4f741d5fceabd675a93847753726eaa813ee1d78 (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
#include "aoc.h"

namespace aoc2016 {

static 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;
}

static void check(const char** pp, int* t) {
  int d[2] = {0};
  const char* p = *pp;
  int i = 0;
  while (*p != ')') {
    if (*p >= '0' && *p <= '9') {
      get_number(&p, d + i++);
    }
    if (*p == '(' || *p == 'x') {
      p++;
    }
  }
  p += d[0];
  *t += d[1] * d[0];
  *pp = p;
}

int day9(line_view file) {
  int t0{0};

  const char* p1 = file.line;
  const char* p2 = p1 + file.length - 1;
  const char* p = p1;

  while (p < p2) {
    if (*p == '(') {
      check(&p, &t0);
      p++;
    } else {
      if (*p != ' ') {
        t0++;
      }
      p++;
    }
  }
  return t0;
}

} // namespace aoc2016