aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-14 08:14:15 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-14 08:14:15 -0500
commitba888c6728558891e9ce068bc5f5b43b357e51b6 (patch)
tree0cf60ea1cd2655722e5f8cf78addc64faf573e45
parente23196f20f7aec2f87127d18060bafd096c3c744 (diff)
add day 14 c++
-rw-r--r--day14/part1.cpp84
-rw-r--r--day14/part2.cpp84
2 files changed, 168 insertions, 0 deletions
diff --git a/day14/part1.cpp b/day14/part1.cpp
new file mode 100644
index 0000000..b95a2aa
--- /dev/null
+++ b/day14/part1.cpp
@@ -0,0 +1,84 @@
+#include <array>
+#include <iostream>
+#include <string>
+
+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 main()
+{
+ std::array<std::array<char, 101>, 165> map;
+ std::string line;
+
+ for (auto& m : map)
+ m.fill('.');
+
+ while (1) {
+ std::getline(std::cin, line);
+ if (std::cin.eof())
+ break;
+
+ auto px = consumeInt(line);
+ line = line.substr(1);
+ auto py = consumeInt(line);
+
+ while (!line.empty()) {
+ line = line.substr(4);
+ auto x = consumeInt(line);
+ line = line.substr(1);
+ auto y = consumeInt(line);
+
+ if (x != px) {
+ for (auto i = std::min(x, px); i <= std::max(x, px); ++i)
+ map[y][500 - i + 50] = '#';
+ } else {
+ for (auto i = std::min(y, py); i <= std::max(y, py); ++i)
+ map[i][500 - x + 50] = '#';
+ }
+
+ px = x;
+ py = y;
+ }
+ }
+
+ int sands = 0;
+ while (map[0][50] != '*') {
+ ++sands;
+ int x = 50, y = 0;
+ while (1) {
+ if (y > map.size() - 2) {
+ map[0][50] = '*';
+ break;
+ } else if (map[y + 1][x] == '.') {
+ ++y;
+ } else if (map[y + 1][x + 1] == '.') {
+ ++y, ++x;
+ } else if (map[y + 1][x - 1] == '.') {
+ ++y, --x;
+ } else {
+ map[y][x] = '*';
+ break;
+ }
+ }
+ }
+
+ for (auto& m : map) {
+ for (auto& n : m)
+ std::cout << n;
+ std::cout << std::endl;
+ }
+
+ std::cout << sands - 1 << std::endl;
+
+ return 0;
+}
+
diff --git a/day14/part2.cpp b/day14/part2.cpp
new file mode 100644
index 0000000..0777395
--- /dev/null
+++ b/day14/part2.cpp
@@ -0,0 +1,84 @@
+#include <array>
+#include <iostream>
+#include <string>
+
+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 main()
+{
+ std::array<std::array<char, 480>, 165> map;
+ std::string line;
+
+ for (auto& m : map)
+ m.fill('.');
+
+ while (1) {
+ std::getline(std::cin, line);
+ if (std::cin.eof())
+ break;
+
+ auto px = consumeInt(line);
+ line = line.substr(1);
+ auto py = consumeInt(line);
+
+ while (!line.empty()) {
+ line = line.substr(4);
+ auto x = consumeInt(line);
+ line = line.substr(1);
+ auto y = consumeInt(line);
+
+ if (x != px) {
+ for (auto i = std::min(x, px); i <= std::max(x, px); ++i)
+ map[y][500 - i + map[y].size() / 2] = '#';
+ } else {
+ for (auto i = std::min(y, py); i <= std::max(y, py); ++i)
+ map[i][500 - x + map[y].size() / 2] = '#';
+ }
+
+ px = x;
+ py = y;
+ }
+ }
+
+ int sands = 0;
+ while (map[0][map[0].size() / 2] != '*') {
+ ++sands;
+ int x = map[0].size() / 2, y = 0;
+ while (1) {
+ if (y > map.size() - 2) {
+ map[y][x] = '*';
+ break;
+ } else if (map[y + 1][x] == '.') {
+ ++y;
+ } else if (map[y + 1][x + 1] == '.') {
+ ++y, ++x;
+ } else if (map[y + 1][x - 1] == '.') {
+ ++y, --x;
+ } else {
+ map[y][x] = '*';
+ break;
+ }
+ }
+ }
+
+ for (auto& m : map) {
+ for (auto& n : m)
+ std::cout << n;
+ std::cout << std::endl;
+ }
+
+ std::cout << sands << std::endl;
+
+ return 0;
+}
+