-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc22day_11.cpp
161 lines (131 loc) · 4.12 KB
/
aoc22day_11.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Day 11: Monkey in the Middle
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
enum Operator {
ADD,
MULTIPLY
};
struct Operation {
Operator op;
int value;
};
struct Monkey {
std::queue<long long> items;
Operation operation;
int divisor;
size_t target_true;
size_t target_false;
int inspections = 0;
};
long long monkey_business(
std::vector<Monkey> monkeys,
std::function<long long(long long)> modifier,
int rounds
) {
for (int round = 0; round < rounds; ++round) {
for (Monkey &monkey : monkeys) {
monkey.inspections += monkey.items.size();
for (; !monkey.items.empty(); monkey.items.pop()) {
long long item = monkey.items.front();
int value;
if (monkey.operation.value == -1) {
value = item;
} else {
value = monkey.operation.value;
}
switch (monkey.operation.op) {
case ADD:
item += value;
break;
case MULTIPLY:
item *= value;
break;
}
item = modifier(item);
if (item % monkey.divisor == 0) {
monkeys[monkey.target_true].items.push(item);
} else {
monkeys[monkey.target_false].items.push(item);
}
}
}
}
long long first = 0;
long long second = 0;
for (Monkey &monkey : monkeys) {
if (monkey.inspections > first) {
second = first;
first = monkey.inspections;
} else if (monkey.inspections > second) {
second = monkey.inspections;
}
}
return first * second;
}
int main() {
std::ifstream file("aoc22day11_data.txt");
std::vector<Monkey> monkeys;
int product = 1;
Monkey current_monkey = Monkey();
int monkey_line = 0;
for (std::string line; std::getline(file, line); ++monkey_line) {
switch (monkey_line) {
case 1: {
std::replace(line.begin(), line.end(), ',', ' ');
std::istringstream stream(line.substr(18));
for (std::string item; stream >> item;) {
current_monkey.items.push(std::stoi(item));
}
} break;
case 2: {
switch (line[23]) {
case '+': current_monkey.operation.op = ADD; break;
case '*': current_monkey.operation.op = MULTIPLY; break;
}
std::string value_str = line.substr(25);
if (value_str == "old") {
current_monkey.operation.value = -1;
} else {
current_monkey.operation.value = std::stoi(value_str);
}
} break;
case 3:
current_monkey.divisor = std::stoi(line.substr(21));
product *= current_monkey.divisor;
break;
case 4:
current_monkey.target_true = std::stoi(line.substr(29));
break;
case 5:
current_monkey.target_false = std::stoi(line.substr(30));
monkeys.push_back(current_monkey);
current_monkey = Monkey();
monkey_line = -2;
break;
}
}
std::cout << "[PART 1] Monkey business: "
<< monkey_business(
monkeys,
[](long long item) -> long long {
return item / 3;
},
20
)
<< "\n";
std::cout << "[PART 2] Monkey business: "
<< monkey_business(
monkeys,
[product](long long item) -> long long {
return item % product;
},
10000
)
<< "\n";
}