From e12db36a20ba78954ea690d9e1e80949c9eaf791 Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Thu, 8 Dec 2022 16:48:06 -0500 Subject: [PATCH] Day 8 --- Day8/part1.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ Day8/part2.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 Day8/part1.cpp create mode 100644 Day8/part2.cpp diff --git a/Day8/part1.cpp b/Day8/part1.cpp new file mode 100644 index 0000000..26b497a --- /dev/null +++ b/Day8/part1.cpp @@ -0,0 +1,93 @@ +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + string line; + getline(cin, line); + + int length = line.size(); + + int* forest = new int[length*length]; + int in_y = 0; + do + { + for(int i = 0; i < length; i++) + { + forest[i+in_y*length] = line[i]-'0'; + } + ++in_y; + } while(getline(cin, line)); + + bool* visible = new bool[length*length]; + for(int i = 0; i < length*length; i++) + { + visible[i] = false; + } + + for(int x = 0; x < length; x++) + { + //top down + int vis_height = -1; + for(int y = 0; y < length; y++) + { + if (forest[x+y*length] > vis_height) + { + vis_height = forest[x+y*length]; + visible[x+y*length] = true; + } + } + + //bottom up + vis_height = -1; + for(int y = length-1; y >= 0; y--) + { + if (forest[x+y*length] > vis_height) + { + vis_height = forest[x+y*length]; + visible[x+y*length] = true; + } + } + } + + for(int y = 0; y < length; y++) + { + //Left right + int vis_height = -1; + for(int x = 0; x < length; x++) + { + if (forest[x+y*length] > vis_height) + { + vis_height = forest[x+y*length]; + visible[x+y*length] = true; + } + } + + //Right left + vis_height = -1; + for(int x = length-1; x >= 0; x--) + { + if (forest[x+y*length] > vis_height) + { + vis_height = forest[x+y*length]; + visible[x+y*length] = true; + } + } + } + + int total_vis = 0; + for(int i = 0; i < length*length; i++) + { + if(visible[i]) + { + ++total_vis; + } + } + + cout << total_vis << endl; + + delete[] visible; + delete[] forest; +} \ No newline at end of file diff --git a/Day8/part2.cpp b/Day8/part2.cpp new file mode 100644 index 0000000..b1e0f36 --- /dev/null +++ b/Day8/part2.cpp @@ -0,0 +1,83 @@ +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + string line; + getline(cin, line); + + int length = line.size(); + + int* forest = new int[length*length]; + int in_y = 0; + do + { + for(int i = 0; i < length; i++) + { + forest[i+in_y*length] = line[i]-'0'; + } + ++in_y; + } while(getline(cin, line)); + + int best_vis = 0; + for(int y = 0; y < length; y++) + { + for(int x = 0; x < length; x++) + { + int north_vis = 0; + int south_vis = 0; + int west_vis = 0; + int east_vis = 0; + int cur_vis = forest[x+y*length];; + + for(int j = y-1; j >= 0; j--) + { + north_vis++; + if (forest[x+j*length] >= cur_vis) + { + break; + } + } + + for(int j = y+1; j < length; j++) + { + south_vis++; + if (forest[x+j*length] >= cur_vis) + { + break; + } + } + + for(int i = x-1; i >= 0; i--) + { + west_vis++; + if (forest[i+y*length] >= cur_vis) + { + break; + } + } + + for(int i = x+1; i= cur_vis) + { + break; + } + } + + int total_vis = north_vis*south_vis*east_vis*west_vis; + + if (total_vis > best_vis) + { + best_vis = total_vis; + } + } + } + + cout << best_vis << endl; + + delete[] forest; +} \ No newline at end of file