-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.R
56 lines (37 loc) · 1.22 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
# Example input, if needed
# day09 <- readLines("puzzle_input/input_day09_exp.txt")
day09 <- readLines("puzzle_input/input_day09.txt")
day09 <- lapply(strsplit(day09, " "), as.numeric)
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
extrapolations <- vector("numeric", length(day09))
for (i_seq in seq_along(day09)) {
temp_seq <- day09[[i_seq]]
end_vals <- temp_seq[length(temp_seq)]
while (!all(temp_seq == 0)) {
temp_seq <- diff(temp_seq)
end_vals <- c(end_vals, temp_seq[length(temp_seq)])
}
new_vals <- cumsum(end_vals)
extrapolations[i_seq] <- new_vals[length(new_vals)]
}
sum(extrapolations)
# 1930746032
Sys.time() - start
## PART 2 ----------------------------------------------------------------------
start <- Sys.time()
extrapolations <- vector("numeric", length(day09))
for (i_seq in seq_along(day09)) {
temp_seq <- day09[[i_seq]]
start_vals <- temp_seq[1]
while (!all(temp_seq == 0)) {
temp_seq <- diff(temp_seq)
start_vals <- c(start_vals, temp_seq[1])
}
new_vals <-
Reduce("-", start_vals, accumulate = TRUE, right = TRUE)
extrapolations[i_seq] <- new_vals[1]
}
sum(extrapolations)
# 1154
Sys.time() - start