blob: 2f7729f70b146a8395a845b36905a2a07ba5289e (
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
|
#include "aoc.h"
#include <stack>
#include <vector>
namespace aoc2018 {
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;
}
struct header {
int nodes;
int metas;
};
int day8(line_view file) {
std::vector<int> is;
const char* p1 = file.line;
const char* p2 = file.line + file.length;
while (p1 < p2) {
if (*p1 >= '0' && *p1 <= '9') {
int d{0};
get_number(&p1, &d);
is.push_back(d);
}
p1++;
}
std::stack<header> hs;
size_t index{0};
int total{0};
hs.push({is[index], is[index + 1]});
index += 2;
while (!hs.empty()) {
auto& h = hs.top();
if (h.nodes > 0) {
hs.push({is[index], is[index + 1]});
index += 2;
h.nodes -= 1;
} else {
while (h.metas > 0) {
total += is[index];
index += 1;
h.metas -= 1;
}
hs.pop();
}
}
return total;
}
} // namespace aoc2018
|