aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-13 14:55:32 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-13 14:55:32 -0500
commite23196f20f7aec2f87127d18060bafd096c3c744 (patch)
tree5b59769f01fb71879cb12100e39a724c52ac9ecb
parentee8c639a38542165a5f9e5ac7d130cd6060e25bc (diff)
add day 13 c++
-rw-r--r--day13/part1.cpp147
-rw-r--r--day13/part2.cpp119
2 files changed, 266 insertions, 0 deletions
diff --git a/day13/part1.cpp b/day13/part1.cpp
new file mode 100644
index 0000000..b725edd
--- /dev/null
+++ b/day13/part1.cpp
@@ -0,0 +1,147 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+int consumeInt(std::string& s)
+{
+ int n = 0;
+ int i;
+
+ for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
+ n = n * 10 + s[i] - '0';
+
+ s = s.substr(i);
+ return n;
+}
+
+// 5762 too high
+// 5659
+// 5632 too low
+
+int main()
+{
+ std::string left, right;
+ std::vector<int> correct;
+ int index = 1;
+
+ while (1) {
+ std::getline(std::cin, left);
+ if (std::cin.eof())
+ break;
+ std::getline(std::cin, right);
+ if (std::cin.eof())
+ break;
+
+ int lt = 0, rt = 0;
+
+ std::cout << left << std::endl;
+ std::cout << right << std::endl;
+
+ while (left.size() != 0 && right.size() != 0) {
+ if (left[0] == ']') {
+ if (right[0] != ']') {
+ std::cout << "Left side ran out of items" << std::endl;
+ correct.push_back(index);
+ break;
+ }
+ if (rt) --rt;
+ else right = right.substr(1);
+ left = left.substr(1);
+ } else if (right[0] == ']') {
+ if (left[0] != ']') {
+ std::cout << "Right side ran out of items" << std::endl;
+ break; // incorrect
+ }
+ if (lt) --lt;
+ else left = left.substr(1);
+ right = right.substr(1);
+ } else if (left[0] == '[') {
+ std::cout << "Left side opens" << std::endl;
+ if (right[0] != '[')
+ ++rt;
+ else
+ right = right.substr(1);
+ left = left.substr(1);
+ } else if (right[0] == '[') {
+ std::cout << "Right side opens" << std::endl;
+ if (left[0] != '[')
+ ++lt;
+ else
+ left = left.substr(1);
+ right = right.substr(1);
+ } else {
+ int ln = consumeInt(left);
+ int rn = consumeInt(right);
+ std::cout << "Compare " << ln << " to " << rn << std::endl;
+
+ if (ln < rn) {
+ correct.push_back(index);
+ break;
+ } else if (ln > rn) {
+ break;
+ }
+
+ int fail = 0;
+ while (rt) {
+ if (left[0] != ']') {
+ fail = 1;
+ std::cout << "Right ends early" << std::endl;
+ break;
+ }
+
+ left = left.substr(1);
+ --rt;
+ }
+ while (lt) {
+ if (right[0] != ']') {
+ fail = 1;
+ std::cout << "Left ends early" << std::endl;
+ correct.push_back(index);
+ break;
+ }
+
+ right = right.substr(1);
+ --lt;
+ }
+ if (fail) break;
+ }
+
+ bool lcomma = left[0] == ',';
+ if (lcomma) {
+ left = left.substr(1);
+ if (right[0] != ',') {
+ std::cout << "Right ends early" << std::endl;
+ break;
+ }
+ }
+ if (right[0] == ',') {
+ right = right.substr(1);
+ if (!lcomma) {
+ std::cout << "Left ends early" << std::endl;
+ correct.push_back(index);
+ break;
+ }
+ }
+ }
+
+ if (left.size() == 0 && right.size() == 0)
+ correct.push_back(index);
+
+ if (correct.size() > 0 && correct.back() == index)
+ std::cout << " correct" << std::endl;
+ else
+ std::cout << std::endl;
+
+ index++;
+ std::getline(std::cin, left);
+ if (std::cin.eof())
+ break;
+ }
+
+ int sum = 0;
+ for (auto& w : correct)
+ sum += w;
+
+ std::cout << "Total corrects: " << sum << std::endl;
+}
+
diff --git a/day13/part2.cpp b/day13/part2.cpp
new file mode 100644
index 0000000..3ecf8d2
--- /dev/null
+++ b/day13/part2.cpp
@@ -0,0 +1,119 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+int consumeInt(std::string& s)
+{
+ int n = 0;
+ int i;
+
+ for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
+ n = n * 10 + s[i] - '0';
+
+ s = s.substr(i);
+ return n;
+}
+
+int compare(std::string left, std::string right)
+{
+ int lt = 0, rt = 0;
+
+ while (left.size() != 0 && right.size() != 0) {
+ if (left[0] == ']') {
+ if (right[0] != ']') {
+ return 1;
+ }
+ if (rt) --rt;
+ else right = right.substr(1);
+ left = left.substr(1);
+ } else if (right[0] == ']') {
+ if (left[0] != ']') {
+ return 0;
+ }
+ if (lt) --lt;
+ else left = left.substr(1);
+ right = right.substr(1);
+ } else if (left[0] == '[') {
+ if (right[0] != '[')
+ ++rt;
+ else
+ right = right.substr(1);
+ left = left.substr(1);
+ } else if (right[0] == '[') {
+ if (left[0] != '[')
+ ++lt;
+ else
+ left = left.substr(1);
+ right = right.substr(1);
+ } else {
+ int ln = consumeInt(left);
+ int rn = consumeInt(right);
+
+ if (ln < rn) {
+ return 1;
+ } else if (ln > rn) {
+ return 0;
+ }
+
+ while (rt) {
+ if (left[0] != ']') {
+ return 0;
+ }
+
+ left = left.substr(1);
+ --rt;
+ }
+ while (lt) {
+ if (right[0] != ']') {
+ return 1;
+ }
+
+ right = right.substr(1);
+ --lt;
+ }
+ }
+
+ bool lcomma = left[0] == ',';
+ if (lcomma) {
+ left = left.substr(1);
+ if (right[0] != ',') {
+ return 0;
+ }
+ }
+ if (right[0] == ',') {
+ right = right.substr(1);
+ if (!lcomma) {
+ return 1;
+ }
+ }
+ }
+
+ if (left.size() == 0 && right.size() == 0)
+ return 1;
+ else
+ return 0;
+}
+
+int main()
+{
+ std::string line;
+ int lt2 = 0;
+ int lt6 = 0;
+
+ while (1) {
+ std::getline(std::cin, line);
+ if (std::cin.eof())
+ break;
+ if (line.empty())
+ continue;
+
+ if (compare(line, "[[2]]"))
+ lt2++;
+ if (compare(line, "[[6]]"))
+ lt6++;
+ }
+
+ std::cout << "lt2: " << lt2 + 1 << " lt6: " << lt6 + 2 << std::endl;
+ std::cout << "product: " << (lt2 + 1) * (lt6 + 2) << std::endl;
+}
+