-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.R
45 lines (29 loc) · 977 Bytes
/
day5.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
stacks <- read.fwf("day5.input.txt", widths = c(-1, rep(4,9)), n=8, header = FALSE) |>
lapply(substr, 1, 1) |>
lapply(trimws) |>
lapply(Filter, f=nchar)
cmds <- read.table("day5.input.txt", header=FALSE, skip = 10, sep=' ')
cmds <- data.frame(n=cmds$V2, from=cmds$V4, to=cmds$V6)
for(i in seq_along(cmds$n)) {
n <- cmds$n[i]
to <- cmds$to[i]
from <- cmds$from[i]
for(j in seq(n)) {
stacks[[to]] <- c(stacks[[from]][1], stacks[[to]])
stacks[[from]] <- stacks[[from]][-1]
}
}
vapply(stacks, `[[`, "", 1) |> paste(collapse="")
# Part 2
stacks <- read.fwf("day5.input.txt", widths = c(-1, rep(4,9)), n=8, header = FALSE) |>
lapply(substr, 1, 1) |>
lapply(trimws) |>
lapply(Filter, f=nchar)
for(i in seq_along(cmds$n)) {
n <- cmds$n[i]
to <- cmds$to[i]
from <- cmds$from[i]
stacks[[to]] <- c(stacks[[from]][1:n], stacks[[to]])
stacks[[from]] <- stacks[[from]][-(1:n)]
}
vapply(stacks, `[[`, "", 1) |> paste(collapse="")