-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.R
50 lines (45 loc) · 1.15 KB
/
day08.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# read data
input <- strsplit(readLines("data/2022/day08.txt"), "")
dims <- length(input)
mat <- input |>
unlist() |>
as.numeric() |>
matrix(byrow = TRUE, nrow = dims)
# tree scoring function
tree_score <- function(val, tree) {
ifelse(
any(val >= tree),
length(val[1:which(val >= tree)[1]]),
length(val)
)
}
# for loops to iterate through rows/cols
vis <- matrix(rep(TRUE, dims * dims), nrow = dims)
scenic <- matrix(rep(TRUE, dims * dims), nrow = dims)
for(i in 2:(dims - 1)) {
for(j in 2:(dims - 1)) {
tree <- mat[i, j]
vis[i, j] <- any(
c(
all(mat[1:(i - 1), j] < tree),
all(mat[(i + 1):dims, j] < tree),
all(mat[i, 1:(j - 1)] < tree),
all(mat[i, (j + 1):dims] < tree)
)
)
scenic[i, j] <- prod(
c(
tree_score(rev(mat[1:(i - 1), j]), tree),
tree_score(mat[(i + 1):dims, j], tree),
tree_score(rev(mat[i, 1:(j - 1)]), tree),
tree_score(mat[i, (j + 1):dims], tree)
)
)
}
}
# part 1 solution
# how many trees are visible from outside the grid?
sum(vis)
# part 2 solution
# what is the highest scenic score possible for any tree?
max(scenic)