-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.R
50 lines (44 loc) · 1.7 KB
/
day11.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
#Day 11
setwd("C:/Users/David.simons/Documents/advent of code")
path <- factor(unlist(strsplit(readLines("day11.txt"), ",")), c("se", "sw", "ne", "nw", "s", "n"))
#part 1 ----
getDistance <- function(path){
counts <- table(path)
#s = se + sw (expand first to cancel later)
counts[["se"]] <- counts[["se"]] + counts[["s"]]
counts[["sw"]] <- counts[["sw"]] + counts[["s"]]
counts[["s"]] <- 0
#n = ne + nw (expand first to cancel later)
counts[["ne"]] <- counts[["ne"]] + counts[["n"]]
counts[["nw"]] <- counts[["nw"]] + counts[["n"]]
counts[["n"]] <- 0
#se + nw = 0
if (counts[["se"]] > counts[["nw"]]) {
counts[["se"]] <- counts[["se"]] - counts[["nw"]]
counts[["nw"]] <- 0
} else {
counts[["nw"]] <- counts[["nw"]] - counts[["se"]]
counts[["se"]] <- 0
}
#sw + ne = 0
if (counts[["sw"]] > counts[["ne"]]) {
counts[["sw"]] <- counts[["sw"]] - counts[["ne"]]
counts[["ne"]] <- 0
} else {
counts[["ne"]] <- counts[["ne"]] - counts[["sw"]]
counts[["sw"]] <- 0
}
#possibly done now if both wests or both easts "won". but if both norths or both souths "won" can combine them
#se + sw = s
counts[["s"]] <- min(counts[["sw"]], counts[["se"]])
counts[c("sw","se")] <- counts[c("sw","se")] - min(counts[["sw"]], counts[["se"]])
#ne + nw = n
counts[["n"]] <- min(counts[["nw"]], counts[["ne"]])
counts[c("nw","ne")] <- counts[c("nw","ne")] - min(counts[["nw"]], counts[["ne"]])
# no more cancellations possible
return(sum(counts))
}
print(getDistance(path))
#part 2 ----
tracking <- sapply(seq_along(path), function(i) getDistance(path[1:i])) #inefficent, but easiest way to re-use part 1
print(max(tracking))