Repository navigation
Expand file tree
/
Copy path11a.cpp
More file actions
210 lines (173 loc) · 4.59 KB
/
Copy path11a.cpp
File metadata and controls
210 lines (173 loc) · 4.59 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <fstream>
#include <iostream>
#include <vector>
#include <array>
#include <sstream>
#include <utility>
#include <unordered_set>
#include <functional>
#include <deque>
#include <algorithm>
const std::string INPUT_FILE = "./data/11.txt";
const size_t number_monkeys = 7;
class Monkey {
public:
size_t number_inspects;
Monkey(std::deque<int> itemst, std::function<int(int)> inspectt, int testt, size_t throw_if_truet, size_t throw_if_falset) {
items = itemst;
inspect = inspectt;
test = testt;
throw_if_true = throw_if_truet;
throw_if_false = throw_if_falset;
number_inspects = 0;
}
void print() {
std::cout << "Items: [";
for (auto &x: items) {
std::cout << x << ", ";
}
std::cout << "]" << std::endl;
}
std::pair<size_t, int> toss() {
if (items.empty()) {
std::cout << "Empty!" << std::endl;
}
int a = items.front();
items.pop_front();
a = inspect(a) / 3;
++number_inspects;
if (a % test == 0) {
return {throw_if_true, a};
}
return {throw_if_false, a};
}
bool empty() {
return items.empty();
}
void push_back(int a) {
items.push_back(a);
}
private:
std::deque<int> items = std::deque<int>(0);
std::function<int(int)> inspect;
int test;
size_t throw_if_true;
size_t throw_if_false;
};
class Monkeys {
public:
Monkeys(std::vector<Monkey> monkeyst) {
monkeys = monkeyst;
}
void simulate() {
for (auto &m: monkeys) {
while (!m.empty()) {
auto [pos, val] = m.toss();
monkeys[pos].push_back(val);
}
}
}
int monkey_business() {
auto vals = std::vector<int>();
for (auto &m: monkeys) {
vals.push_back(m.number_inspects);
}
std::cout << "Number inspects:" << std::endl;
for (auto &num: vals) {
std:: cout << num << std::endl;
}
std::sort(vals.begin(), vals.end());
std::cout << "Monkey business:" << std::endl;
int business = vals.back() * vals[vals.size() - 2];
std::cout << business << std::endl;
return business;
}
private:
std::vector<Monkey> monkeys;
};
void match(std::string pattern, std::string &parse) {
// std::cout << "Expected: " << pattern << std::endl;
// std::cout << "Got : " << parse << std::endl;
if (parse.size() < pattern.size()) {
std::cout << "Expected: " << pattern << std::endl;
std::cout << "Got : " << parse << std::endl;
throw std::exception();
}
for (size_t k = 0; k < pattern.size(); ++k) {
if (pattern[k] != parse[k]) {
std::cout << "Expected: " << pattern << std::endl;
std::cout << "Got : " << parse << std::endl;
throw std::exception();
}
}
parse.erase(0, pattern.size());
}
Monkey read_monkey(std::ifstream &infile) {
std::string line;
std::getline(infile, line);
match("Monkey ", line);
std::getline(infile, line);
std::deque<int> items;
match(" Starting items: ", line);
std::istringstream iss(line);
std::string entry;
while (std::getline(iss, entry, ',')) {
// std::cout << entry << std::endl;
items.push_back(std::stoi(entry));
}
iss.clear();
// std::cout << line << std::endl;
std::getline(infile, line);
// std::cout << line << std::endl;
std::function<int(int)> fun;
match(" Operation: new = old ", line);
char op = line[0];
// std::cout << line.size() << std::endl;
line.erase(0, 2);
if (line[0] == 'o') {
if (op == '+') {
fun = [] (int x) -> int { return x + x; };
} else if (op == '*') {
fun = [] (int x) -> int { return x * x; };
} else {
throw std::exception();
}
} else {
int a = std::stoi(line);
if (op == '+') {
fun = [a] (int x) -> int { return x + a; };
} else if (op == '*') {
fun = [a] (int x) -> int { return x * a; };
} else {
throw std::exception();
}
}
std::getline(infile, line);
match(" Test: divisible by ", line);
int test = std::stoi(line);
std::getline(infile, line);
match(" If true: throw to monkey ", line);
int throw_if_true = std::stoi(line);
std::getline(infile, line);
match(" If false: throw to monkey ", line);
int throw_if_false = std::stoi(line);
std::getline(infile, line);
return Monkey(items, fun, test, throw_if_true, throw_if_false);
}
std::vector<Monkey> read() {
std::ifstream infile(INPUT_FILE);
std::vector<Monkey> data;
for (size_t i = 0; i < number_monkeys + 1; ++i) {
data.push_back(read_monkey(infile));
}
return data;
}
int main() {
auto input = read();
auto monkeys = Monkeys(input);
for (int k = 0; k < 20; ++k) {
monkeys.simulate();
}
monkeys.monkey_business();
return 0;
}