-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.R
220 lines (175 loc) · 5.28 KB
/
day17.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
## PART 1 ----------------------------------------------------------------------
options(scipen = 999)
# To manage the size of the matrix, we remove the parts where no rock can
# possibly fall anymore. The most conservative criterion is: When a row is
# entirely filled with rocks, it becomes the new bottom.
# Alternatively, we can cut the cave below a row that has gaps that are no
# wider than 1 unit and no more than 4 units deep (so even the upright shape
# would not fit through).
# jets <- readLines("inputs/day17_ex.txt")
jets <- readLines("inputs/day17.txt")
tic <- Sys.time()
jets <- unlist(strsplit(jets, ""))
# is rock, 0 is empty space.
# 3 lines of empty space
cave <- t(matrix(rep(0, 7 * 3), nrow = 7))
shapes_add_rows <- vector("numeric", length = 5)
coordinates <- list()
## FALLING ROCK SHAPES ---------------------------------------------------------
# 1)
# ####
shapes_add_rows[1] <- 1 # add 1 row to the matrix
# Then, the coordinates are ...
coordinates[[1]] <-
data.frame(
x = 1,
y = 3:6
)
# 2)
# .#.
# ###
# .#.
shapes_add_rows[2] <- 3
coordinates[[2]] <-
data.frame(
x = c(1, rep(2, 3), 3),
y = c(4, 3:5, 4)
)
# 3)
# ..#
# ..#
# ###
shapes_add_rows[3] <- 3
coordinates[[3]] <-
data.frame(
x = c(1, 2, rep(3, 3)),
y = c(5, 5, 3:5)
)
# 4)
# #
# #
# #
# #
shapes_add_rows[4] <- 4
coordinates[[4]] <-
data.frame(
x = 1:4,
y = 3
)
# 5)
# ##
# ##
shapes_add_rows[5] <- 2
coordinates[[5]] <-
data.frame(
x = c(1, 1, 2, 2),
y = c(3, 4, 3, 4)
)
## LET ROCKS FALL --------------------------------------------------------------
# n_rocks <- 1000000000000
n_rocks <- 2022
jet_no <- 1
stack_height <- 0
pb <- txtProgressBar(min = 0, max = n_rocks, initial = 0, style = 3)
for (i_rock in 1:n_rocks) {
setTxtProgressBar(pb, i_rock)
# Check whether there are three empty rows above the highest point
cave_stats <- rle(apply(cave, 1, sum))
if (cave_stats$values[1] != 0) {
# make three empty rows appear on top
cave <- rbind(matrix(rep(0, ncol(cave) * 3), nrow = 3), cave)
} else if (cave_stats$lengths[1] != 3) {
# add required number or subtract it
row_diff <- 3 - cave_stats$lengths[1]
if (row_diff < 0) {
cave <- cave[-(1:abs(row_diff)), ]
} else {
cave <- rbind(matrix(rep(0, ncol(cave) * row_diff), nrow = row_diff), cave)
}
}
# Rock appears
rock_no <- ifelse(i_rock %% length(coordinates) == 0, length(coordinates), i_rock %% length(coordinates))
temp_coordinates <- coordinates[[rock_no]]
rock_falling <- TRUE
# Add rows to accommodate the new rock
cave <-
rbind(
matrix(rep(0, ncol(cave) * shapes_add_rows[rock_no]), nrow = shapes_add_rows[rock_no]),
cave
)
# Jets and downward movement until rock stops
while (rock_falling) {
# Jet
temp_jet <- jets[jet_no]
new_coordinates <- temp_coordinates
if (temp_jet == ">") { # try moving right
new_coordinates$y <- new_coordinates$y + 1
} else if (temp_jet == "<") { # try moving left
new_coordinates$y <- new_coordinates$y - 1
}
coordinates_valid <- all(new_coordinates$y %in% 1:(ncol(cave)))
if (coordinates_valid) {
hit_no_rock <- sum(cave[cbind(new_coordinates$x, new_coordinates$y)]) == 0
} else {
hit_no_rock <- FALSE
}
if (coordinates_valid & hit_no_rock) temp_coordinates <- new_coordinates
jet_no <- ifelse((jet_no + 1) %% length(jets) == 0, length(jets), (jet_no + 1) %% length(jets))
# move down
new_coordinates <- temp_coordinates
new_coordinates$x <- new_coordinates$x + 1
coordinates_valid <- all(new_coordinates$x %in% 1:(nrow(cave)))
if (coordinates_valid) {
hit_no_rock <- sum(cave[cbind(new_coordinates$x, new_coordinates$y)]) == 0
} else {
hit_no_rock <- FALSE
}
if (coordinates_valid & hit_no_rock) {
temp_coordinates <- new_coordinates
} else {
cave[cbind(temp_coordinates$x, temp_coordinates$y)] <- 3
rock_falling <- FALSE
}
}
# Rows that have gaps that are no more than 1 unit wide
target_rows <-
which(
apply(cave, 1, function(x) {
all(rle(x)$lengths[rle(x)$values == 0] == 1)
})
)
if (length(target_rows) > 1) {
# Which of these gaps are no more than 4 units deep?
for (i_row in seq_along(target_rows)) {
endpoint <- ifelse(target_rows[i_row] + 4 > nrow(cave), nrow(cave), target_rows[i_row] + 4)
if (is.null(ncol(cave[target_rows[i_row]:endpoint, ]))) next
gotcha <-
all(
apply(
cave[target_rows[i_row]:endpoint, ],
2,
sum
) != 0
)
if (gotcha & (target_rows[i_row] + 5 <= nrow(cave))) {
new_bottom <- target_rows[i_row] + 4
break
} else {
new_bottom <- NULL
}
}
if (!is.null(new_bottom)) {
if (!is.null(nrow(cave[(new_bottom + 1):nrow(cave), ]))) {
stack_height <- stack_height + nrow(cave[(new_bottom + 1):nrow(cave), ])
cave <- cave[1:new_bottom, ]
}
}
}
}
# Remove empty rows at top
row_sums <- apply(cave, 1, sum)
if (rle(row_sums)$values[1] == 0) cut_rows <- rle(row_sums)$lengths[1]
cave <- cave[-(1:cut_rows), ]
nrow(cave) + stack_height
# 3153
Sys.time() - tic