-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.R
35 lines (24 loc) · 878 Bytes
/
day13.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
#Day 13
setwd("C:/Users/David.simons/Documents/advent of code")
library(data.table)
input <- strsplit(readLines("day13.txt"), ": ")
#part 1 ----
wall <- rbindlist(lapply(input, function(line) {
list(depth = as.integer(line[1]), range = as.integer(line[2]))
}))
wall[, severity := ifelse(depth %% ((range-1)*2) == 0, depth*range, NA)]
print(sum(wall$severity, na.rm=T))
#part 2 ----
#actually much faster if list of lists vs dt!
wall <- lapply(input, function(line) {
list(depth = as.integer(line[1]), range = as.integer(line[2]))
})
caught <- function(delay) {
for (x in wall) {
if ((x$depth + delay) %% ((x$range-1)*2) == 0) return(T) #speed vs dt likely due to early stopping
}
return(F)
}
delay <- 0
while (caught(delay)) delay <- delay + 1 #kinda "brute force" but relatively efficient; takes ~24s
print(delay)