aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-11 08:23:26 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-11 08:23:26 -0500
commit6676f6711f0f6a93ff4f2a9035b4d6181737507d (patch)
tree79995e672b5d480ae86a2f8766b8456788a43e55
parentcf84d0ecbd5df06ef93dac6045d6d410753b6e73 (diff)
add day 11 c++
-rw-r--r--day11/part1.cpp87
-rw-r--r--day11/part2.cpp92
2 files changed, 179 insertions, 0 deletions
diff --git a/day11/part1.cpp b/day11/part1.cpp
new file mode 100644
index 0000000..38bec2f
--- /dev/null
+++ b/day11/part1.cpp
@@ -0,0 +1,87 @@
+#include <array>
+#include <iostream>
+#include <string>
+
+int main()
+{
+ int monkeys[10][100];
+ int ms[10];
+ int ic[10] = {};
+ int idx = -1;
+ std::string im;
+
+ do {
+ std::string line;
+ std::getline(std::cin, line);
+
+ if (!std::cin.eof()) {
+ if (line.starts_with("Monkey")) {
+ ++idx;
+ std::cout << "Monkey " << idx << "..." << std::endl;
+ } else if (line.starts_with(" St")) {
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im += (char)stoi(line);
+
+ auto g = line.find(',');
+ while (g != std::string::npos) {
+ line = line.substr(g);
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im += (char)stoi(line);
+ g = line.find(',');
+ }
+ } else if (line.starts_with(" Op")) {
+ auto f = line.find('=');
+ char o = line[f + 6];
+ char v = line[f + 8] == 'o' ? 0 : stoi(line.substr(f + 8));
+ im = std::string() + v + o + im;
+ } else if (!line.empty()) {
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im = std::string() + (char)stoi(line) + im;
+ } else {
+ int i = 0;
+ for (auto c : im)
+ monkeys[idx][i++] = c;
+ ms[idx] = im.size();
+ im.clear();
+ }
+ }
+ } while (!std::cin.eof());
+
+ for (int R = 0; R < 20; ++R) {
+
+ for (int i = 0; i <= idx; ++i) {
+ auto s = monkeys[i];
+ for (int j = 5; j < ms[i]; ++j) {
+ int worry = s[j];
+
+ int o = s[3] != 0 ? s[3] : worry;
+ if (s[4] == '*')
+ worry *= o;
+ else if (s[4] == '+')
+ worry += o;
+
+ worry /= 3;
+
+ int d = worry / s[2];
+ int k = worry == d * s[2] ? s[1] : s[0];
+ monkeys[k][ms[k]++] = worry;
+ }
+ ic[i] += ms[i] - 5;
+ ms[i] = 5;
+ }
+
+ }
+
+ for (int q = 0; q <= idx; ++q) {
+ std::cout << ic[q] << std::endl;
+ }
+
+ return 0;
+}
+
diff --git a/day11/part2.cpp b/day11/part2.cpp
new file mode 100644
index 0000000..fc0a9f4
--- /dev/null
+++ b/day11/part2.cpp
@@ -0,0 +1,92 @@
+#include <array>
+#include <cstdint>
+#include <iostream>
+#include <string>
+
+int main()
+{
+ uint64_t monkeys[10][100];
+ int ms[10];
+ int ic[10] = {};
+ int idx = -1;
+ std::string im;
+
+ do {
+ std::string line;
+ std::getline(std::cin, line);
+
+ if (!std::cin.eof()) {
+ if (line.starts_with("Monkey")) {
+ ++idx;
+ std::cout << "Monkey " << idx << "..." << std::endl;
+ } else if (line.starts_with(" St")) {
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im += (char)stoi(line);
+
+ auto g = line.find(',');
+ while (g != std::string::npos) {
+ line = line.substr(g);
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im += (char)stoi(line);
+ g = line.find(',');
+ }
+ } else if (line.starts_with(" Op")) {
+ auto f = line.find('=');
+ char o = line[f + 6];
+ char v = line[f + 8] == 'o' ? 0 : stoi(line.substr(f + 8));
+ im = std::string() + v + o + im;
+ } else if (!line.empty()) {
+ auto f = line.find_first_of("0123456789");
+ line = line.substr(f);
+
+ im = std::string() + (char)stoi(line) + im;
+ } else {
+ int i = 0;
+ for (auto c : im)
+ monkeys[idx][i++] = c;
+ ms[idx] = im.size();
+ im.clear();
+ }
+ }
+ } while (!std::cin.eof());
+
+ uint64_t supermod = 1;
+ for (int i = 0; i <= idx; ++i)
+ supermod *= monkeys[i][2];
+
+ for (int R = 0; R < 10000; ++R) {
+
+ for (int i = 0; i <= idx; ++i) {
+ auto s = monkeys[i];
+ for (int j = 5; j < ms[i]; ++j) {
+ auto worry = s[j] % supermod;
+
+ auto o = s[3] != 0 ? s[3] : worry;
+ if (s[4] == '*')
+ worry = worry * o;
+ if (s[4] == '+')
+ worry += o;
+
+ //worry /= 3;
+
+ auto d = worry / s[2];
+ auto k = worry == d * s[2] ? s[1] : s[0];
+ monkeys[k][ms[k]++] = worry;
+ }
+ ic[i] += ms[i] - 5;
+ ms[i] = 5;
+ }
+
+ }
+
+ for (int q = 0; q <= idx; ++q) {
+ std::cout << ic[q] << std::endl;
+ }
+
+ return 0;
+}
+