aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day8/aoc.cpp
blob: 573e7c19e27122db1df723600d9e805d9a962241 (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
#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 get_number(const char* p1, const char* p2, int* ds) {
  int index{0};
  while (p1 < p2) {
    if (*p1 >= '0' && *p1 <= '9') {
      get_number(&p1, &ds[index++]);
    }
    p1++;
  }
}

int day8(line_view file) {
  grid<50, 6> g;
  per_line(file, [&g](line_view lv) {
    const char* p = lv.line;
    int ds[2] = {0};
    get_number(p, p + lv.length, ds);
    if (p[1] == 'e') {
      g.set(ds[0], ds[1]);
    }
    if (p[1] == 'o' && p[7] == 'c') {
      g.rotate_v(ds[0], ds[1]);
    }
    if (p[1] == 'o' && p[7] == 'r') {
      g.rotate_h(ds[0], ds[1]);
    }
    // g.print();
    return true;
  });
  return g.count();
}

} // namespace aoc2016