blob: 70be05c964f3acb87348b37e19a5a999219b4d7b (
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#include "common.h"
#include <vector>
#include <deque>
#include <functional>
namespace aoc2022 {
struct addx {
int x;
size_t operator()(size_t n) {
return n + x;
}
};
struct mulx {
int x;
size_t operator()(size_t n) {
return n * (x > 0 ? x : n);
}
};
typedef size_t(*func)(size_t);
struct monkey {
std::deque<size_t> items;
std::function<size_t(size_t)> op;
int count = 0;
int mod = 0;
int to_t = 0;
int to_f = 0;
int get_number(const char* p) {
int d{0};
while(*p >= '0' && *p <= '9') {
d = d * 10 + *p - '0';
p++;
}
return d;
}
bool get_number(const char** pp, int* d) {
const char *p = *pp;
while(*p >= '0' && *p <= '9') {
*d = 10 * (*d) + *p - '0';
p++;
}
*pp = p;
return *d > 0;
}
void print() const noexcept {
for (auto& i: items) {
printf("%zu ", i);
}
printf("\nmod[%d] to_t[%d] to_f[%d]\n\n", mod, to_t, to_f);
}
monkey(line_view lv) {
const char* p = lv.line;
while (p < lv.line + lv.length) {
if(*p == 's' && *(p+1) == ':') {
int d{0};
const char *pp = p + 3;
while(get_number(&pp, &d)) {
items.push_back(d);
d = 0;
pp += 2;
}
}
if(*p == ' ' && *(p+1) == '*') {
int d = get_number(p + 3);
op = [d](size_t x){ return x * (d > 0 ? d : x);};
// op = mulx{d};
}
if(*p == ' ' && *(p+1) == '+') {
int d = get_number(p + 3);
op = [d](size_t x){ return x + d;};
// op = addx{d};
}
if(*p == 'b' && *(p+1) == 'y') {
mod = get_number(p + 3);
}
if(*p == 'u' && *(p+1) == 'e') {
to_t = get_number(p + 20);
}
if(*p == 's' && *(p+1) == 'e') {
to_f = get_number(p + 20);
}
p++;
}
}
void transfer(monkey* ms, func f) {
while (!items.empty()) {
size_t x = items.front();
items.pop_front();
count += 1;
size_t n = op(x);
n = f(n);
if ( n % mod == 0) {
ms[to_t].items.push_back(n);
}
else {
ms[to_f].items.push_back(n);
}
}
}
};
std::pair<size_t, size_t> day11(line_view);
}
|