-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.R
55 lines (41 loc) · 1.34 KB
/
day14.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
51
52
53
54
55
# Example input, if needed
# day14 <- readLines("./puzzle_input/input_day14_exp.txt")
day14 <- readLines("./puzzle_input/input_day14.txt")
day14 <- do.call(rbind, strsplit(day14, ""))
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
# Rock IDs: We want to start with the first rock in the north row, then the
# second, then move one row south ...
round_rocks <-
data.frame(
which(day14 == "O", arr.ind = TRUE)
)
round_rocks <-
with(
round_rocks,
round_rocks[order(row, col), ]
)
for (i_rock in 1:nrow(round_rocks)) {
temp_pos <- round_rocks[i_rock, ]
roll_rock <-
ifelse(
length(day14[temp_pos$row - 1, temp_pos$col] %in% c("O", "#")) == 0,
FALSE,
!day14[temp_pos$row - 1, temp_pos$col] %in% c("O", "#")
)
while(roll_rock) {
temp_pos$row <- temp_pos$row - 1
roll_rock <-
ifelse(
length(day14[temp_pos$row - 1, temp_pos$col] %in% c("O", "#")) == 0,
FALSE,
!day14[temp_pos$row - 1, temp_pos$col] %in% c("O", "#")
)
}
day14[round_rocks$row[i_rock], round_rocks$col[i_rock]] <- "."
day14[temp_pos$row, temp_pos$col] <- "O"
}
# Number of round rocks in each row, multiplied with reverse row index
sum(apply(day14, 1, function(x) sum(x == "O")) * nrow(day14):1)
# 105249
Sys.time() - start