aboutsummaryrefslogtreecommitdiffstats
path: root/day8
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2022-12-08 18:09:49 -0500
committerClyne Sullivan <clyne@bitgloo.com>2022-12-08 18:09:49 -0500
commit1efcf6b597f0c6feb7ff01ef9caeae8296f93951 (patch)
tree1ed2badfe76a3d263f3d759833934f334bea00a3 /day8
parenta5f17e298a1d7500ff6c3c53950b2af086948cff (diff)
add day 8 (c++)
Diffstat (limited to 'day8')
-rw-r--r--day8/both.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/day8/both.cpp b/day8/both.cpp
new file mode 100644
index 0000000..d604954
--- /dev/null
+++ b/day8/both.cpp
@@ -0,0 +1,107 @@
+#include <algorithm>
+#include <iostream>
+#include <string>
+#include <vector>
+
+int visible = 0;
+std::vector<std::string> map;
+
+bool checkTree(int x, int y)
+{
+ char c = map[y][x];
+ char v;
+
+ // Left check
+ v = '0';
+ for (int i = 0; i < x; ++i)
+ v = std::max(v, map[y][i]);
+ if (c > v)
+ return true;
+
+ // Right check
+ v = '0';
+ for (int i = map[y].size() - 1; i > x; --i)
+ v = std::max(v, map[y][i]);
+ if (c > v)
+ return true;
+
+ // North check
+ v = '0';
+ for (int i = 0; i < y; ++i)
+ v = std::max(v, map[i][x]);
+ if (c > v)
+ return true;
+
+ // South check
+ v = '0';
+ for (int i = map.size() - 1; i > y; --i)
+ v = std::max(v, map[i][x]);
+
+ return c > v;
+}
+
+int scenicScore(int x, int y)
+{
+ char c = map[y][x];
+ int n = 0, s = 0, e = 0, w = 0;
+
+ // Left check
+ for (int i = x - 1; i >= 0; --i) {
+ ++w;
+ if (c <= map[y][i])
+ break;
+ }
+
+ // Right check
+ for (int i = x + 1; i < map[y].size(); ++i) {
+ ++e;
+ if (c <= map[y][i])
+ break;
+ }
+
+ // North check
+ for (int i = y - 1; i >= 0; --i) {
+ ++n;
+ if (c <= map[i][x])
+ break;
+ }
+
+ // South check
+ for (int i = y + 1; i < map.size(); ++i) {
+ ++s;
+ if (c <= map[i][x])
+ break;
+ }
+
+ std::cout << x << ", " << y << ": " << n << ' ' << s << ' ' << e << ' ' << w << std::endl;
+ return n * s * e * w;
+}
+
+int main()
+{
+ // Consume entire input into map
+ while (!std::cin.eof()) {
+ std::string line;
+ std::getline(std::cin, line);
+ if (!std::cin.eof())
+ map.push_back(line);
+ }
+
+ visible = 2 * map[0].size() - 4 + 2 * map.size();
+ int sx, sy, sc = 0;
+
+ // For each possibly-hidden tree...
+ for (int y = 1; y < map.size() - 1; ++y) {
+ for (int x = 1; x < map[y].size() - 1; ++x) {
+ if (checkTree(x, y))
+ ++visible;
+ if (int qq = scenicScore(x, y); qq > sc)
+ sx = x, sy = y, sc = qq;
+ }
+ }
+
+ std::cout << visible << std::endl;
+ std::cout << sx << ", " << sy << ": " << sc << std::endl;
+
+ return 0;
+}