-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.R
88 lines (65 loc) · 2.23 KB
/
day09.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
instructions <- read.table("inputs/day09.txt")
# instructions <- read.table("inputs/day09_ex.txt")
names(instructions) <- c("direction", "moves")
source("day09_functions.R")
## PART 1 ----------------------------------------------------------------------
tic <- Sys.time()
# Track all individual positions of T, because we will need it to determine the
# unique positions of T later.
# Also save H so we can build a cool plot.
positions <-
data.frame(
xH = c(0, rep(NA, sum(instructions$moves))),
yH = c(0, rep(NA, sum(instructions$moves))),
xT = c(0, rep(NA, sum(instructions$moves))),
yT = c(0, rep(NA, sum(instructions$moves)))
)
# Because we're tracking every step anyways, we'll just turn the instructions
# into a series of steps of size 1
instructions <- rep(instructions$direction, times = instructions$moves)
# Initial tail positions
newT <- c(0, 0)
newH <- c(0, 0)
for (i in 1:length(instructions)) {
# move head first
newH <- move(instructions[i], c(positions$xH[i], positions$yH[i]))
# drag tail along
newT <- drag(newH, newT)
positions$xT[i + 1] <- newT[1]
positions$yT[i + 1] <- newT[2]
positions$xH[i + 1] <- newH[1]
positions$yH[i + 1] <- newH[2]
}
# Get all unique positions of T
nrow(unique(positions[c("xT", "yT")]))
# 6011
Sys.time() - tic
## PART 2 ----------------------------------------------------------------------
tic <- Sys.time()
positions <-
data.frame(
xK1 = c(0, rep(NA, length(instructions))),
yK1 = c(0, rep(NA, length(instructions)))
)
n_knots <- 10
for (i in 2:n_knots) {
positions[[paste0("xK", i)]] <- c(0, rep(NA, length(instructions)))
positions[[paste0("yK", i)]] <- c(0, rep(NA, length(instructions)))
}
for (i in 1:length(instructions)) {
# move head first
positions[i + 1, 1:2] <- move(instructions[i], c(positions$xK1[i], positions$yK1[i]))
# drag the other knots - each is dragged to the previous knot
for (j in 2:n_knots) {
positions[i + 1, (j * 2 - 1):(j * 2)] <-
drag(
positions[i + 1, ((j - 1) * 2 - 1):((j - 1) * 2)],
positions[i, (j * 2 - 1):(j * 2)]
)
}
}
# Get all unique positions of the tail
nrow(unique(positions[c("xK10", "yK10")]))
# 2419
Sys.time() - tic
# save(positions, file = "day09.RData")