aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day8/aoc.cpp
blob: be789d25d0ccfde58b6a6e67d95b85cab4698a49 (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
#include "aoc.h"

namespace aoc2015 {
// a -> 1
// \\ -> 1
// \" -> 1
// \xXX -> 1
std::pair<int, int> count_day8(line_view lv) {
  int t1 = 0;
  int t2 = 0;
  const char* p1 = lv.line;
  const char* p2 = lv.line + lv.length;
  const char* p = p1;
  while (p != p2) {
    if (*p != '\\' || p == p2 - 1) {
      t1 += 1;
      t2 += 1;
      p++;
      continue;
    } else {
      if (*(p + 1) == '\\' || *(p + 1) == '"') {
        t1 += 1;
        t2 += 4;
        p += 2;
        continue;
      }
      if (*(p + 1) == 'x') {
        t1 += 1;
        t2 += 5;
        p += 4;
        continue;
      }
      t1 += 1;
      t2 += 1;
      p++;
    }
  }
  return {t1, t2};
}

std::pair<int, int> day8(line_view file) {
  int t1 = 0;
  int t2 = 0;
  int t3 = 0;
  per_line(file, [&t1, &t2, &t3](line_view lv) {
    t1 += lv.length - 1;
    auto p = count_day8({lv.line + 1, lv.line + lv.length - 2});
    t2 += p.first;
    t3 += p.second + 6;
    return true;
  });
  return {t1 - t2, t3 - t1};
}

} // namespace aoc2015