aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day12/aoc.cpp
blob: 33db5d0a15022e2a05cec8fc3fe6e83e98427ed8 (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
89
90
91
92
#include "aoc.h"
#include <stack>

namespace aoc2015 {

int get_number(const char* p1, const char* p2) {
  int sign = *(p1 - 1) == '-' ? -1 : 1;
  int d{0};
  while (p1 != p2) {
    d = d * 10 + *p1 - '0';
    p1++;
  }
  return sign * d;
}

int parse_day12(line_view s) {
  int total = 0;
  const char* p1 = s.line;
  const char* p2 = p1;
  bool to_parse = false;
  while (p2 < s.line + s.length) {
    if (*p2 >= '0' && *p2 <= '9') {
      if (!to_parse) {
        p1 = p2;
        to_parse = !to_parse;
      }
    } else {
      if (to_parse) {
        total += get_number(p1, p2);
        to_parse = !to_parse;
      }
    }
    p2++;
  }
  if (to_parse) {
    total += get_number(p1, p2);
  }
  return total;
}

int day12(line_view file) { return parse_day12(file); }

struct day12_object {
  const char* p1;

  int fold(const char* p2) {
    line_view lv{p1, p2};
    const char* p = lv.contains("red");
    return (p != nullptr) ? 0 : parse_day12(lv);
  }
};

// {{x}   x} = 0
// {{x} ...} = ...
// {{...} x} = 0
// {{...} .} = ... + .
int day12_part2(line_view file) {
  int total = 0;
  std::stack<day12_object> os;
  std::stack<int> is;
  const char* p = file.line;
  const char* p1 = p;
  while (p < file.line + file.length) {
    if (*p == '{') {
      if (os.empty()) {
        total += parse_day12({p1, p});
        p1 = p;
      }
      os.push({p});
    }
    if (*p == '}') {
      auto o = os.top();
      is.push(o.fold(p));
      os.pop();
      if (os.empty()) {
        while (!is.empty()) {
          total += is.top();
          is.pop();
        }
        p1 = p;
      } else {
        auto& oo = os.top();
        oo.p1 = p;
      }
    }
    p++;
  }
  total += parse_day12({p1, p});
  return total;
}

} // namespace aoc2015