<- here("DATA", "2022", "input_day_08.txt")
input_file_path
<- system(paste("wc -l", input_file_path), intern = TRUE) %>%
no_rows word(1) %>%
as.numeric()
<- scan(input_file_path, what = "character", nmax = 1) %>% str_length() no_cols
Day 8
Data
The input is height of the trees on a grid.
There are 99 rows and 99 columns on the grid.
<- scan(
input_grid
input_file_path,what = "character"
)# let's make a matrix out of it
<- map(
input_grid
input_grid,function(row){
<- str_split(row, pattern = "") %>%
cur_row_trees unlist()
return(as.numeric(cur_row_trees))
%>%
}) do.call(rbind, args = .)
We can visualize it:
%>%
input_grid as_tibble(rownames = "row") %>%
pivot_longer(cols = V1:V99, names_to = "col", values_to = "val") %>%
mutate(
col = as.numeric(str_sub(col, start = 2, end = -1)),
row = as.numeric(row)
%>%
) ggplot(aes(col, row)) +
geom_tile(aes(fill = as.factor(val))) +
coord_fixed() +
scale_fill_grey() +
theme_minimal()
Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
`.name_repair` is omitted as of tibble 2.0.0.
ℹ Using compatibility `.name_repair`.
Functions
<- function(sequence, last = TRUE){
check_visibility # I have a sequence of heights that ends or begins with the tree we are checking
if(last){
<- sequence[length(sequence)]
chosen_tree <- sequence[-length(sequence)]
other_trees return(all(other_trees < chosen_tree))
}
<- sequence[1]
chosen_tree <- sequence[-1]
other_trees return(all(other_trees < chosen_tree))
}
Check all trees
We don’t need to check trees that are on the edge.
<- no_cols*2 + (no_rows - 2)*2
no_visible_trees
<- matrix(
visibility_matrix NA, nrow = no_rows, ncol = no_cols
)for(cur_row in 2:(no_rows - 1)){
for(cur_col in 2:(no_cols - 1)){
# check from top
<- input_grid[1:cur_row, cur_col]
cur_sequence <- check_visibility(cur_sequence)
vis_from_top # check from bottom
<- input_grid[cur_row:no_rows, cur_col]
cur_sequence <- check_visibility(cur_sequence, FALSE)
vis_from_bottom # check from left
<- input_grid[cur_row, 1:cur_col]
cur_sequence <- check_visibility(cur_sequence)
vis_from_left # check from right
<- input_grid[cur_row, cur_col:no_cols]
cur_sequence <- check_visibility(cur_sequence, FALSE)
vis_from_right
<- any(
cur_tree_vis c(vis_from_top, vis_from_bottom, vis_from_left, vis_from_right)
)<- cur_tree_vis
visibility_matrix[cur_row, cur_col] <<- no_visible_trees + as.numeric(cur_tree_vis)
no_visible_trees
} }
That’s how looks the visibility matrix:
%>%
visibility_matrix as_tibble(rownames = "row") %>%
pivot_longer(cols = V1:V99, names_to = "col", values_to = "val") %>%
mutate(
col = as.numeric(str_sub(col, start = 2, end = -1)),
row = as.numeric(row)
%>%
) ggplot(aes(col, row)) +
geom_tile(aes(fill = val)) +
coord_fixed() +
scale_fill_grey() +
theme_minimal()
There are 1823 trees that are visible from outside of the grid! 🥳