Day 08: Treetop Tree House
**
There's a grid of tree of different heights. Part I is figuring out how many trees are visible from outside the grid. Part II is figuring out which tree can see the most other trees.
Journal
This one is pretty hacky and just trying the first obvious solution of cycling through all the trees and either checking if they're visible by trying to get out in each direction (Part I) or seeing how many trees they could see in each direction (Part II).
That worked so I didn't see much point in trying to clean it up any...
Relevant Code
export type Grid = number[][];const getVisibility = (row: number, column: number, grid: Grid) => {const height = grid[row][column];// Check each direction// if we get to the outside return true// if we get to a taller tree end the for loop in a super hacky way to move alongfor (let i = row + 1; i <= grid.length; i++) {if (i === grid.length) {return true;}if (grid[i][column] >= height) {i = grid.length + 1;}}for (let i = row - 1; i >= -1; i--) {if (i === -1) {return true;}if (grid[i][column] >= height) {i = -2;}}for (let i = column + 1; i <= grid[row].length; i++) {if (i === grid[row].length) {return true;}if (grid[row][i] >= height) {i = grid[row].length + 1;}}for (let i = column - 1; i >= -1; i--) {if (i === -1) {return true;}if (grid[row][i] >= height) {i = -2;}}return false;};const getViewingScore = (row: number, column: number, grid: Grid) => {const height = grid[row][column];// Similar to getVisbility, but this time add up trees along the way and no early returnslet right = 0;for (let i = row + 1; i < grid.length; i++) {if (grid[i][column] >= height) {i = grid.length;}right++;}let left = 0;for (let i = row - 1; i >= 0; i--) {if (grid[i][column] >= height) {i = -1;}left++;}let down = 0;for (let i = column + 1; i < grid[row].length; i++) {if (grid[row][i] >= height) {i = grid[row].length;}down++;}let up = 0;for (let i = column - 1; i >= 0; i--) {if (grid[row][i] >= height) {i = -1;}up++;}return right * left * up * down;};// Part Iexport const getVisibleTrees = (grid: Grid) => {let visibleCount = 0;grid.forEach((row, x) => {row.forEach((tree, y) => {const isVisible = getVisibility(x, y, grid);visibleCount += isVisible ? 1 : 0;});});return visibleCount;};// Part IIexport const getBestTree = (grid: Grid) => {let bestTree = 0;grid.forEach((row, x) => {row.forEach((tree, y) => {const visibleScore = getViewingScore(x, y, grid);bestTree = Math.max(visibleScore, bestTree);});});return bestTree;};// Start here for both Part I and Part II to get the gridexport const getGrid = (input: string) => {const rows = input.split("\n");return rows.reduce((accum, row) => {const columns = [];for (let i = 0; i < row.length; i++) {columns.push(+row[i]);}accum.push(columns);return accum;}, [] as Grid);};
Output
Test Grid
- Making the grid took 0 ms
- Part I: 21 visible trees (0 ms)
- Part II: Best tree visibility score is 8 (0 ms)
Real Grid
- Making the grid took 3 ms
- Part I: 1801 visible trees (5 ms)
- Part II: Best tree visibility score is 209880 (5 ms)