-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.R
37 lines (29 loc) · 764 Bytes
/
03.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
library(tidyverse)
get_mat <- function(dir) {
mat <- matrix(0, nrow = length(dir), ncol = length(dir))
init_i <- length(dir) / 2
init_j <- length(dir) / 2
mat[init_i,init_j] <- 1
for (i in 1:length(dir)) {
if (dir[i] == "^") {
init_i <- init_i - 1
} else if (dir[i] == "v") {
init_i <- init_i + 1
} else if (dir[i] == ">") {
init_j <- init_j + 1
} else if (dir[i] == "<") {
init_j <- init_j - 1
} else {
print("Problem")
}
mat[init_i,init_j] <- 1
}
mat
}
dir <- (scan("input01", character(0)) %>%
str_split(""))[[1]]
dir_santa <- dir[seq(1, length(dir), 2)]
dir_robo <- dir[seq(2, length(dir), 2)]
m1 <- get_mat(dir_santa)
m2 <- get_mat(dir_robo)
print(sum(pmax(m1, m2)))