commit a2f7e550d795b7112cabf6290c80e57f92bdbe4a
parent b1d7b9b214cbdca206b659d338952053f3c2f885
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Sun, 11 Dec 2022 12:56:06 -0500
Day 8 challenge complete
Diffstat:
M | day8/main.c | | | 81 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
1 file changed, 58 insertions(+), 23 deletions(-)
diff --git a/day8/main.c b/day8/main.c
@@ -5,26 +5,6 @@
#define DIMENSION 99
-// Create 2D array for all the trees, then go through each tree one by one
-//
-// start assuming not visible
-// if it's an edge tree, no further processing, it's visible
-// if it's an inner tree, navigate in each direction, if every tree in a given
-// direction is < than this one, it's visible (stop processing)
-
-void
-splitString(char *string, char delim, char **result)
-{
- int index = 0;
- for (int i = 0; i < strlen(string); i++)
- {
- if (string[i] == delim)
- index++;
- else
- strncat(result[index], &string[i], 1);
- }
-}
-
int
main (void)
{
@@ -35,8 +15,9 @@ main (void)
exit(EXIT_FAILURE);
}
- int numTreesVisible = 0;
int trees[DIMENSION][DIMENSION] = { 0 };
+ int numTreesVisible = 0;
+ int mostScenicTree = 0;
int linecount = 0;
char *line = NULL;
@@ -55,10 +36,64 @@ main (void)
fclose(fp);
for (int i = 0; i < DIMENSION; i++)
+ {
for (int j = 0; j < DIMENSION; j++)
- if (i == 0 || j == 0 || i == DIMENSION-1 || j == DIMENSION-1)
+ {
+ int visibleDirections = 4;
+ int scenicValues[4] = { 0 };
+ int scenicScore = 0;
+ // North
+ for (int k = j-1; k >= 0; k--)
+ {
+ scenicValues[0]++;
+ if (trees[i][j] <= trees[i][k])
+ {
+ visibleDirections--;
+ break;
+ }
+ }
+ // South
+ for (int k = j+1; k < DIMENSION; k++)
+ {
+ scenicValues[1]++;
+ if (trees[i][j] <= trees[i][k])
+ {
+ visibleDirections--;
+ break;
+ }
+ }
+ // East
+ for (int k = i+1; k < DIMENSION; k++)
+ {
+ scenicValues[2]++;
+ if (trees[i][j] <= trees[k][j])
+ {
+ visibleDirections--;
+ break;
+ }
+ }
+ // West
+ for (int k = i-1; k >= 0; k--)
+ {
+ scenicValues[3]++;
+ if (trees[i][j] <= trees[k][j])
+ {
+ visibleDirections--;
+ break;
+ }
+ }
+
+ if (visibleDirections)
numTreesVisible++;
- printf("Part 1: %d", numTreesVisible);
+ scenicScore = scenicValues[0] * scenicValues[1]
+ * scenicValues[2] * scenicValues[3];
+
+ if (scenicScore > mostScenicTree)
+ mostScenicTree = scenicScore;
+ }
+ }
+ printf("PART 1: The number of trees visible from the edge is %d\n", numTreesVisible);
+ printf("PART 2: The most scenic tree has a scenic value of %d\n", mostScenicTree);
exit(EXIT_SUCCESS);
}